Help with PHP & HTML (Beginner)

So, my lecturer gave me a PHP code (basically a register form) and told me to design it using css & html

Basically, I have already design the form in HTML & CSS . And already add the PHP code in it .Creating a database and tables . Done everything…

I have got one problem, When I run the form website, It seems that when I fill up the form and hit the submit button (thats where my problem is) Check back my cpanel> database > table> structure . It doesnt save the details that I have already fill up the form. Please Help Me !!!

I will show you my code…This is my HTML code

<!--//////FORM//////--><!--//////FORM //////--><!--/////FORM//////-->
<form action="<?php
 echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
  <!-- PHP--> 
  <!--  General -->
  <div class="form-group">
    <h2 class="heading">Booking & contact</h2>
    <div class="controls">
      <input type="text" id="name" class="floatLabel" name="name" value="<?= $varName;
	?>" />
      <label for="name">Name</label>
    </div>
    <div class="controls">
      <input type="text" id="email" class="floatLabel" name="email" value="<?= $varEmail;
	?>" />
      <label for="email">Email</label>
    </div>
    <div class="controls">
      <input type="tel" id="phone" class="floatLabel" name="phone" value="<?= $varPhone;
	?>" />
      <label for="phone">Phone</label>
    </div>
    <div class="grid">
      <div class="col-2-3">
        <div class="controls">
          <input type="text" id="street" class="floatLabel" name="street" value="<?= $varStreet;
	?>" />
          <label for="street">Street</label>
        </div>
      </div>
      <div class="col-1-3">
        <div class="controls">
          <input type="number" id="street-number" class="floatLabel" name="number" value="<?= $varNumber;
	?>" />
          <label for="street-number">Number</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <div class="col-2-3">
        <div class="controls">
          <input type="text" id="city" class="floatLabel" name="city" value="<?= $varCity;
	?>" />
          <label for="city">City</label>
        </div>
      </div>
      <div class="col-1-3">
        <div class="controls">
          <input type="text" id="post-code" class="floatLabel" name="post-code" value="<?= $varPostCode;
	?>" />
          <label for="post-code">Post Code</label>
        </div>
      </div>
    </div>
    <div class="controls">
      <input type="text" id="country" class="floatLabel" name="country" value="<?= $varCountry;
	?>" />
      <label for="country">Country</label>
    </div>
  </div>
  <!--  Details -->
  <div class="form-group">
    <h2 class="heading">Details</h2>
    <div class="grid">
      <div class="col-1-4 col-1-4-sm">
        <div class="controls">
          <input type="date" id="arrive" class="floatLabel" name="arrive" value="<?php
 echo date('Y-m-d'); ?>">
          <label for="arrive" class="label-date"><i class="fa fa-calendar"></i>&nbsp;&nbsp;Arrive</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="blank"></option>
            <option value="1" <?php if ($varPeople === "Less than 5")    echo ("selected=\"selected\""); ?>>Less than 5</option>
            <option value="2" <?php if ($varPeople === "More than 5")    echo ("selected=\"selected\""); ?>>More than 5</option>
            <option value="3" <?php if ($varPeople === "More than 10")    echo ("selected=\"selected\""); ?>>More than 10</option>
          </select>
          <label for="fruit"><i class="fa fa-male"></i>&nbsp;&nbsp;People</label>
        </div>
      </div>
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="blank"></option>
            <option value="deluxe" <?php if ($varTables === "Indoor")    echo ("selected=\"selected\""); ?>>Indoor</option>
            <option value="Zuri-zimmer" <?php if ($varTables === "Outdoor")    echo ("selected=\"selected\""); ?>>Outdoor</option>
            <option value="Zuri-zimmer" <?php if ($varTables === "VIP Room")    echo ("selected=\"selected\""); ?>>VIP Room</option>
          </select>
          <label for="fruit">Tables</label>
        </div>
      </div>
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="single-bed" <?php if ($varTime === "Breakfast")    echo ("selected=\"selected\""); ?>>Breakfast</option>
            <option value="double-bed" <?php if ($varTime === "Lunch")    echo ("selected=\"selected\""); ?>>Lunch</option>
            <!-- Add "selected >Lunch" when not using php code -->
            <option value="double-bed" <?php if ($varTime === "Dinner")    echo ("selected=\"selected\""); ?>>Dinner</option>
          </select>
          <label for="fruit">Time</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <p class="info-text">Please describe your needs e.g. Annual Dinner, Party</p>
      <br>
      <div class="controls">
        <textarea name="comments" class="floatLabel" id="comments"><?= $varComment;
	?>
