Search code examples
phphtmlformsposting

HTML form is not posting the data through PHP


I am currently creating an interface for users to enter details in regarding their pets. I am using two php pages, one with a html form in which allows the users to enter the data and the other one to display the data. I have currently encountered two problems,

one being that when the "submit" button on the HTML form is clicked, it does not divert to the second page.

The second one being that the data which is being entered onto the first page through text boxes is not being posted to the next page for the users to get an overview.

I am very new to php, I will display all of the code as I feel that there are many errors. The code is not too overwhelming and I hope someone can help with these issues, thanks.

p.s. the first section of code is the page where users can enter the data, and the second section is the second page.

<?php
echo '<link href="style.css" rel="stylesheet">';
session_start();

$fname = $_POST["fname"];
$petname = $_POST["petname"];
$pettype = $_POST["pettype"]; 
$pdob = $_POST["pdob"];
$appdate = $_POST["appdate"];
$apptime = $_POST["apptime"];
$reason = $_POST["reason"];


 $validated = false;
 $_SESSION['login'] = "";

 if ($result > 0) $validated = true;
 if($validated)
{//Divert to the protected page to display information after.
$_SESSION['login'] = "OK";
 $_SESSION['fname'] = $fname;
 $_SESSION['petname'] = $petname;
 $_SESSION['pettype'] = $pettype;
 $_SESSION['pdob'] = $pdob;
 $_SESSION['appdate'] = $appdate;
 $_SESSION['apptime'] = $apptime;
 $_SESSION['reason'] = $reason;


 header('Location: protected.php');
 }
$_SESSION['login'] = "";
?>
<html>
   
 <body>
 <div align="center">

<h1 class="ribbon">
   <strong class="ribbon-content">Veterinary Appointment Form</strong>
</h1>
 <p><i>Please enter information reguarding your pet</i></p>
     
 <form action="protected.php"
method="post">

 <table>
 <tr>
 <td
align="right">Full Name* : </td>
 <td><input size=\"20\"
type="text" size="20" maxlength="50"
name="fname"></td>
 </tr>
 <tr>
 <td
align="right">Name of Pet* : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="petname"></td>
 </tr>
      <tr>
 <td
align="right">Pet Type* : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="pettype"></td>
 </tr>
      <tr>
 <td
align="right">Pet D.O.B : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="petdob"></td>
 </tr>
      <tr>
 <td
align="right">Date of Appointment* : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="appdate"></td>
 </tr>
           <tr>
 <td
align="right">Time of Appointment* : </td>
 <td><input size=\"20\"
type="text" size="40"
maxlength="50" name="apptime"></td>
 </tr>
                <tr>
 <td
align="right">Why Appoitnment is Needed : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="reason"></td>
 </tr>
 <tr>
 <td> </td>
 <td colspan="2"
align="left"><input type="submit"
value="Login"></td>
</div> </tr>
     <br>

 </table>
 </form>
 </body>
<img src="doglogo.png" alt="Logo" style="width:150;height:130;">
</html>
<?php

echo '<link href="style.css" rel="stylesheet">';
 {
 header('Location: ManageUser.php');
 exit();
 }

?>
<html>
 <head>
 
 <title>Welcome!</title> 

 </head>
<body> 
 <div align="center">
<h1 class="ribbon">
   <strong class="ribbon-content">Veterinary Appointment Form</strong>
</h1>
 <p><i>Here are your details</i></p>

<?php

 echo "<p>Full Name: ";
 echo $_SESSION['fname'];
echo "<br/>";
 echo "<p>Name of Pet: ";
 echo $_SESSION['petname'];
echo "<br/>";
 echo "<p>Pet Type: ";
 echo $_SESSION['pettype'];
echo "<br/>";
 echo "<p>Pet D.O.B: ";
 echo $_SESSION['pdob'];
echo "<br/>";
 echo "<p>Date of Appointment: ";
 echo $_SESSION['appdate'];
echo "<br/>";
 echo "<p>Time of Appointment: ";
 echo $_SESSION['apptime'];
echo "<br/>";
 echo "<p>Why Appoitnment is Needed: ";
 echo $_SESSION['reason'];
echo "<br/>";
 echo "</p>";

 {
 echo "<p><a href='ManageUser.php'><i>Click here to return</i></a></p>";
 }

?>
    
     <a href="#" onclick="window.print();return false;">Print this page</a>
     <br>
     <img src="doglogo.png" alt="Logo" style="width:150;height:130;">
</div>
</body>
    
</html>


Solution

  • This will always exit the script.

     {
         header('Location: ManageUser.php');
         exit();
     }
    

    Maybe you wanted to do something like this :

     if (something) // if the user didn't fill the form
     {
         header('Location: ManageUser.php');
         exit();
     }
    

    Also, be careful with :

    echo '<link href="style.css" rel="stylesheet">';
    

    This will display the tag before <html>, you may want to place it inside the <head> tag.

    You also forgot to add session_start() on the second page.

    To summarize :

    <?php
     session_start();
     if (something)
     {
     header('Location: ManageUser.php');
     exit();
     }
    
    ?>
    <html>
      <head>
        <title>Welcome!</title> 
        <link href="style.css" rel="stylesheet">
      </head>
    

    Bonus : this code

    <?php
    
     echo "<p>Full Name: ";
     echo $_SESSION['fname'];
    echo "<br/>";
     echo "<p>Name of Pet: ";
     echo $_SESSION['petname'];
    echo "<br/>";
     echo "<p>Pet Type: ";
     echo $_SESSION['pettype'];
    echo "<br/>";
     echo "<p>Pet D.O.B: ";
     echo $_SESSION['pdob'];
    echo "<br/>";
     echo "<p>Date of Appointment: ";
     echo $_SESSION['appdate'];
    echo "<br/>";
     echo "<p>Time of Appointment: ";
     echo $_SESSION['apptime'];
    echo "<br/>";
     echo "<p>Why Appoitnment is Needed: ";
     echo $_SESSION['reason'];
    echo "<br/>";
     echo "</p>";
    
     {
     echo "<p><a href='ManageUser.php'><i>Click here to return</i></a></p>";
     }
    
    ?>
    

    can be replaced by :

    <p>Full Name: <?=$_SESSION['fname']?></p>
    <br/>
    <p>Name of Pet: <?=$_SESSION['petname']?></p>
    <br/>
    <p>Pet Type: <?=$_SESSION['pettype']?></p>
    <br/>
    <p>Pet D.O.B: <?=$_SESSION['pdob']?></p>
    <br/>
    <p>Date of Appointment: <?=$_SESSION['appdate']?></p>
    <br/>
    <p>Time of Appointment: <?=$_SESSION['apptime']?></p>
    <br/>
    <p>Why Appoitnment is Needed: <?=$_SESSION['reason']?></p>
    <br/>
    <p><a href='ManageUser.php'><i>Click here to return</i></a></p>
    

    Be sure to check the rendered code with the W3C Validation tool. This will check if the HTML code is correct and tell you what is wrong.