bigtimslim Posted June 4, 2008 Share Posted June 4, 2008 My page displays a list of jobs, each with a button to save that particular job to a mysql db using ajax. Before the job can be saved I check with js whether the user is logged in(based on the responseText). It seems to me like this should work, but it doesn't. My problem is in the uncommented xstateChanged function. I have a similar function (without the condition) that works fine commented out. I've hacked this together from examples I've seem, so I might be doing this in a stupid unconventional way. Thanks. savejob.js var xxmlHttp function saveJob(str) { xxmlHttp=xGetXmlHttpObject() if (xxmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="includes/savejob.php" url=url+"?q="+str url=url+"&sid="+Math.random() xxmlHttp.onreadystatechange=xstateChanged xxmlHttp.open("GET",url,true) xxmlHttp.send(null) } /* THIS FUNCTION WORKS function xstateChanged() { if (xxmlHttp.readyState==4 || xxmlHttp.readyState=="complete") { document.getElementById(xxmlHttp.responseText).innerHTML="<span style=\"background-color:#ECE9AA;\">  Saved  </span>"; } } */ function xstateChanged() { if (xxmlHttp.readyState==4 || xxmlHttp.readyState=="complete") { if (xxmlHttp.responseText=="jobnouser") { alert("You must log in to save a job."); } else { document.getElementById(xxmlHttp.responseText).innerHTML="<span style=\"background-color:#ECE9AA;\">  Saved  </span>"; } } function xGetXmlHttpObject() { var xxmlHttp=null; try { // Firefox, Opera 8.0+, Safari xxmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xxmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xxmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xxmlHttp; } savejob.php <?php session_start(); $jobs = unserialize($_SESSION['array']); //require_once ('config.inc.php'); //require_once (MYSQL); //get the q parameter from URL $q = $_GET["q"]; if (isset($_SESSION['user_id'])) { /* $saveid = $q; $uid = mysqli_real_escape_string ($dbc, $_SESSION['user_id']); $jdate = mysqli_real_escape_string ($dbc, $jobs[$saveid]['date']); $jlink = mysqli_real_escape_string ($dbc, $jobs[$saveid]['link']); $jtitle = mysqli_real_escape_string ($dbc, $jobs[$saveid]['title']); $jdesc = mysqli_real_escape_string ($dbc, $jobs[$saveid]['desc']); $jsite = mysqli_real_escape_string ($dbc, $jobs[$saveid]['site']); //set up and run query $qy = "INSERT INTO job (user_id, job_date, job_link, job_title, job_desc, job_site) VALUES ('$uid', '$jdate', '$jlink', '$jtitle', '$jdesc', '$jsite' )"; $r = mysqli_query ($dbc, $qy); or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); */ } else { $q = 'nouser'; } // mysqli_close($dbc); //output the response echo 'job' . $q; ?> Quote Link to comment Share on other sites More sharing options...
bigtimslim Posted June 5, 2008 Author Share Posted June 5, 2008 Still not sure what was wrong, but this worked: ??? function xstateChanged() { if (xxmlHttp.readyState==4 || xxmlHttp.readyState=="complete") { if (xxmlHttp.responseText=="jobnouser") { alert("You must log in to save a job."); return; } document.getElementById(xxmlHttp.responseText).innerHTML="<span style=\"background-color:#ECE9AA;\">  Saved  </span>"; } } Quote Link to comment Share on other sites More sharing options...
haku Posted June 5, 2008 Share Posted June 5, 2008 I'm not sure why it didn't work either, because you didn't even tell us what wasn't working! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.