andrew_biggart Posted April 1, 2011 Share Posted April 1, 2011 Ok this should be straight forward but my tired brain cannot work this out. I'm creating a simple contact form and for some reason the validation isn't working. It wont go past the check fields are filled in validation. Can anyone spot what I'm doing wrong? <form method="get"> <h1 class='contact_form_h'>Contact Us</h1> <div id="login_response"></div> <input type='text' name='name' id='name' class='contact_form_input' value='Name' onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" /> <input type='text' name='email' id='email' class='contact_form_input' value='Email' onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" /> <textarea name='enquiry' id='enquiry' class='contact_form_textarea' rows='10' cols='10' onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;">Enquiry</textarea> <input type='submit' name='contact' id='contact' class='contact_form_submit' value='Contact Us' /> </form> <?php //Check if form is submitted if(isset($_GET['contact'])) { //Require check email function require "check_email.php"; //Variables $err_name=stripslashes($_GET['name']); $err_email=stripslashes($_GET['email']); $err_enquiry=stripslashes($_GET['enquiry']); $to="[email protected]"; $subject="Website Contact Form"; $from = stripslashes($_GET['name'])."<".stripslashes($_GET['email']).">"; $message = $err_enquiry; $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; //Check all form fields are filled in if ($_GET["name"]!='' OR $_GET["name"]!='Name' OR $_GET["email"]!='' OR $_GET["email"]!='Email' ) { if (isValidEmail($_GET['email'])){ //Send Mail if (@mail($to, $subject, $message, $headers)) { echo "3 - sent"; } else{ echo "2 - not"; } } else { echo "1 - not valid"; } } else { echo"0 - Fill in"; } } ?> Below is the check email script. <?php // This function tests whether the email address is valid function isValidEmail($email){ $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"; if (eregi($pattern, $email)){ return true; } else { return false; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232384-simple-task-frying-my-brain/ Share on other sites More sharing options...
Adam Posted April 1, 2011 Share Posted April 1, 2011 if ($_GET["name"]!='' OR $_GET["name"]!='Name' OR $_GET["email"]!='' OR $_GET["email"]!='Email' ) { You should use "&&" (or "AND") here, as you want to only continue if ALL of the checks pass. At the moment only one has to evaluate to true for the inputs to be considered valid. Also instead of checking for an empty string like that, use the empty() function: if (!empty($_GET["name"]) && $_GET["name"] != 'Name' && !empty($_GET["email"]) && $_GET["email"] != 'Email') { Currently if $_GET['name'] or $_GET['email'] do not exist, you'll get a PHP error notice. empty() performs an implicit isset() check, so you're checking that it exists, and that it's not empty, in one go. Which of the errors do you get back? Quote Link to comment https://forums.phpfreaks.com/topic/232384-simple-task-frying-my-brain/#findComment-1195416 Share on other sites More sharing options...
andrew_biggart Posted April 1, 2011 Author Share Posted April 1, 2011 Yea I noticed that as I posted it. I currently have this. <?php //Check if form is submitted //Require check email function require "check_email.php"; //Variables $err_name=stripslashes($_GET['name']); $err_email=stripslashes($_GET['email']); $err_enquiry=stripslashes($_GET['enquiry']); $to="[email protected]"; $subject="Website Contact Form"; $from = stripslashes($_GET['name'])."<".stripslashes($_GET['email']).">"; $message = $err_enquiry; $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; //Check all form fields are filled in if ($_GET["name"]!='' && $_GET["name"]!='Name' && $_GET["email"]!='' && $_GET["email"]!='Email' && $_GET["enquiry"]!='' && $_GET["enquiry"]!='Enquiry' ) { if (isValidEmail($_GET['email'])){ //Send Mail if (@mail($to, $subject, $message, $headers)) { echo "3"; } else{ echo "2"; } } else { echo "1"; } } else { echo"0"; } ?> The problem I am having now is with the ajax part of the form. I have tested the php and it now works but when the form is hooked up by ajax its getting stuck. Here is the ajax part of the form which alway echo's the please fill in all form fields error. /* ---------------------------- */ /* XMLHTTPRequest Enable */ /* ---------------------------- */ function createObject() { var request_type; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ request_type = new ActiveXObject("Microsoft.XMLHTTP"); }else{ request_type = new XMLHttpRequest(); } return request_type; } var http = createObject(); /* -------------------------- */ /* LOGIN */ /* -------------------------- */ /* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */ var nocache = 0; function send_email() { // Optional: Show a waiting message in the layer with ID ajax_response document.getElementById('login_response').innerHTML = "Loading..." // Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding. var name = encodeURI(document.getElementById('name').value); var email = encodeURI(document.getElementById('email').value); var enquiry = encodeURI(document.getElementById('enquiry').value); // Set te random number to add to URL request nocache = Math.random(); // Pass the login variables like URL variable http.open('get', 'send_email.php?name='+name+'&email='+email+'&enquiry='+enquiry+'&nocache = '+nocache); http.onreadystatechange = Reply; http.send(null); } function Reply() { if(http.readyState == 4){ var response = http.responseText; if(response == 0){ // if fields are empty document.getElementById('login_response').innerHTML = 'Please fill in all the fields.'; } else if(response == 1){ // if email isnt valid document.getElementById('login_response').innerHTML = 'Please enter a valid email address.'; } else if(response == 2){ // if email has been sent document.getElementById('login_response').innerHTML = 'Your email has been sent.'; } else if(response == 3){ // if email hasnt been sent document.getElementById('login_response').innerHTML = 'Your email has not been sent.'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/232384-simple-task-frying-my-brain/#findComment-1195417 Share on other sites More sharing options...
Adam Posted April 1, 2011 Share Posted April 1, 2011 Try alerting 'send_email.php?name='+name+'&email='+email+'&enquiry='+enquiry+'&nocache = '+nocache to ensure you're sending what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/232384-simple-task-frying-my-brain/#findComment-1195419 Share on other sites More sharing options...
andrew_biggart Posted April 1, 2011 Author Share Posted April 1, 2011 Not quite sure what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/232384-simple-task-frying-my-brain/#findComment-1195424 Share on other sites More sharing options...
Adam Posted April 1, 2011 Share Posted April 1, 2011 Before: http.open('get', 'send_email.php?name='+name+'&email='+email+'&enquiry='+enquiry+'&nocache = '+nocache); Add: alert('send_email.php?name='+name+'&email='+email+'&enquiry='+enquiry+'&nocache = '+nocache); Check to make you're passing in the data you expect. That will narrow the problem down to between the JavaScript or PHP. Quote Link to comment https://forums.phpfreaks.com/topic/232384-simple-task-frying-my-brain/#findComment-1195425 Share on other sites More sharing options...
andrew_biggart Posted April 1, 2011 Author Share Posted April 1, 2011 Yea it is definitely passing the php the correct values. The php is definitely doing its part so maybe its an issue with the reply function but i'm not sure what. Quote Link to comment https://forums.phpfreaks.com/topic/232384-simple-task-frying-my-brain/#findComment-1195430 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.