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;
?>

 

 

Link to comment
Share on other sites

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>";
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.