php - Why is the error callback function getting called? -
i'm trying figure out why i'm getting 500 (internal server error) , error
function called. there glaring error in code
// $namemap map of input names table cells in email // e.g. <input name="email" value="somedude@gmail.com"/> gets made row <tr><td><b>email address:<b></td><td>somedude@gmail.com</td></tr> // mapping 'email' => 'email address' in array $namemap = array( 'name' => 'name', 'email' => 'email address', 'phone' => 'phone number', 'comments' => 'comments'); // https://css-tricks.com/sending-nice-html-email-with-php/ $headers = "mime-version: 1.0\r\ncontent-type: text/html; charset=iso-8859-1\r\n"; function contact ( ) { global $namemap, $headers; $info = array('validname' => (strlen(trim($_post['name'])) > 0 ? true : false), // name long not empty 'validemail' => (preg_match("!^[a-za-z0-9_.+-]+@[a-za-z0-9-]+\.[a-za-z0-9-.]+$!",trim($_post['email'])) == 1 ? true : false), // http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address 'validphonenumber' => (preg_match("!^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$!",trim($_post['phone'])) == 1 ? true : false) // http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation ); // see http://stackoverflow.com/questions/482377/php-regex-delimiter-whats-the-point why "!" around regexes if (!$info['validname'] || !$info['validemail'] || !$info['validphonenumber']) // if invalid name, email or phone number { $info['sentemail'] = false; } else // inputs contained valid values { // contstruct email body $emailmsg = '<html><body><h3>someone submitted contact form ...</h3><table>'; foreach ( $_post $key => $value ) if (array_key_exists($key, $namemap)) $emailmsg .= '<tr><td><b>' . $namemap[$key] . ':</b></td><td>' . $value . '</td></tr>'; $emailmsg .= '</table></body></html>'; // attempt send email , set 'sentemail' key accordingly $info['sentemail'] = email("myemail@gmail.com","a comment submitted",$emailmsg,$headers) ? true : false; } echo json_encode($info); die(); }
which called
$.ajax({ url: ajaxurl, type: 'post', data: formdata, async: false, success: function (info) { info = json.parse(info); console.log(info); // test if (info.sentemail) // means info={validname:true,validemail:true,validphonenumber:true,sentemail:true} { $('.contact-form h1').text('thank you!').css('text-align','center'); $('.contact-form input').css('visibility', 'hidden'); $('.contact-form .contact-submit-btn').css('visibility', 'hidden'); } else // means info has false value validname, validemail or validphonenumber { if (!info.validname) { $('.contact-form .error-message').text('you must enter name'); } else if (!info.validemail) { $('.contact-form .error-message').text('you must enter valid email address'); } else if (!info.validphonenumber) { $('.contact-form .error-message').text('you must enter phone number in form xxx-xxx-xxxx'); } } }, error: function () { $('.contact-form .error-message').text('oops! unexpected error occurred.'); }, cache: false, contenttype: false, processdata: false });
Comments
Post a Comment