Reputation: 51
I'm trying to create a php form with a captcha question, but on submit the form fields are emptied (annoying if they haven't filled in all required fields etc.). I'm sure the solution must be an easy one.
Here's my submit code:
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
unset($_POST);
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
Upvotes: 1
Views: 8087
Reputation:
if you have the habit of leaving the "id" property always equal to the "name" property in your html elements, you can do somenthing like this:
<script type="text/javascript" src="path/to/jquery.min.js"></script>
<script type="text/javascript">
$().ready(function(){
//<?php foreach ($_POST as $fieldName => $fieldValue): ?>
$("#<?php echo $fieldName; ?>").val("<?php echo yourSanitizationFunction($fieldValue); ?>");
//<?php endforeach; ?>
});
</script>
This will keep your code cleaner than manually set the value in each field. But maybe you need to use a different technique to radio and check buttons.
Upvotes: 1
Reputation: 64526
Looks like you might be relying on register globals (unless you are setting $name
elsewhere), instead you should get the form data from $_POST
.
In order to keep the form values after submitting and failing validation, I recommend keeping an array that you can modify if the form has been submitted:
$display = array(
'name' => '',
'email' => '',
'message' => ''
);
if($_SERVER['REQUEST_METHOD'] == 'POST'){
foreach($_POST as $key => $value){
if(isset($display[$key])){
$display[$key] = htmlspecialchars($value);
}
}
}
In your form, set each value using the $display
array:
<form>
<input type="text" name="name" value="<?php echo $display['name']; ?>" />
<input type="text" name="email" value="<?php echo $display['email']; ?>" />
<input type="text" name="message" value="<?php echo $display['message']; ?>" />
</form>
Upvotes: 4
Reputation: 774
is not needed, do form with prefiled (posted) values
exists much easier solution
at form.php send correct cache HTTP headers (disable revalidate)
Date: Wed, 19 Mar 2014 11:44:01 GMT
Expires: Wed, 19 Mar 2014 11:44:01 GMT (means now, not history)
Cache-Control: private, max-age=0
Pragma: private
<form action="post.php"> ....
and at post.php on error found
<a href="javascript:window.history.go(-1);">go back</a> and correct error
form would be at same state as before posting
Upvotes: 0
Reputation: 4414
You'l need to echo
your form fields if data is filled, because by default on form postback it will re-load all HTMLs so it will clear fields.
You should do something like this:
<?php
$to="";
$from="";
$body="";
$subject="";
//check for "submit click"
if(isset($_POST['submit'])){
$to=$_POST['to'];
$from=$_POST['from'];
$subject=$_POST['subject'];
$body=$_POST['body'];
//your email code here
}
?>
<form action="your_script.php" method="post">
From: <input type="email" name="from" value="<?php echo $from;?>"/>
<br/>
To: <input type="email" name="to" value="<?php echo $to;?>"/>
<br/>
Subject: <input type="te3xt" name="subject" value="<?php echo $subject;?>"/>
<br/>
Body: <textarea name="body"><?php echo $body;?></textarea>
<br/>
From: <input type="email" name="from" value="<?php echo $from;?>"/>
<br/>
<input type="submit" name="submit" value="Send"/>
</form>
Upvotes: 0
Reputation: 97672
You'll actually have to set the values back e.g.
<input name="text" <?php isset($_POST['text'])?'value="'.htmlspecialchars($_POST['text']).'"':''; ?>>
...
<input name="check" type="checkbox" <?php isset($_POST['check'])?'checked':''; ?>>
each type of field may require a different technique.
Upvotes: 0
Reputation: 578
you are not getting values of form fields.
you should use like $name = $_POST['name'];
Upvotes: 1