php - Ajax not filling target DIV correctly after form submit -
i trying implement form plugin website have. supposed simple, refuses behave want.
it's simple php email form inside div, once correctly submitted, replaced success or failure message inside same div. js adds message on top of form.
i stunned because thought tackled serious trouble experience doing this, can't past simple bug. want response message inside div once contained form.
here's include php:
if (isset($_request['sender']) && isset($_request['message'])) {//if "sender" , "message" filled out, proceed //check if email address invalid $mailcheck = spamcheck($_request['sender']); if ($mailcheck==false) { echo "an error has occurred" . $_request['sender'] . " " . $_request['message']; } else {//send email $email = $_request['sender'] ; $subject = "this subject" ; $message = $_request['message'] ; mail("myself@gmail.com", $subject, $message, "from: $email" ); echo "your message sent"; } } function createform($formid, $formname) { echo "<div id=\"" . $formid . "\" class=\"ajaxform\" >" . php_eol; echo "<form name =\"" . $formname . "\" method=\"post\" onsubmit=\"ajaxupdate(\"" . $formid . "\",\"sandbox.php\"," . sender . "," . message . ");>" . php_eol; echo "<input name=\"sender\" type=\"text\" placeholder=\"email\" required><br>" . php_eol; echo "<br>" . php_eol; echo "<textarea rows=\"6\" cols=\"40\" name=\"message\" placeholder=\"message\" required></textarea><br>" . php_eol; echo "<input type=\"submit\" value=\"send\" >" . php_eol; echo "</form>" . php_eol; echo "</div>" . php_eol; } function spamcheck($field) { //filter_var() sanitizes e-mail //address using filter_sanitize_email $field=filter_var($field, filter_sanitize_email); //filter_var() validates e-mail //address using filter_validate_email if(filter_var($field, filter_validate_email)) { return true; } else { return false; } }
and ajaxupdate js:
function ajaxupdate(targetdiv, responsephp, param1, param2) { if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { document.getelementbyid(targetdiv).innerhtml = xmlhttp.responsetext; } } var strcontent = responsephp + "?sender=" + param1 + "&message=" + param2; xmlhttp.open("post", strcontent, true); xmlhttp.send(); }
i use including js , php, , spawning form using createform() function.
please help..
this line incorrect:
echo "<form name =\"" . $formname . "\" method=\"post\" onsubmit=\"ajaxupdate(\"" . $formid . "\",\"sandbox.php\"," . sender . "," . message . ");>" . php_eol;
you have use single quotes in function call:
echo "<form name =\"" . $formname . "\" method=\"post\" onsubmit=\"ajaxupdate('" . $formid . "','sandbox.php','" . sender . "','" . message . "');\">" . php_eol;
for preventing submission can return false
:
echo "<form name =\"" . $formname . "\" method=\"post\" onsubmit=\"ajaxupdate('" . $formid . "','sandbox.php','" . sender . "','" . message . "');return false;\">" . php_eol;
Comments
Post a Comment