PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a consultation form on my website that I've added a few verifications to. The problem is, now that I have these verifications, the client would lose the entire form if they submitted erroneous information into the three required fields.
I would like it so that if they happen to make a mistake that they do not lose all the information that they have already given.
<form method="post" action="#"> <!-- make sure form is posting via post and its posting back to this page -->
<input name="name" type="text" value="<?php echo htmlspecialchars($_POST['name'], ENT_QUOTES); ?>" size="27">
<form method="post" action="#"> <!-- make sure form is posting via post and its posting back to this page -->
<input name="name" type="text" value="<?php echo htmlspecialchars($_POST['name'], ENT_QUOTES); ?>" size="27">
Not working! What exactly are PHP short tags? All I can tell you is that within my HTML code (my main PHP code is outside of the HTML code) I have a small piece of PHP code that echos a confirmation message:
No I did not take a look at your code. I rarely download zip files that people post on forums. If you want me to look at your code, post it here. I can tell that you didn't try my code though because if $_GET is working, obviously you didn't ensure that you are posting via the POST method instead of GET. make sure your form tag opens like this:
Why are you redirecting to the page instead of simply posting to itself? Do you understand the code you are using? If you don't, then try something a little less complicated until you understand it.
<?php
if ($_POST['action'] == "send") {
// pseudo-code - do not copy and paste this - you'll have to ACTUALLY do what I'm emulating
if (!empty($_POST['name'] && strlen($_POST['name']) > 3) {
$msg = "You have successfully sent your email.";
} else {
$msg = "There were errors in your submission";
}
}
if (isset($msg)) echo "<p>" . $msg . "</p>";
?>
<form method="post" action="#">
<input type="hidden" name="action" value="send">
<div>Name: <input type="text" name="name" value="<?php if (isset($_POST['name'])) echo htmlentities($_POST['name'], ENT_QUOTES); ?>">
</form>
To be completely honest, I'm a complete newbie here. I'm working on my dad's website that was created for us. I'm quite good at computers and I learn quickly, but I'm definitely not a pro so far.
Sure can... you see, your script actually redirects back to the script you are rendering, and by doing so it loses all the $_POST variables on the way. My script posts directly back to itself, and so the $_POST variables are there. So take a look below I have added comments to describe what is happening...
<?php
if ($_POST['action'] == "send") { // if there is an index called "action" in the POST array with a value of "send" this means the form has been posted. We know this because we put that in a hidden field in the form below(see the hidden field down there?)
// this is where you'd perform your validation... if all of the $_POST variables didnt pass your tests, you'd assign an error message to $msg for display
if (!empty($_POST['name'] && strlen($_POST['name']) > 3) {
$msg = "You have successfully sent your email.";
} else {
// if the POST vars pass your validation, then assign a confirmation message to $msg
$msg = "There were errors in your submission";
}
}
// if we have a $msg to display, display it.
if (isset($msg)) echo "<p>" . $msg . "</p>";
?>
<!-- this uses # as the action because that will make it post back to THIS page -->
<form method="post" action="#">
<!-- this is so we know whether or not the form has been submitted -->
<input type="hidden" name="action" value="send">
<!-- You will notice that I checked that $_POST['name'] was set before trying to output it. This is because PHP will issue a NOTICE (or is it a warning?) if you try to use a variable that hasnt been assigned. If this form has not been posted, than $_POST['name'] wont exist and PHP will issue the NOTICE. You have to use htmlentities to avoid XSS attacks -->
<div>Name: <input type="text" name="name" value="<?php if (isset($_POST['name'])) echo htmlentities($_POST['name'], ENT_QUOTES); ?>">
</form>
Try copying and pasting this code into a file called test.php and play around with it a bit.
I got most of what you were saying. I replaced the "action="appointment.php"" with "appointment="#"" in my code to keep the values stored. I'm having difficulty seeing why it shouldn't work now?
if(strlen($name) < 1)
{
$strerr= "You must fill in all the required fields!";
header('location:appointment.php?msg='.$strerr); // this is redirecting... not necessary
}
else if(strlen($email) < 1)
{
$strerr= "You must fill in all the required fields!";
header('location:appointment.php?msg='.$strerr); // this is redirecting... not necessary
}
else if(strlen($phone) < 1)
{
$strerr= "You must fill in all the required fields!";
header('location:appointment.php?msg='.$strerr); // this is redirecting... not necessary
}
else if(strlen(preg_replace("/\D/",'',$phone)) != 10)
{
$strerr= "Your phone number must be 10 digits.";
header('location:appointment.php?msg='.$strerr); // this is redirecting... not necessary
}
else if(checkEmail($email) == FALSE)
{
$strerr= "Invalid e-mail address. Please try again!";
header('location:appointment.php?msg='.$strerr); // this is redirecting... not necessary
}