I have just started with PHP and I am having 2 issues with this form so far (I'm sure I will have many more lol)
This form is supposed to submit data to itself and repopulate is the back button is pressed.
First issue, for the radio buttons php is not reading the > before the text Mr/Mrs/Ms as the closing for the input tag.
Second issue, I am getting and "Undefined index" error for all fields.
The text fields sem to work though. If I type something, submit and then hit back the text is there.
<form method="POST"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Title:</td>
<td><input type="radio" name="title" value="Mr" <?php if ($_POST['title'] == "Mr") echo "CHECKED"; ?>>Mr.</td>
</tr><tr>
<td></td>
<td><input type="radio" name="title" value="Mrs" <?php if ($_POST['title'] == "Mrs") echo "CHECKED";?>>Mrs.</td>
</tr><tr>
<td></td>
<td><input type="radio" name="title" value="Ms" <?php if ($_POST['title'] == "Ms") echo "CHECKED";?>>Ms.</td>
</tr><tr>
<td>First Name:</td><td><input type="text" name="fname" width=50 value="<?php if ($_POST['fname']) echo $_POST[fname];?>" ></td>
Thank you in advance.
To fix the undefined index errors, you can use isset to check if the POST variables in question actually exist before you attempt to use them:
if(isset($_POST['my_form_value']) && $_POST['my_form_value'] == 'Mr'){
echo "CHECKED";
}
array_key_exists will also work, if that's what you prefer:
if(array_key_exists('my_form_value', $_POST) && $_POST['my_form_value'] == 'Mr'){
echo "CHECKED";
}
As for the tags, try using:
<input type="radio" name="title" value="Mrs" />
instead of:
<input type="radio" name="title" value="Mrs" >
Note the />
at the end of my example, as it actually closes the input tag.
change:
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
for this:
action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"
and
echo $_POST[fname]
for this:
echo $_POST['fname']