javascript - unable to redirect after successfully inserted data into mysql via Ajax -
i use ajax, php insert data mysql database. after finished executing queries, doesn't anything. mean, when click submit button in form, user don't see happening though set ajax function alert data. in backend data inserted database.
so, i'm not sure how notify user of successful form submission. please? thank in advance.
i referred here, doesn't help:page redirect successful ajax request
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script> var xmlhttp = false; try { xmlhttp = new activexobject(msxml2.xmlhttp); //alert("javascript version greater 5!"); } catch(e) { try { xmlhttp = new activexobject(microsoft.xmlhttp); // alert("you're using ie!"); } catch(e) { xmlhttp = new xmlhttprequest(); //alert("non ie!"); } }
//ajax function other element before submitting form
function sendtobox(param,param2) { xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { if (this.responsetext !== null) { var ajaxelm = document.getelementbyid('boxa'); ajaxelm.innerhtml = this.responsetext + ajaxelm.innerhtml; // append in front } } } xmlhttp.open("get","getsubjects.php?q="+param+"&r="+param2,true); xmlhttp.send(); }
//ajax after submit button clicked
$("document").ready(function(){ $(".form").submit(function(){ var data = { "action": "test" }; data = $(this).serialize() + "&" + $.param(data); $.ajax({ type: "post", datatype: "json", url: "response.php", //relative or absolute path response.php file data: data, success: function(data) { $(".the-return").html("<br />json: " + data["json"] ); //alert("form submitted successfully.\nreturned json: " + data["json"]); window.location='tutor-profile.php' //header('location:tutor-profile.php'); } }); return false; }); }); </script>
response.php
if (is_ajax()) { if (isset($_post["action"]) && !empty($_post["action"])) { //checks if action value exists $action = $_post["action"]; switch($action) { //switch case value of action case "test": test_function(); break; } } } //function check if request ajax request function is_ajax() { return isset($_server['http_x_requested_with']) && strtolower($_server['http_x_requested_with']) == 'xmlhttprequest'; } function test_function(){ include('inc/config.php'); $return = $_post; //$return ='{"slider1":"150","level":{"upper secondary":"2"},"sub":{"8":""},"rate2":{"7":"89","8":"150","9":"150","10":"150","11":"150","12":"150","13":"150","14":"150"},"a_end":"on","e_week":"on","e_end":"on","name":"ting1","phone":"098-098-0980","email":"ting1@gmail.com","postcode":"56547","gender":"on","password":"nk","action":"test"}'; $return["json"] = json_encode($return); // json_encode($return); // //below code store in database $data = json_decode($return["json"], true); //var_dump($data); // dump data of array >>>>>>insert queries go here>>>>>>>>>>>>>>>> echo"form submitted "; }
the code redirect page in javascript
window.location.href ='tutor-profile.php'
anyway not understand previous roles , xmlhttprequest statements, ajax, simplify using last function $.ajax of jquery.
the other correction you're saying server response of type "json" give string through miss. can change this:
exit( json_encode( array('response'=>'put text here') ) );
and in js instead of
$(".the-return").html("<br />json: " + data["json"] );
place this:
$(".the-return").html("<br />json: " + data.response );
Comments
Post a Comment