www.webdeveloper.com
Results 1 to 4 of 4

Thread: Getting data from PHP database

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Getting data from PHP database

    I'm trying to import some questions and answers using Jquery-AJAX, but I get this error.
    Code:
    SyntaxError: JSON.parse: unexpected character
    [Break On This Error] 	
    
    ...ute(i),"string"==typeof r){try{r="true"===r?!0:"false"===r?!1:"null"===r?null:+r...
    
    jquery....min.js (line 3)

    And the variables I want parsed from JSON get undefined in my script.(answ1, answ2...)

    It's echoing out fine, saw the output with Firebug and validated with JSlint.

    questionDB.php
    Code:
    	$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'] . '"}';
    	
    ?>

  2. #2
    Join Date
    Jun 2013
    Posts
    3
    Forgot to include the javascript code. I'm using jQuery.

    Code:
    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);
    			});
    	}

  3. #3
    Join Date
    Aug 2008
    Location
    Phoenix, AZ
    Posts
    84
    You're putting the result into the theJSONObject, but you aren't referencing it when you are trying to parse it into the DOM. Try putting the object before your text declarations, like this:
    $("#questionTxt").text(theJSONObject.questionTxt);
    See if that does anything.
    Keep your nose to the grindstone, it sharpens your boogers.

  4. #4
    Join Date
    Jun 2013
    Posts
    3
    Yes, it was that. Another person offered a solution to my problem and I got it solved. I appreciate your efforts though.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles