Undefined Variable in Ajax jQuery from database

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 3418 Views - Last Post: 17 June 2013 - 02:06 PM

#1 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 12:16 PM

I'm trying to get some strings from my database and have them as variables, but the variables I want keep coming up as undefined no matter what I do. answ1, answ2... are the ones undefined.

$(document).ready(function(){
	
	function getQuestions(){
		//In the php database, they are called answer1, answer2...
		//On this script, they are called answ1, answ2...
		//On the html, they are called ans1, ans2...
		
		var questNum = 0;
		//var theJSONObject;
		"use strict";
		
		$.ajax({
			type:	"POST",
			url:	"questionDB.php",
			data:	{pid:questNum},
			dataType:	"json",
			}).done(function(result){
				//theJSONObject = jQuery.parseJSON(result);
				$("#questionTxt").text(questionTxt);
				$("#ans1").text(answ1);
				$("#ans2").text(answ2);
				$("#ans3").text(answ3);
				$("#ans4").text(answ4);
				$("#hintTxt").text(hint);
			});
	}

	getQuestions();
	$("#hintTxt").fadeOut();
	$("#hintTrigger").click(function(){
		$(this).fadeOut(); //It seems to be skipping this animation.
		$("#hintTxt").text(hint);
		$("#hintTxt").fadeIn();
		$(this).remove();
	});

				
	console.log(questionTxt); // I get <div id="questionTxt"> in the console.
	console.log(answ1);
});



<?php
	/* I don't think I need this code
	$_POST['questionTxt'];
	$_POST['answer1'];
	$_POST['answer2'];
	$_POST['answer3'];
	$_POST['answer4'];
	$_POST['hint'];
	*/

	    $questNum = $_POST['pid'];

	
	//echo '$questNum = ' . $questNum;
	
    //connects to our database
    $con = mysql_connect("localhost","root","");
	if (!$con)
	{
		die('error connecting to database server' . mysql_error());
	}
	
	// Check if there's a database named databasehelloz
	if (!mysql_select_db("databasehelloz", $con))
	{
		die('error connection to database' . mysql_error());
	}
	
	// TODO 
	$result = mysql_query("SELECT * FROM questionTable WHERE pid = ".$questNum."");
	// TODO
   	$row = mysql_fetch_array($result);
		//echo $row['questionTxt'] . $row['answer1'] . $row['answer2'] . $row['answer3'] . $row['answer4'] . $row['hint'];
		echo '{"questionTxt" : "' . $row['questionTxt'] .
		'", "answ1" : "' . $row['answer1'] .
		'", "answ2" : "' . $row['answer2'] .
		'", "answ3" : "' . $row['answer3'] .
		'", "answ4" : "' . $row['answer4'] .
		'", "hint" : "' . $row['hint'] . '"}';
		
		/*echo '"questionTxt" : ', json_encode($row['questionTxt']), '/n';
		echo '"answ1" : ', json_encode($row['answer1']), '/n';
		echo '"answ2" : ', json_encode($row['answer2']), '/n';
		echo '"answ3" : ', json_encode($row['answer3']), '/n';
		echo '"answ4" : ', json_encode($row['answer4']), '/n';*/
	
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Undefined Variable in Ajax jQuery from database

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4246
  • View blog
  • Posts: 13,461
  • Joined: 08-June 10

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 12:22 PM

Quote

but the variables I want keep coming up as undefined no matter what I do

thatís because they are never defined anywhere.

for clearing the matter, print out your AJAX Response (or look it up in the browserís dev tools) to see what data you really get.
Was This Post Helpful? 0
  • +
  • -

#3 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 12:47 PM

This is what I sent.

pid 0

This is what php is echoing out.

{"questionTxt" : "What is 2 + 2?", "answ1" : "1", "answ2" : "2", "answ3" : "3", "answ4" : "4", "hint" : "What is 2?"}

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4246
  • View blog
  • Posts: 13,461
  • Joined: 08-June 10

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 12:54 PM

View PostEmwat Oon, on 17 June 2013 - 09:47 PM, said:

This is what php is echoing out.

{"questionTxt" : "What is 2 + 2?", "answ1" : "1", "answ2" : "2", "answ3" : "3", "answ4" : "4", "hint" : "What is 2?"}

And is this how variables are declared in JS?
Was This Post Helpful? 0
  • +
  • -

#5 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 12:59 PM

This is how JSON works. The php echoes out Json so my script can parse it and use it in the html.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4246
  • View blog
  • Posts: 13,461
  • Joined: 08-June 10

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:00 PM

but I didnít ask about JSON, I asked about Javascript.
Was This Post Helpful? 0
  • +
  • -

#7 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:10 PM

Javascript would do
var answ1 = '';

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4246
  • View blog
  • Posts: 13,461
  • Joined: 08-June 10

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:11 PM

correct. and does your JSON response do anything like that?
Was This Post Helpful? 0
  • +
  • -

#9 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:15 PM

The JSON response applies 1 to answ1.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4246
  • View blog
  • Posts: 13,461
  • Joined: 08-June 10

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:16 PM

what does JSON (the abbreviation) stand for?

This post has been edited by Dormilich: 17 June 2013 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#11 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:19 PM

Javascript Object Notation.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4246
  • View blog
  • Posts: 13,461
  • Joined: 08-June 10

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:21 PM

correct. so JSON defines an object, not a variable, right?
Was This Post Helpful? 0
  • +
  • -

#13 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:22 PM

Right...
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4246
  • View blog
  • Posts: 13,461
  • Joined: 08-June 10

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:24 PM

so what is "answ1" and where do you find it?
Was This Post Helpful? 0
  • +
  • -

#15 Emwat Oon   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 07-January 12

Re: Undefined Variable in Ajax jQuery from database

Posted 17 June 2013 - 01:30 PM

I'm not getting your point. answ1 is.. not declared anywhere but appears in the Json echo.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2