Jump to content

need help with html and php

shadowss
Go to solution Solved by mariushm,

You can mix html code and php code in a single file, with the PHP extension.

 

Fragments of php code must be between <?php  and ?>

 

You don't generally want to escape the content received from the user as soon as you get it, instead only escape it when you're about to print it on page.

If you escape it like you do, then the text will be saved into the database escaped and later, someone may retrieve the text from database a second time and escape it again.

 

I like to have the variables initialized with some empty or default value, so that they will exist later in the page even if user didn't post

 

<?php

$submit = false;
$submit_valid = false;
$submit_errors = "";
$Title= "";
$Indhold = "";
$Uid = 0; // needed by add_post - figure it out what's that about

IF ($_SERVER['REQUEST_METHOD']=='POST') {
  $submit = true;  // keep track in a variable if data was submitted or not, for later
  $Title = $_POST['title']; //  input name = "title" 
  $Indhold = $_POST['content']; // input name="content"
  $Uid = $_POST['uid']; // if needed
  // optional validation of input, let's say we don't want to submit empty text boxes (or boxes that only have spaces)
  $submit_valid = true; // assume data is valid, now check it 
  
  if (trim($Title)=='') { $submit_valid = false; $submit_errors .= 'Type a title.';}
  if (trim($Indhold)=='') { $submit_valid = false; $submit_errors .= 'Type a content.';}
  
  if ($submit_valid == true) {
    // add the content to the database 
    add_post($Uid,$Title,$Indhold);
    // up to you if you want to clear the Title and Indhold variables.
  }
  
}

/* best not to have empty lines or spaces before <html> */ 
?><html>
  <head><title></title></head>
  <body>
    <h1> Opret Indlæg</h1>
    <?php 
    // if there was a post, we could either say message arrived, or failure and failure reason
    if ($submit==true) {
      if ($submit_valid==true) {
        echo '<p>Thank you. Message received and added to database.</p>';
        
      } else {
      	echo '<p>Sorry, there was at least one problem submitting your post : '.$submit_errors.'</p>';
      }
    }
    ?>
    <main>
      <form action="opret_rediger.php" method="post">
        <label for="title">Titel:</label>
        <input type="text" name="title" id="title" required value="<?php echo htmlspecialchars($Title); ?>" >
        <label for "content">Indhold:</label>
        <textarea name="content" id="content" rows="4" cols="50" required ><?php echo htmlspecialchars($Indhold); ?></textarea>
        <?php /*optional if you need to send that Uid with title and content */ ?>
        <input type="hidden" name="uid" value="<?php echo htmlspecialchars($Uid);?>">
        <button type="submit">Submit</button>
      </form>
    </main>
  </body>
</html>
      
        
        
                                                                   

 

hello im a school student and im sitting here trying to figure out to connect a php document to a html. the code i have is 


image.thumb.png.f0186960784d67d499fb5a36f3efb7a7.png
image.png.a87011c717df83618fed64e33f24616e.png

and this is what i get out of it 
 

image.png.3931c279228f66acebff714851ac08aa.png

image.png.0b69e81a13b1312170b282dace0af0e2.png


to me the html seem to be setup right from what i have been looking around the internet for but for some reason it just display the code i have in the php document instead of a html page


what im trying to do is be able to submit the title and text to a server there is already made i think our teacher want us to use something called add_post(string $uid, string $title, string $content);
which to me seem the same as the submit function in html if im not mistaken but im kinda stuck cause i just keep getting the code instead of a html page 

Link to comment
Share on other sites

Link to post
Share on other sites

and before someone is saying im just missing a closing of the php i have tried that too 

Link to comment
Share on other sites

Link to post
Share on other sites

You can mix html code and php code in a single file, with the PHP extension.

 

Fragments of php code must be between <?php  and ?>

 

You don't generally want to escape the content received from the user as soon as you get it, instead only escape it when you're about to print it on page.

If you escape it like you do, then the text will be saved into the database escaped and later, someone may retrieve the text from database a second time and escape it again.

 

I like to have the variables initialized with some empty or default value, so that they will exist later in the page even if user didn't post

 

<?php

$submit = false;
$submit_valid = false;
$submit_errors = "";
$Title= "";
$Indhold = "";
$Uid = 0; // needed by add_post - figure it out what's that about

