javascript - AJAX Request with PHP -
i'm working on ajax first time , feel i'm close solving problem need help. have webpage file first below, has input field email address. when user submits, ajax dowork() function should called creates request , processes request. have fixed initial issue of request being created i'm positive correct object has been created based on browser. issue there's no response text being submitted , no email created. goal user enter email, introductory email sent address, when successful, response string should submitted letting user know have been added mailing list , submission has worked. help, appreciated.
<?php include('../includes/educateheader.php');?> <script type="text/javascript" charset="utf-8" src="ajax.js"></script> <div class="involve"> <h1>how involved in oec</h1> <span>want become more involved in operation:educate children , don't know how? share email address us, our facebook page, or check out blog out learn more how can join , children obtain education deserve</span><br></br> <form method="get"> email: <input type="email" name="email" id="email" required><br></br> <input type="submit" value="send" onclick="dowork()"> </form> </div> <div id="outputresponse"> </div> <?php include('../includes/educatefooter.php');?>
so here ajax.js file creates request , prints out data recieved email.php file
function gethttpobject() { if (window.activexobject) { return new activexobject("microsoft.xmlhttp"); } else if (window.xmlhttprequest) { return new xmlhttprequest(); } else { alert("your browser not support ajax."); return null; } } function setoutput() { if (httpobject.readystate == 4 && httpobject.status == 200) { document.getelementbyid('outputresponse').value = httpobject.responsetext; } } function dowork() { httpobject = gethttpobject(); if (httpobject != null) { httpobject.open("get", "email.php?email=" + document.getelementbyid('email').value, true); httpobject.send(null); httpobject.onreadystatechange = setoutput; } } var httpobject = null;
lastly here email.php script should accept ajax request , echo whether success has occurred or not.
<?php if (isset($_get['email'])) { $mail = trim($_get['email']); $subject = 'welcome!'; $message = 'thank joining operation:educate children email list. in future, send updates new opportunities become more involved in activities run here @ oec , make difference on children\'s futures. thank , best wishes!'; mail($mail, $subject, $message); echo 'success! thank interest in operaton:educate children. stay tuned updates!'; } ?>
first add return false;
@ end of function dowork
, change onclick="dowork()"
onclick="return dowork()"
then change below line
document.getelementbyid('outputresponse').value = httpobject.responsetext;
to
document.getelementbyid('outputresponse').innerhtml = httpobject.responsetext;
read question :) setting innerhtml vs. setting value javascript
Comments
Post a Comment