</textarea>
        <label for="comments">Comments</label>
      </div>
      <button type="submit" value="Submit" class="col-1-4">Submit</button>
    </div>
  </div>
  <!-- /.form-group -->
</form>

THIS IS MY PHP CODE :

<?php 
if($_POST['formSubmit'] === "Submit")
{
	$errorMessage = "";
	
	if(empty($_POST['formName']))
	{
		$errorMessage .= "<li> You forgot to enter your name! </li>";
	}
	if(empty($_POST['formEmail']))
	{
		$errorMessage .= "<li>You forgot to enter a IC! </li>";
	}

	//Doesnt Have List
	$varName = $_POST['name'];
        $varEmail = $_POST['email'];
        $varPhone = $_POST['phone'];
        $varStreet = $_POST['street'];
        $varNumber = $_POST['street-number'];
        $varCity = $_POST['city'];
        $varPostCode = $_POST['post-code'];
        $varCountry = $_POST['country'];
        //Have List
        $varPeople = $_POST['arrive'];
        $varTables = $_POST['tables'];
        $varTime = $_POST['time'];
        $varComment = $_POST['comments'];
	
	if(empty($errorMessage))
	{
		
		
		$db = mysql_connect ('sql***.byethost*.com','b*_*******','password');
		if(!$db) die("Error connecting to MySQL database.");
		mysql_select_db('b*_*******_reservation',$db);
		
		$sql = "INSERT INTO form2 (Name, Email, Phone, Street, Number, City, PostCode, Country, People, Tables, Time, Comment) VALUES (".
		//Doesnt Have List
		PrepSQL($varName) . ", ".
		PrepSQL($varEmail) . ", ".
		PrepSQL($varPhone) . ", ".
		PrepSQL($varStreet) . ", ".
		PrepSQL($varNumber) . ", ".
		PrepSQL($varCity) . ", ".
		PrepSQL($varPostCode) . ", ".
		PrepSQL($varCountry) . ", ".
		//Have List
		PrepSQL($varPeople) . ", ".
		PrepSQL($varTables) . ", ".
		PrepSQL($varTime) . ", ".
		PrepSQL($varComment) . ") ";
		mysql_query($sql);
		
		header('Location:thankyou.html');
		exit();
	      }
	}
	
	//function:PrepSQL()
	//use stripslashes and mysql_real_escape_string PHP functions
	//tosanitize a string for use in an SQL query
	//
	//also puts single quotes around the string
	//
	function PrepSQL($value)
	{
		//Stripslashes
		if(get_magic_quotes_gpc())
		{
			$value = stripslashes($value);
		}
		
		//Quote
		$value = "'" . mysql_real_escape_string($value) . "'";
		
		return($value);
	}
?>

There must be some mistake that I did wrong , If you can help me … I would thanks you a million times

You are welcome to check the demohttp://teikfai.byethost8.com/Table/form2.php?ckattempt=1

Hi wan5, welcome to the forum

If you were told to design it using HTML & CSS you don’t need to be concerned about any PHP code or database stuff.

That PHP code it is a few years behind the times and has an appointment with the Grim Reaper already on the scroll.

Your HTML form elements