IF ($_SERVER['REQUEST_METHOD']=='POST') {
  $submit = true;  // keep track in a variable if data was submitted or not, for later
  $Title = $_POST['title']; //  input name = "title" 
  $Indhold = $_POST['content']; // input name="content"
  $Uid = $_POST['uid']; // if needed
  // optional validation of input, let's say we don't want to submit empty text boxes (or boxes that only have spaces)
  $submit_valid = true; // assume data is valid, now check it 
  
  if (trim($Title)=='') { $submit_valid = false; $submit_errors .= 'Type a title.';}
  if (trim($Indhold)=='') { $submit_valid = false; $submit_errors .= 'Type a content.';}
  
  if ($submit_valid == true) {
    // add the content to the database 
    add_post($Uid,$Title,$Indhold);
    // up to you if you want to clear the Title and Indhold variables.
  }
  
}

/* best not to have empty lines or spaces before <html> */ 
?><html>
  <head><title></title></head>
  <body>
    <h1> Opret Indlæg</h1>
    <?php 
    // if there was a post, we could either say message arrived, or failure and failure reason
    if ($submit==true) {
      if ($submit_valid==true) {
        echo '<p>Thank you. Message received and added to database.</p>';
        
      } else {
      	echo '<p>Sorry, there was at least one problem submitting your post : '.$submit_errors.'</p>';
      }
    }
    ?>
    <main>
      <form action="opret_rediger.php" method="post">
        <label for="title">Titel:</label>
        <input type="text" name="title" id="title" required value="<?php echo htmlspecialchars($Title); ?>" >
        <label for "content">Indhold:</label>
        <textarea name="content" id="content" rows="4" cols="50" required ><?php echo htmlspecialchars($Indhold); ?></textarea>
        <?php /*optional if you need to send that Uid with title and content */ ?>
        <input type="hidden" name="uid" value="<?php echo htmlspecialchars($Uid);?>">
        <button type="submit">Submit</button>
      </form>
    </main>
  </body>
</html>
      
        
        
                                                                   

 

Link to comment
Share on other sites

Link to post
Share on other sites

29 minutes ago, mariushm said:

You can mix html code and php code in a single file, with the PHP extension.

 

Fragments of php code must be between <?php  and ?>

 

You don't generally want to escape the content received from the user as soon as you get it, instead only escape it when you're about to print it on page.

If you escape it like you do, then the text will be saved into the database escaped and later, someone may retrieve the text from database a second time and escape it again.

 

I like to have the variables initialized with some empty or default value, so that they will exist later in the page even if user didn't post

 

<?php

$submit = false;
$submit_valid = false;
$submit_errors = "";
$Title= "";
$Indhold = "";
$Uid = 0; // needed by add_post - figure it out what's that about

IF ($_SERVER['REQUEST_METHOD']=='POST') {
  $submit = true;  // keep track in a variable if data was submitted or not, for later
  $Title = $_POST['title']; //  input name = "title" 
  $Indhold = $_POST['content']; // input name="content"
  $Uid = $_POST['uid']; // if needed
  // optional validation of input, let's say we don't want to submit empty text boxes (or boxes that only have spaces)
  $submit_valid = true; // assume data is valid, now check it 
  
  if (trim($Title)=='') { $submit_valid = false; $submit_errors .= 'Type a title.';}
  if (trim($Indhold)=='') { $submit_valid = false; $submit_errors .= 'Type a content.';}
  
  if ($submit_valid == true) {
    // add the content to the database 
    add_post($Uid,$Title,$Indhold);
    // up to you if you want to clear the Title and Indhold variables.
  }
  
}

/* best not to have empty lines or spaces before <html> */ 
?><html>
  <head><title></title></head>
  <body>
    <h1> Opret Indlæg</h1>
    <?php 
    // if there was a post, we could either say message arrived, or failure and failure reason
    if ($submit==true) {
      if ($submit_valid==true) {
        echo '<p>Thank you. Message received and added to database.</p>';
        
      } else {
      	echo '<p>Sorry, there was at least one problem submitting your post : '.$submit_errors.'</p>';
      }
    }
    ?>
    <main>
      <form action="opret_rediger.php" method="post">
        <label for="title">Titel:</label>
        <input type="text" name="title" id="title" required value="<?php echo htmlspecialchars($Title); ?>" >
        <label for "content">Indhold:</label>
        <textarea name="content" id="content" rows="4" cols="50" required ><?php echo htmlspecialchars($Indhold); ?></textarea>
        <?php /*optional if you need to send that Uid with title and content */ ?>
        <input type="hidden" name="uid" value="<?php echo htmlspecialchars($Uid);?>">
        <button type="submit">Submit</button>
      </form>
    </main>
  </body>
</html>
      
        
        
                                                                   

 

ok thx a lot for the help was very valuable and got a lot out of it also found another way to do it but always nice to get to know about this 

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, mariushm said:

You can mix html code and php code in a single file, with the PHP extension.

 

Doing so is generally advised against, and for good reasons (maintenance, security).

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×