Jump to content

Search the Community

Showing results for tags 'php form validation not working'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • W3Schools
    • General
    • Suggestions
    • Critiques
  • HTML Forums
    • HTML/XHTML
    • CSS
  • Browser Scripting
    • JavaScript
    • VBScript
  • Server Scripting
    • Web Servers
    • Version Control
    • SQL
    • ASP
    • PHP
    • .NET
    • ColdFusion
    • Java/JSP/J2EE
    • CGI
  • XML Forums
    • XML
    • XSLT/XSL-FO
    • Schema
    • Web Services
  • Multimedia
    • Multimedia
    • FLASH

Calendars

  • Community Calendar

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Languages

Found 1 result

  1. Hello, new to PHP but w3schools has been VERY HELPFUL!. Been through tutorials over and over and all over the internet,. However I need help. I just haven't been able to find a successful answer for this. I am in the final stages of setting up my form. The user can fill it out. The form is coded to check for errors and this all works. YEAH! Took awhile to get to the point and super happy all this works 😀 BUT... 1. When the email is entered in the 'email' field, AND both or one of the other fields are left blank and showing error, the form is being sent to my email, even though two other fields on the form are blank and showing errors. How can I make sure the form is not sent (submitted) when there are still errors outstanding. 2. ALSO...when the form is complete and submitted with no errors, AND I get the email with the values, I want to redirect the user to a different page the says, basically, "Thank you" and acknowledges that their form has been sent successfully. I don't want this as an 'echo', but would like it as a redirect (crrently I have it coded as an 'echo'. I have tried the header("Location: http://www.example.com/thankyou.html"); and this doesn't work (not sure why as this seems fairly straight forward). The forms action is set to <?php echo htmlspecialchars($_SERVER["php_self"]);?>. I do want the form to check all errors on the same page, but would really like a new page redirect on submit. Any help greatly appreciated. My code may be unorganized as of yet, and possibly quite amateur, but here is what I am working on: PHP code is: $nameErr = $emailErr = $monthErr = ""; $name = $email = $month = $headers = $to =$subject = $body = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); $month = test_input($_POST["month"]); $to = "info@myemail.com"; // insert my own email address here $headers = "From: $email \r\n"; $subject = "Rentals Reservation Request"; $body .= "\r\n Name: " . $name; $body .= "\r\n Email: " . $email; //users email address $body .= "\r\n Month: " . $month; } function test_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } if (empty($_POST["name"])) { $nameErr = "<font color='red'>Name is required</font>"; $error .= "<li>Name is required. </li>"; } else { $name = test_input($_POST["name"]); } if (empty($_POST["email"])) { $emailErr = "<font color='red'>Email is required</font>"; $error .= "<li>Email is required.</li>"; } else { $email = test_input($_POST["email"]); } if (empty($_POST["month"])) { $monthErr = "Enter a month from the drop-down list"; $error .= "<li>Month is required.</li>"; } else { $month = test_input($_POST["month"]); } $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) { $nameErr = "<font color='red'>Only letters and spaces allowed"; } $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "<font color='red'>Invalid email format"; $error .= "<li>Invalid email format. </li>"; echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) occurred:</strong><br/>'. '<ul>' . $error . '</ul>' . '</p>'; } else { echo "Thank you. Your form has been sent."; mail($to, $subject, $body, $headers); } ---------------------------------------------------------- Page (HTML) Code is: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="generator" content="RocketCake"> <style> .error {color: red;} </style> <title>Rentals Reservation Form</title> <link rel="stylesheet" type="text/css" href="myformtest_php.css"> </head> <body> <div class="textstyle1"> <div id="container_12c6a992"> <div class="textstyle1"> <div id="container_21868f1"> <div id="container_21868f1_padding" > <div class="textstyle2"> <div id="container_100b4b7e" > <div id="container_100b4b7e_padding" > <div class="textstyle2"> <div id="php_5c4505e1"><?PHP $nameErr = $emailErr = $monthErr = ""; $name = $email = $month = $headers = $to =$subject = $body = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $email = test_input($_POST["email"]); $month = test_input($_POST["month"]); $to = "infor@myemail.com"; //insert my own email address here $headers = "From: $email \r\n"; $subject = "Rentals Reservation Request"; $body .= "\r\n Name: " . $name; $body .= "\r\n Email: " . $email; //users email address $body .= "\r\n Month: " . $month; } function test_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } if (empty($_POST["name"])) { $nameErr = "<font color='red'>Name is required</font>"; $error .= "<li>Name is required. </li>"; } else { $name = test_input($_POST["name"]); } if (empty($_POST["email"])) { $emailErr = "<font color='red'>Email is required</font>"; $error .= "<li>Email is required.</li>"; } else { $email = test_input($_POST["email"]); } if (empty($_POST["month"])) { $monthErr = "Enter a month from the drop-down list"; $error .= "<li>Month is required.</li>"; } else { $month = test_input($_POST["month"]); } $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) { $nameErr = "<font color='red'>Only letters and spaces allowed"; } $email = test_input($_POST["email"]); if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailErr = "<font color='red'>Invalid email format"; $error .= "<li>Invalid email format. </li>"; echo '<p class="error"><strong>Your message was NOT sent<br/> The following error(s) occurred:</strong><br/>'. '<ul>' . $error . '</ul>' . '</p>'; } else { echo "Thank you. Your form has been sent."; mail($to, $subject, $body, $headers); } ?></div> <span class="textstyle3"><br/></span> </div> <div class="textstyle1"> <span class="textstyle3"><br/></span> </div> <div class="textstyle2"> <div id="anchor_3f8a9634"><a name="FormTop"></a></div> <p><span class="error">* Required fields.</span></p> <form action="myformtest.php" enctype="application/x-www-form-urlencoded" method="POST" id="form_2d8b2532"> <div id="form_2d8b2532_padding" > <div class="textstyle1"> <span class="textstyle3">Please fill in all the necessary information:</span> </div> <div class="textstyle2"> <span class="textstyle4"><br/><br/><br/></span> <span class="textstyle4">Email address: </span> <input type="text" value="<?php echo $email;?>" title="" name="email" maxlength="50" id="edit_7a791d4e"<span class="error"> <font color='red'>*</font><?php echo $emailErr;?></span > <span class="textstyle4"><br/>(Please ensure the email address is correct or we will not be able to respond to you</span> <span class="textstyle4">)</span> <span class="textstyle4"> <br/></span> <span class="textstyle4"><br/> <br/>Name of Renter </span> <input type="text" value="<?php echo $name;?>" title="" name="name" placeholder="First & Last Name" id="edit_26871aa4" <span class="error"> <font color='red'>*</font><?php echo $nameErr;?></span > <span class="textstyle4"><br/><br/><br/>Month </span> <select name="month" title="" id="combo_740addf2" <span class="error"> <font color='red'>*</font><?php echo $monthErr;?></span //First MAY option is defunct but needs to be there for this to work// <option value="MAY" <?php echo (isset($_POST['month']) && $_POST['month'] == 'MAY') ? 'selected="selected"' : ''; ?>>MAY</option> <option value="">--Select--</option> <option value="MAY" <?php echo (isset($_POST['month']) && $_POST['month'] == 'MAY') ? 'selected="selected"' : ''; ?>>MAY</option> <option value="JUNE" <?php echo (isset($_POST['month']) && $_POST['month'] == 'JUNE') ? 'selected="selected"' : ''; ?>>JUNE</option > </select> <span class="textstyle4"><br/></span> <input type="hidden" value="" name="HiddenFormField5" id="field_ba6ca4e"<p><span class="error"><font color='red'> <font size ='4'>*</font><?php echo $monthErr;?></span </p > <span class="textstyle4"><br/><br/><br/><br/><br/><br/><br/><br/></span> </div> <div class="textstyle1"> <input name="formSubmit" type="submit" value="Submit" title="" id="button_25956e11" > </div> <div class="textstyle2"> <span class="textstyle4"><br/><br/><br/><br/><br/></span> </div> <div style="clear:both"></div> </div> </form> </div> <div class="textstyle1"> <span class="textstyle5"><br/></span> </div> <div class="textstyle2"> </div> <div style="clear:both"></div> </div> </div> </div> <div style="clear:both"></div> </div> </div> </div> </div> </div> <div class="textstyle2"> <span class="textstyle6"><br/></span> </div> </body> </html>
×
×
  • Create New...