Featured post
AJAX, response headers are not sent by PHP script! -
i'm banging head hours now. have select option updates database using ajax (at least it's trying to!). happens while running php script directly params required database gets updated not when running indirectly through ajax, instead 3 times alert("there problem in returned data:\n");
, gets updated. javascript on <head>
, not in external file. here goes:
javascript:
function updatehub(){ 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("hubinfo").innerhtml=xmlhttp.responsetext; }else{ alert("there problem in returned data:\n"); } } var prefhub = document.getelementbyid("prefhub"); var hubid = prefhub.options[prefhub.selectedindex].value; xmlhttp.open("get","updatehub.php?hubid="+hubid,true); xmlhttp.send(); }
updatehub.php:
session_start(); include '../../../common/config.php'; $hubid = ''; if(isset($_post['hubid'])){ $hubid = strip_tags(mysql_real_escape_string(trim($_post['hubid']))); }elseif(isset($_get['hubid'])){ $hubid = strip_tags(mysql_real_escape_string(trim($_get['hubid']))); } mysql_query("update prefs set hubid='$hubid' userid = '".$_session['userid']."'") or die(mysql_error()); if(mysql_affected_rows()){ echo "updated"; }else{ echo 'error'; } return $hubid;
and html:
<form action="" method="post" onsubmit="updatehub();"> <select name="prefhub" id="prefhub"> <option value="43">opt1</option> <option value="64">opt2</option> <option value="30">opt2</option> </select> <input type="submit" name="update" value="update hub"/> </form> <div id="hubinfo"></div>
instead 3 times alert("there problem in returned data:\n");
if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("hubinfo").innerhtml=xmlhttp.responsetext; } else{ alert("there problem in returned data:\n"); }
this means see alert. since other xmlhttp.readystate values else {} block executed.
the xmlhttprequest object can in several states. readystate attribute must return current state, must 1 of following values:
unsent (numeric value 0)
the object has been constructed.
opened (numeric value 1)
the open() method has been invoked. during state request headers can set using setrequestheader() , request can made using send() method.
headers_received (numeric value 2)
all redirects (if any) have been followed , http headers of final response have been received. several response members of object available.
loading (numeric value 3)
the response entity body being received.
done (numeric value 4)
- Get link
- X
- Other Apps
Comments
Post a Comment