javascript - Cannot get a simple XMLHttpRequest to work -
i'm struggling work part of larger exercise.
it's pretty simple - user fills in form sent using xmlhttprequest processing page. should return response below form.
i had working, 1 field wouldn't show... , nothing. cache problem or problem code.
here's form:
<div id="minicontact"> <label for="yourname">your name</label> <input type="text" name="yourname" id="yourname"><br> <label for="phone">your phone</label> <input type="text" name="phone" id="phone"><br> <input type="text" name="reqd" id="reqd"><br> <label for="email">your email</label> <input type="email" name="email" id="email"><br> <label for="type">your vehicle type</label> <input type="text" name="type" id="type"> <input name="mybtn" type="submit" value="submit data" onclick="ajax_post();"> <br><br> <div id="status"></div>
javascript:
<script> function ajax_post(){ // create our xmlhttprequest object var hr = new xmlhttprequest(); // create variables need send our php file var url = "my_parse_file.php"; var fn = document.getelementbyid("first_name").value; var yourname = document.getelementbyid("yourname").value; var phone = document.getelementbyid("phone").value; var reg = document.getelementbyid("reg").value; var srv = document.getelementbyid("reqd").value; var email = document.getelementbyid("email").value; var type = document.getelementbyid("type").value; var vars = "yourname="+yourname+"&phone="+phone+"®="+reg+"srv="+service+"email="+email+"&type="+type; hr.open("post", url, true); // set content type header information sending url encoded variables in request hr.setrequestheader("content-type", "application/x-www-form-urlencoded"); // access onreadystatechange event xmlhttprequest object hr.onreadystatechange = function() { if(hr.readystate == 4 && hr.status == 200) { var return_data = hr.responsetext; document.getelementbyid("status").innerhtml = return_data; } } // send data php now... , wait response update status div hr.send(vars); // execute request document.getelementbyid("status").innerhtml = "processing..."; } </script>
...and php file (my_parse_file.php):
<?php echo 'thank '. $_post['yourname'] . ' ' . $_post['service'] . ', says php file'; $user_name = $_post['yourname']; $reg = $_post['reg']; $email = $_post['email']; $srv = $_post['srv']; $phone_number = $_post['phone']; $vehicle = $_post['type']; ?>
Comments
Post a Comment