simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 Ken, just for fun I modified the query to the more 'traditional' values statements like this: <? $sql = "INSERT INTO leads (first_name, last_name, email, day_phone, alt_phone, best_time, address, city, state, zip, type_mortgage, first_company, months_behind, back_payments, notice_of_foreclosure, auction_date, date_of_auction, second_company, combined_balances, combined_payments, in_bankruptcy, situation, keep_property, referred_by, date ) VALUES ('".$clean_data['first_name']."', '".$clean_data['lastname']."', '".$clean_data['email']."', '".$clean_data['day_phone']."', '".$clean_data['alt_phone']."', '".$clean_data['best_time']."', '".$clean_data['address']."', '".$clean_data['city']."', '".$clean_data['state']."', '".$clean_data['zip']."', '".$clean_data['type_mortgage']."', '".$clean_data['first_company']."', '".$clean_data['months_behind']."', '".$clean_data['back_payments']."', '".$clean_data['notice_of_foreclosure']."', '".$clean_data['auction_date']."', '".$clean_data['date_of_auction']."', '".$clean_data['second_company']."', '".$clean_data['combined_balances']."', '".$clean_data['combined_payments']."', '".$clean_data['in_bankruptcy']."', '".$clean_data['situation']."', '".$clean_data['keep_property']."', '".$clean_data['referred_by']."', '$today')"; ?> Now at least the page shows up But, when I click on submit without filling in any form fields I get this: Array ( [first_name] => [last_name] => => [day_phone] => [alt_phone] => [best_time] => [address] => [city] => [state] => [zip] => [type_mortgage] => [first_company] => [months_behind] => [back_payments] => [date_of_auction] => [second_company] => [combined_balances] => [combined_payments] => [situation] => [referred_by] => -- Please Select One -- [submit_x] => 87 [submit_y] => 5 ) No recipient addresses found in header No errors are displayed for empty fields or anything. Plus, the code runs for the mail and the insert without stopping for the validation Here's the message when first arriving on the page just for reference. This is before hitting submit or anything: Array ( ) No recipient addresses found in header This is such a baffler. All i'm trying to do is validate the entries, stop the form from running the query and send the mail until all required fields are inserted. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 Can you post the entire script again. I have a feeling that there's a misplaced closing bracket that is ending an "if" statement too soon. Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 Good morning Here's the whole ball of wax with the 'tweaks' included: <?php include 'db_config.inc'; $err_message = array(); $clean_data = array(); $qtmp = array(); $today = date("F j, Y, g:i a"); if (isset($_POST['submit'])) { foreach ($_POST as $fld => $val) { switch($fld) { case 'first_name': case 'last_name': case 'email': case 'day_phone': if (strlen(trim(stripslashes($val))) == 0) $err_message[] = 'Your first name, last name, email address and daytime phone number are required. Please complete those fields and resubmit.'; else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'address': case 'city': case 'state': case 'zip': if (strlen(trim(stripslashes($val))) == 0) $err_message[] = 'Please complete ALL fields in the property information section and resubmit'; else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'email': if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $err_message[] = $email. ' is not a valid email address. Please enter a valid email address.'; else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'submit': break; default: $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } } } if (!empty($err_messge)) { echo "<span style='color:red'>" . implode("<br>\n",$err_message) . "<br>\n"; } else { $link = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname, $link) or die(mysql_error()); // run our query to insert the record $sql = "INSERT INTO leads set " . implode(', ',$qtmp); // this uses the alternative format of the MySQL insert command $results = mysql_query($sql, $link) or die(mysql_error()); $new_id = mysql_insert_id(); // start mail process $mailContent="--------CONTACT--------\n" ."Name: ".$clean_data['first_name']." ".$clean_data['lastname']."\n" ."Date: ".$today."\n" ."E-mail: ".$clean_data['email']."\n\n--------PHONE--------\n" ."Phone: ".$clean_data['day_phone']."\n" ."Alt Phone: ".$clean_data['alt_phone']."\n" ."Best time to call: ".$clean_data['best_time']."\n\n--------Details--------\n" ."Address: ".$clean_data['address']."\n" ."City: ".$clean_data['city']."\n" ."State: ".$clean_data['state']."\n" ."Zip: ".$clean_data['zip']."\n\n---------Foreclosure Info---------\n" ."Loan type: ".$clean_data['type_mortgage']."\n" ."First Mtg: ".$clean_data['first_company']."\n" ."Months behind: ".$clean_data['months_behind']."\n" ."Back payments: ".$clean_data['back_payments']."\n" ."Notice of foreclosure filed: ".$clean_data['notice_of_foreclosure']."\n" ."Auction date set: ".$clean_data['auction_date']."\n" ."Date of auction: ".$clean_data['date_of_auction']."\n" ."Second mortgage: ".$clean_data['second_company']."\n" ."Combined balances: ".$clean_data['combined_balances']."\n" ."Combined payments: ".$clean_data['combined_payments']."\n" ."Are you in bankruptcy: ".$clean_data['in_bankruptcy']."\n" ."Situation detail: ".$clean_data['situation']."\n" ."Keep or Sell: ".$clean_data['keep_property']."\n\n----------Other---------\n" ."Referred by: ".$clean_data['referred_by']."\n"; //---------------------------------- $toAddress="bsniff@gmail.com"; /* change this! */ $subject="WA HomeSaver Inquiry"; /* change this! */ $recipientSubject="WA HomeSaver Inquiry"; /* change this! */ $receiptMessage = "Thank you ".$fname." for inquiring at WAHomeSaver.com's website!\n\n\nHere is the contact information you submitted to us:\n\n" ."--------CONTACT--------\n" ."Name: ".$clean_data['first_name']." ".$clean_data['lastname']."\n" ."E-mail: ".$clean_data['email']."\n\n--------PHONE--------\n" ."Phone: ".$clean_data['day_phone']."\n" ."Alternate phone: ".$clean_data['alt_phone']."\n" ."Best time to contact? ".$clean_data['best_time']."\n"; //---------------------------------- mail($email, $subject, $receiptMessage,"From:$toAddress"); //---------------------------------- mail($toAddress,$recipientSubject,$mailContent,"From:".$clean_data['email'].""); } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 I was correct in my guess. There is a mis-placed closing bracket that closed the "if (isset($_POST['submit']))" too soon. Move the closing bracket before: <?php if (!empty($err_messge)) { ?> to the end of the script. If you could post the form that drives this script, I could take a look at the whole package and see if it could be improved... Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, made the change but the page still does nothing in response to the errors. I'm using this code to cycle through and display them in the page. But, nothing shows: <?php if(!empty($err_message)) { echo "<strong id='errorTitle'>One or more input fields on the form has not been completed</strong>"; echo "<ul>"; foreach ($err_message as $value) { echo "<li>$value</li>"; } echo "</ul>"; } ?> Here's the url: http://www.wahomesaver.com/response3.php Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 I see what is going on and I've spent some time fixing the problems: 1) I misspelled "$err_message" in the "if". Change: <?php if (!empty($err_messge)) ?> to <?php if (!empty($err_message)) ?> 2) You're using an image for the "submit" button, so we have to test for the presence of either "submit_x" or "submit_y". change: <?php if (isset($_POST['submit'])) { ?> to <?php if (isset($_POST['submit_x'])) { ?> 3) The way I coded the script, each error message would have come out a maximum of 4 times. I made a modification so that each type of error message will list the fields not filled in. Put this at the start of your script: <?php $field_names = array('first_name' => 'First Name', 'last_name' => 'Last Name','email' => 'Email', 'day_phone' => 'Day Phone', 'address' => 'Address', 'city' => 'City', 'state'=>'State', 'zip' => 'Zip'); $required_fields = array(); $property_fields = array(); ?> The "switch" now becomes: <?php switch($fld) { case 'first_name': case 'last_name': case 'day_phone': if (strlen(trim(stripslashes($val))) == 0) { $required_fields[] = $field_names[$fld]; }else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'address': case 'city': case 'state': case 'zip': if (strlen(trim(stripslashes($val))) == 0) { $property_fields[] = $field_names[$fld]; } else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'email': if (strlen(trim(stripslashes($val))) == 0) $required_fields[] = $field_names[$fld]; elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $err_message[] = $email. ' is not a valid email address. Please enter a valid email address.'; else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'submit': case 'submit_x': case 'submit_y': break; default: $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } ?> and the printing of the error messages now becomes <?php if (!empty($required_fields)) { $err_message[] = 'The following required fields were not entered:'; $err_message[] = implode(',',$required_fields); } if (!empty($property_fields)) { $err_message[] = '<br>The following property fields were not entered:'; $err_message[] = implode(',',$property_fields); } if (!empty($err_message)) { echo "<span style='color:red'>" . implode("<br>\n",$err_message) . "<br>\n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 Ken, ok...made those changes. For some reason the server doesn't like line 49: Warning: mysql_real_escape_string(): A link to the server could not be established in /home/content/C/o/r/Corbaley8076/html/response4.php on line 49 Warning: mysql_real_escape_string(): Can't connect to local MySQL server through socket '/usr/local/mysql-5.0/data/mysql.sock' (2) in /home/content/C/o/r/Corbaley8076/html/response4.php on line 49 Line 49 is: $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; The top of the page is covered with this error repeated about 15 times plus it's echoing the form field errors above the page. Check it out at: http://www.wahomesaver.com/response4.php I know it's throwing a MySQL error but i'm in the database without problems. Not sure why it's stating that. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 It's doing that because you're not connecting to the database until after the checks are done, move <?php $link = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname, $link) or die(mysql_error()); ?> to the beginning of your script after <?php include 'db_config.inc'; ?> Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 DOHHHHHHHH! ok, i'm ashamed on that one :-\ Alright, in testing it is doing it's job except for the email validation. Throwing an error that the email is invalid format when I know it's not. I think the eregi line might have a glitch. One or more input fields on the form has not been completed * is not a valid email address. Please enter a valid email address. Also, Ken, so the person doesn't have to complete the form all over again if an error surfaces (which right now wipes the form clean) can I use the value="<? echo ".$clean_data['first_name']."; ?>" method to display their input? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 That I can't help you with. I've never gotten the hang of regular expressions. Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 I don't think it's the eregi itself. Looking closely at the error message displayed, it should also include the input for that field. It's not, which indicates to me that it's not setting a value for $email. Since it's not setting a value then it's throwing the error. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 It wasn't puting in the value of the email address entered because the line should look like: <?php $err_message[] = $val. ' is not a valid email address. Please enter a valid email address.'; ?> Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, that makes perfect sense. Now the field entry shows up in the error message display. But, it's still showing an error yet it's executing the 'else' code which inserts into the db and sends the emails. Essentially it's stating an error but not stopping the script from running as if there was no error. In the eregi statement it references the $email variable again. I've tried replacing that with $val and that returned a 500 error. Not sure what to replace that with. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 I just tested my test code and it works fine. Here is the "case" for "email" that I have: <?php case 'email': if (strlen(trim(stripslashes($val))) == 0) $required_fields[] = $field_names[$fld]; elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $val)) $err_message[] = $val. ' is not a valid email address. Please enter a valid email address.'; else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_escape_string($clean_data[$fld]) . "'"; } break; ?> In my test code, I echo the query after it's built and that code is not running, so the if/else sequences are working. Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 ARRRRRRRRGHHHHHH! I had tried the exact same code and produced a 500 server error. Just to make sure, I just copied/pasted your snippet over that part of the script and ran it. 500 server error! Honestly i'm beginning to think that this crap ass Godaddy hosting is the problem. Go figure. Here's exactly what's in the script up to the 'else' statement: <?php include 'db_config.inc'; // connect to db $link = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname, $link) or die(mysql_error()); //set variable arrays $field_names = array('first_name' => 'First Name', 'last_name' => 'Last Name','email' => 'Email', 'day_phone' => 'Day Phone', 'address' => 'Address', 'city' => 'City', 'state'=>'State', 'zip' => 'Zip'); $required_fields = array(); $property_fields = array(); $err_message = array(); $clean_data = array(); $qtmp = array(); $today = date("F j, Y, g:i a"); //run validation if (isset($_POST['submit_x'])) { foreach ($_POST as $fld => $val) { switch($fld) { case 'first_name': case 'last_name': case 'day_phone': if (strlen(trim(stripslashes($val))) == 0) { $required_fields[] = $field_names[$fld]; }else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'address': case 'city': case 'state': case 'zip': if (strlen(trim(stripslashes($val))) == 0) { $property_fields[] = $field_names[$fld]; } else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } break; case 'email': if (strlen(trim(stripslashes($val))) == 0) $required_fields[] = $field_names[$fld]; elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $val)) $err_message[] = $val. ' is not a valid email address. Please enter a valid email address.'; else { $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_escape_string($clean_data[$fld]) . "'"; } break; case 'submit': case 'submit_x': case 'submit_y': break; default: $clean_data[$fld] = trim(stripslashes($val)); $qtmp[] = $fld . " = '" . mysql_real_escape_string($clean_data[$fld]) . "'"; } } // establish errors if (!empty($required_fields)) { $err_message[] = 'The following required fields were not entered:'; $err_message[] = implode(',',$required_fields); } if (!empty($property_fields)) { $err_message[] = '<br>The following property fields were not entered:'; $err_message[] = implode(',',$property_fields); } if (!empty($err_message)) { //echo "<span style='color:red'>" . implode("<br>\n",$err_message) . "<br>\n"; } else { ?> The weird part is that it's sending the mails AND inserting the data! But slams this funky 500 error trying to validate the email address as legit. Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 19, 2007 Share Posted April 19, 2007 I didn't see the whole post, but it seems as if you are trying a little too hard. Use something simple, like. function validateemail($email) { global $errorhandler; // set regular expression to test email if ($email == "") { return false; } $regexemail = "^[A-Za-z0-9\._-]+@([A-Za-z0-9][A-Za-z0-9-]{1,62})(\.[A-Za-z][A-Za-z0-9-]{1,62})+$"; if (!ereg($regexemail, $email)) { // test for formatting return false; } return true; } Instead of eregi. If for some reason the error is still coming I am pretty sure if you check an info page <?php phpinfo(); ?> there is one or more "forbidden" functions, that might be throwing that error. They could also have PHP running in safe mode, and a lot of functions don't work in safemode. However knowing go-daddy they are one of the best and I have never encountered those types of settings, chances are it's something to do with an htaccess file or something somewhere else. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 Ok, turns out it's NOT the eregi that's causing the 500 error. It's something after that. The script is doing everything it's supposed to do: 1: check the fields - OK 2: validate email - OK 3: run the query - OK 4: email the people - OK THEN it crashes with the 500 error. So it's something at the end of the script... ??? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 19, 2007 Share Posted April 19, 2007 What browser are you using? If you're generating invalid HTML, this might be the problem. Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted April 19, 2007 Author Share Posted April 19, 2007 FIRST THING... Thank ALL of you! For your help and patience in working through this! :) Now, I found the problem! Unfortunately, until now, the customer's hosting account didn't have access to the error logs so I couldn't see what was causing the 500 error as everything else was working as it should. So, I got him to 'upgrade' his account (can't believe Godaddy charges extra to have error logs!) and was able to see immediately what the problem was: [Thu Apr 19 15:53:07 2007] [error] [client 24.17.215.76] malformed header from script. Bad header=No recipient addresses found i: /var/chroot/home/content/C/o/r/Corbaley8076/html/response4.php The mail function was looking for a value in a variable named $email which wasn't populated. This was causing the page/script to crash at the end causing the 500 error. I added the $email = $_POST['email']; params and voila! No more crash! Woohoo! I am HAPPILY MARKING THIS THREAD AS SOLLLLLVED!! Quote Link to comment 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.