<form action="/Table/form2.php" method="post">
  <!-- PHP--> 
  <!--  General -->
  <div class="form-group">
    <h2 class="heading">Booking & contact</h2>
    <div class="controls">
      <input type="text" id="name" class="floatLabel" name="name" value="" />
      <label for="name">Name</label>
    </div>
    <div class="controls">
      <input type="text" id="email" class="floatLabel" name="email" value="" />
      <label for="email">Email</label>
    </div>
    <div class="controls">
      <input type="tel" id="phone" class="floatLabel" name="phone" value="" />
      <label for="phone">Phone</label>
    </div>
    <div class="grid">
      <div class="col-2-3">
        <div class="controls">
          <input type="text" id="street" class="floatLabel" name="street" value="" />
          <label for="street">Street</label>
        </div>
      </div>
      <div class="col-1-3">
        <div class="controls">
          <input type="number" id="street-number" class="floatLabel" name="number" value="" />
          <label for="street-number">Number</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <div class="col-2-3">
        <div class="controls">
          <input type="text" id="city" class="floatLabel" name="city" value="" />
          <label for="city">City</label>
        </div>
      </div>
      <div class="col-1-3">
        <div class="controls">
          <input type="text" id="post-code" class="floatLabel" name="post-code" value="" />
          <label for="post-code">Post Code</label>
        </div>
      </div>
    </div>
    <div class="controls">
      <input type="text" id="country" class="floatLabel" name="country" value="" />
      <label for="country">Country</label>
    </div>
  </div>
  <!--  Details -->
  <div class="form-group">
    <h2 class="heading">Details</h2>
    <div class="grid">
      <div class="col-1-4 col-1-4-sm">
        <div class="controls">
          <input type="date" id="arrive" class="floatLabel" name="arrive" value="2016-07-08">
          <label for="arrive" class="label-date"><i class="fa fa-calendar"></i>&nbsp;&nbsp;Arrive</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="blank"></option>
            <option value="1" >Less than 5</option>
            <option value="2" >More than 5</option>
            <option value="3" >More than 10</option>
          </select>
          <label for="fruit"><i class="fa fa-male"></i>&nbsp;&nbsp;People</label>
        </div>
      </div>
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="blank"></option>
            <option value="deluxe" >Indoor</option>
            <option value="Zuri-zimmer" >Outdoor</option>
            <option value="Zuri-zimmer" >VIP Room</option>
          </select>
          <label for="fruit">Tables</label>
        </div>
      </div>
      <div class="col-1-3 col-1-3-sm">
        <div class="controls"> <i class="fa fa-sort"></i>
          <select class="floatLabel">
            <option value="single-bed" >Breakfast</option>
            <option value="double-bed" >Lunch</option>
            <!-- Add "selected >Lunch" when not using php code -->
            <option value="double-bed" >Dinner</option>
          </select>
          <label for="fruit">Time</label>
        </div>
      </div>
    </div>
    <div class="grid">
      <p class="info-text">Please describe your needs e.g. Annual Dinner, Party</p>
      <br>
      <div class="controls">
        <textarea name="comments" class="floatLabel" id="comments"></textarea>
        <label for="comments">Comments</label>
      </div>
      <button type="submit" value="Submit" class="col-1-4">Submit</button>
    </div>
  </div>
  <!-- /.form-group -->
</form>

Because form elements need a name if they are to get submitted it can be seen that the PHP code you posted will never run.

Keep it simple while developing.

All you really need to do is have PHP var_dump or print_r the $_POST array to see if what you expected to be submitted is actually getting submitted.

1 Like

Use smart debugging and leave antiquated debugging in the past with mysql_* functions.

2 Likes

Others may disagree but something i do instead of trying to redirect the page to somewhere else i set a variable which i can use to hide the form and show the success message. The advantage with this is if you wanted to put something on there that you have to sign up to see you can’t by pass it and go to the success page. Also sometimes header location gets difficult if you output anything to the browser before it.

e.g. If after you submit the form you set $showform to = ‘1’ the below code should then hide the form.

<?php if(!($showform =='1')){ //show the form ;}else{ echo $successmessage;}?>'

1 Like

The more usual way is to use:

if ($_SERVER['REQUEST_METHOD'] == 'POST')

which tests if the page were accessed from posting the form or not.

1 Like

@felgall nice suggestion.
Beginners like me do not aware about this.

That would be good for the first check but that wouldn’t tell whether the script completed successfully whatever it was you wanted it to do, only to hide the form if the form is submitted. I tend to do server-side validation so if it fails it continues to show the forms and highlights any errors, if its successful it runs the update/insert/whatever and hides the form. If i just looked for ‘POST’ and hid the form i’d then have to get the user to click something to reset the form.

Unless i am missing something.

Hi guys, I have finally figure out what cause the problem.

It seems that I have to remove all the id =“example” between the type and class.

And many thanks to all of you guys too.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.