I have a form that takes some user details, and allows them to upload a file. The file names and information is saved to a csv file.
If the user doesn't fill in all the fields, the form asks them to fill them and try again. I wanted it to remember the values the user had entered, so I set the default value of the form fields using php:
value="<?php print $fulltitle;?>"
where $fulltitle = $_POST['fulltitle'];
However, if the user submits something unsuccessfully, hitting reset does not clear the form, it returns their previous attempts.
How can I make the reset functionality work? In the reset button I tried to use
onclick="<?php $fulltitle='' ?>"
but it seems to have no effect, I guess because the PHP is already processed by the time you hit reset.
Anyone got any suggestions?
EDIT:
Thanks for the help, I solved this with a simple reload as suggested. For anyone looking for an answer to the problem:
In place of a reset button I put:
<FORM METHOD="LINK" ACTION="resetform.php">
<INPUT TYPE="submit" VALUE="Reset">
</FORM>
In the file resetform.php I have:
<?php
$firstname = '' ;
$lastname = '' ;
$fulltitle = '' ;
$email = '' ;
// etc.
header("Location: submitfile.php");
?>
where submitfile.php is the form
/EDIT
Trimmed code follows...
Header:
<?php
//error_reporting(E_ALL & ~E_NOTICE);
$message = false;
if(isset($_POST['Submit'])){
//user input with commas and < stripped:
$firstname = str_replace("<","",str_replace(",",";",$_POST['firstname']));
//if values missing, dont process:
if(empty($firstname)){
$message = '<span style="color:red;text-align:center;">Please complete all required(*) fields,<br />and check the file has been selected correctly.</span>';
$aClass = 'errorClass';
}
else {
//return data array cvsData from user input
$cvsData = $firstname
fwrite($fp,$cvsData);
fclose($fp);
//reset form
$firstname = '' ;
$message = '<span style="color:blue;text-align:center;">Submission successful, thank you.</span>';
}
}
//if page reloaded, clear form:
else {
$firstname = '' ;
}
?>
Body:
<form enctype="multipart/form-data" id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" >
<table>
<?php
if ($message != false)
{
print "<tr><td colspan='2'><span align='center'>" . $message . "</span></td></tr>";
}
?>
<tr>
<td width="200"><span class="texto">*First Name(s)</span></td>
<td><input type="text" name="firstname" id="firstname" value="<?php print $firstname;?>" /></td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="Submit" id="Submit" value="Submit"/>
<input type="reset" name="Reset" id="Reset" value="Reset" />
</div>
</td>
</tr>
</table>