5 Replies - 681 Views - Last Post: 30 September 2017 - 11:50 AM Rate Topic: -----

#1 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-October 08

A Basic Form

Posted 29 September 2017 - 11:14 PM

Hello,
I am trying to code a form that should take some input and when submitted (to itself) display the submission in a table. when i run the code i get a 500 error.
Can anyone offer any help.
Thanks in advance.

<?php
 $Fname = $_POST["Fname"];
 $Lname = $_POST["Lname"];
 $gender = $_POST["gender"];
 $email = $_POST["email"];

if(isset($_POST['submit'])){
?>
<html>
<head>
	<title>Form Table</title>
</head>
<body>
	<form action="" method="post" accept-charset="utf-8">
		First Name:<input type="text" size="12" maxlength="15" name="Fname"><br/>
		Last Name:<input type="text" size="12" maxlength="20" name = "Lname"><br/>
		Gender:<br/>
		Male:<input type="radio" value="male" name="gender"><br/>
		Female:<input type="radio" value="female" name="gender"><br/>
		Email:<input type="text" size="15" maxlength="20" name="email"><br/>		
	</form>

<?php
} else {
	<Table style='border: 1px solid black; color:blue'>
		<tr>
		 <th>First Name:</th>
         <td>{$_POST['Fname']}</td> 
		</tr> 
		<tr>
		 <th>Last Name:</th>
		 <td>{$_POST['Lname']}</td> 
		</tr>
		<tr>
		 <th>Gender:</th>
		 <td>{$_POST['gender']}</td>
		</tr>
		<tr>
		 <th>Email:</th>
		 <td>{$_POST['email']}</td>
		</tr>
	</table> 
}
}
?>



Is This A Good Question/Topic? 0
  • +

Replies To: A Basic Form

#2 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: A Basic Form

Posted 29 September 2017 - 11:34 PM

The HTML inside the else clause is inside PHP tags, and therefore will be interpreted as PHP code, which will cause syntax errors. You need to escape out of PHP (like in the first IF clause) to print anything other than PHP.

Plus, there seems to be an extra closing bracket at the bottom of the code.

Lastly, you should only try to access $_POST after you've verified that the POST data is there, ideally using isset or empty, rather than just checking that the POST array is set.

// Don't do this:
$name = $_POST["name"];
$message = $_POST["message"];
if (isset($_POST["submit"])) {
    saveToDatabaseOrWhatever($name, $message);
}

// Instead do this:
if (isset($_POST["name"], $_POST["message"])) {
    saveToDatabaseOrWhatever($_POST["name"], $_POST["message"]);
}


Also there is little point in doing $name = $_POST["name"];. You can just as easily use $_POST["name"] when the value is needed.
Was This Post Helpful? 1
  • +
  • -

#3 williamscel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 25-October 08

Re: A Basic Form

Posted 30 September 2017 - 12:18 AM

View PostAtli, on 30 September 2017 - 12:34 AM, said:

The HTML inside the else clause is inside PHP tags, and therefore will be interpreted as PHP code, which will cause syntax errors. You need to escape out of PHP (like in the first IF clause) to print anything other than PHP.

Plus, there seems to be an extra closing bracket at the bottom of the code.

Lastly, you should only try to access $_POST after you've verified that the POST data is there, ideally using isset or empty, rather than just checking that the POST array is set.

// Don't do this:
$name = $_POST["name"];
$message = $_POST["message"];
if (isset($_POST["submit"])) {
    saveToDatabaseOrWhatever($name, $message);
}

// Instead do this:
if (isset($_POST["name"], $_POST["message"])) {
    saveToDatabaseOrWhatever($_POST["name"], $_POST["message"]);
}


Also there is little point in doing $name = $_POST["name"];. You can just as easily use $_POST["name"] when the value is needed.

Thanks for much. that tip did the trick. im not sure i understood what your meant about the variable declarations.
I modified my code to this:
<?php
if(isset($_POST['submit'])){
    $Fname = $_POST['Fname'];
    $Lname = $_POST['Lname'];
    $gender = $_POST['gender'];
    $email = $_POST['email'];

    echo "
	<table style='border-collapse: collapse; border: 1px solid black; color:black; text-align: left'>
		<tr>
		 <th style='border: 1px solid black'>First Name:</th>
         <td style='border: 1px solid black'>{$_POST['Fname']}</td> 
		</tr> 
		<tr>
		 <th style='border: 1px solid black'>Last Name:</th>
		 <td style='border: 1px solid black'>{$_POST['Lname']}</td> 
		</tr>
		<tr>
		 <th style='border: 1px solid black'>Gender:</th>
		 <td style='border: 1px solid black'>{$_POST['gender']}</td>
		</tr>
		<tr>
		 <th style='border: 1px solid black'>Email:</th>
		 <td style='border: 1px solid black'>{$_POST['email']}</td>
		</tr>
	</table> ";

}
else{
?>
<html>
<head>
	<title>Form Table</title>
</head>
<body>
	<form action="" method="post" accept-charset="utf-8">
		First Name:<input type="text" size="12" maxlength="15" name="Fname"><br/>
		Last Name:<input type="text" size="12" maxlength="20" name = "Lname"><br/>
		Gender:<br/>
		Male:<input type="radio" value="male" name="gender"><br/>
		Female:<input type="radio" value="female" name="gender"><br/>
		Email:<input type="text" size="15" maxlength="20" name="email"><br/>
		<input type="submit" value="submit" name="submit">		
	</form>
</body>
</html>
<?php
}
?>



