Jump to content

[SOLVED] javascript condition just won't work


bigtimslim

Recommended Posts

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;\">&nbsp Saved &nbsp</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;\">&nbsp Saved &nbsp</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;
?>

 

 

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;\">&nbsp Saved &nbsp</span>";
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.