View Postwilliamscel, on 30 September 2017 - 01:17 AM, said:

View PostAtli, on 30 September 2017 - 12:34 AM, said:

The HTML inside the else clause is inside PHP tags, and therefore will be interpreted as PHP code, which will cause syntax errors. You need to escape out of PHP (like in the first IF clause) to print anything other than PHP.

Plus, there seems to be an extra closing bracket at the bottom of the code.

Lastly, you should only try to access $_POST after you've verified that the POST data is there, ideally using isset or empty, rather than just checking that the POST array is set.

// Don't do this:
$name = $_POST["name"];
$message = $_POST["message"];
if (isset($_POST["submit"])) {
    saveToDatabaseOrWhatever($name, $message);
}

// Instead do this:
if (isset($_POST["name"], $_POST["message"])) {
    saveToDatabaseOrWhatever($_POST["name"], $_POST["message"]);
}


Also there is little point in doing $name = $_POST["name"];. You can just as easily use $_POST["name"] when the value is needed.

Thanks for much. that tip did the trick. im not sure i understood what your meant about the variable declarations.
I modified my code to this:
<?php
if(isset($_POST['submit'])){
    $Fname = $_POST['Fname'];
    $Lname = $_POST['Lname'];
    $gender = $_POST['gender'];
    $email = $_POST['email'];

    echo "
	<table style='border-collapse: collapse; border: 1px solid black; color:black; text-align: left'>
		<tr>
		 <th style='border: 1px solid black'>First Name:</th>
         <td style='border: 1px solid black'>{$_POST['Fname']}</td> 
		</tr> 
		<tr>
		 <th style='border: 1px solid black'>Last Name:</th>
		 <td style='border: 1px solid black'>{$_POST['Lname']}</td> 
		</tr>
		<tr>
		 <th style='border: 1px solid black'>Gender:</th>
		 <td style='border: 1px solid black'>{$_POST['gender']}</td>
		</tr>
		<tr>
		 <th style='border: 1px solid black'>Email:</th>
		 <td style='border: 1px solid black'>{$_POST['email']}</td>
		</tr>
	</table> ";

}
else{
?>
<html>
<head>
	<title>Form Table</title>
</head>
<body>
	<form action="" method="post" accept-charset="utf-8">
		First Name:<input type="text" size="12" maxlength="15" name="Fname"><br/>
		Last Name:<input type="text" size="12" maxlength="20" name = "Lname"><br/>
		Gender:<br/>
		Male:<input type="radio" value="male" name="gender"><br/>
		Female:<input type="radio" value="female" name="gender"><br/>
		Email:<input type="text" size="15" maxlength="20" name="email"><br/>
		<input type="submit" value="submit" name="submit">		
	</form>
</body>
</html>
<?php
}
?>


Also i added the last "<?php}>" bit but im not quite sure why i had to do that to make it work.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: A Basic Form

Posted 30 September 2017 - 02:18 AM

There is no need to fumble with the Quote buttons, there is a Reply button further down the page, or use the Fast Reply box.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 13,474
  • Joined: 08-August 08

Re: A Basic Form

Posted 30 September 2017 - 05:05 AM

As you were told, the <?php and ?> tell the php interpreter that the code between them is to be processed as php. Anything not between them is passed on to the browser.

Suppose you have a variable $y that held a value you needed to output. Would you do this?
$x = $y;
echo $x;

Or would you do this?
echo $x;


You're doing the first with $_POST values. Remember that $_POST is a superglobal variable so it can be used the same way as any other variable.

This post has been edited by CTphpnwb: 30 September 2017 - 05:05 AM

Was This Post Helpful? 0
  • +
  • -

#6 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 131
  • Joined: 28-March 15

Re: A Basic Form

Posted 30 September 2017 - 11:50 AM

There are still a few issues with your code. From the top down...

You need to check the REQUEST METHOD, not hope the name of a button is being submitted in order for your script to work. That will completely fail in certain circumstances. You need to use if ($_SERVER['REQUEST_METHOD'] == 'POST')

Get rid of the variables for nothing.

Stay away from echoing HTML. Just go like <td style='border: 1px solid black'><?= $_POST['Fname'] ?></td>

Remove the action=""

You probably don't need accept-charset

Use HTML5. It's almost 2018

Decide on a consistent naming convention and stick to it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1