Postback in PHP with JQUERY/AJAX and JSON

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2554 Views - Last Post: 18 May 2013 - 11:12 AM Rate Topic: -----

#1 edjoks  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 17-May 13

Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 03:28 AM

Hi i'm new to PHP and just learnt how to do a postback with JQUERY/AJAX, now what i want to do is retrieve an array from PHP through AJAX and populate a dropdown with the values from the array dynamically.

Here is the php file(postback.php):
<?php
  if (isset($_POST["add"]))
  {
	$arr = array("1", "2", "3");
	foreach ($arr as $val){
	    echo $val;
	}
	die();
  }
?>


And here is the html file(postback.html):
<html>
  <head>
    <title>Untitled Document</title>
    <script src="jquery-1.9.1.min.js" ></script>
    <script type="text/javascript">
	$(document).ready(function() {
		$("#btn").click(function() {
			var request = $.ajax({
			url: "postback.php",
			type: "POST",
			data: {
				add : "ok"
						
			},
			dataType: "json"
			});
			request.done(function(msg) {
			  console.log(msg);
			  var jsArray = JSON.parse(msg);
			  var i = 0;
			  for (var arrItem in msg)
			  {
			     i = i + 1;
			     var options =  $('#sel').attr('options');
			     options[options.length] = newOption(arrItem, i, true, true);
			  }
					
			});
			  request.fail(function(jqXHR, textStatus) {
			    alert( "Request failed: " + textStatus );
			  });
			});
		});
</script>
</head>

  <body>
    <input type="button" id="btn" value="add" name="add">
    <select name="sel" id="sel">
      <option></option>
    </select>
  </body>
</html>




I'm i in the right direction or am i doing everything completely wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Postback in PHP with JQUERY/AJAX and JSON

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 03:52 AM

youíre not doing everything wrong.
- your AJAX request expects a JSON string, but you feed it only a single number ("123"). there is this neat little function called json_encode() you should make use of.
- the for...in loop is intended for objects, for arrays you can use Arrayís forEach() method. (where you also have access to the index number)
- I doubt you meant newOption() as function rather than new Option() (itís a big difference!)
- do <select> elements have an options attribute in jQuery?
- the options property of a <select> element is read-only.
- check out the Error Console
Was This Post Helpful? 0
  • +
  • -

#3 edjoks  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 17-May 13

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 04:58 AM

I keep on getting "parser error", plus i read somewhere that the 'for each' had been deprecated, dunno how true that is though and as for the adding the "options" attribute to the "<select>" i got it from this link.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 05:03 AM

parser error in JS or in PHP?


View Postedjoks, on 17 May 2013 - 01:58 PM, said:

i read somewhere that the 'for each' had been deprecated

the for each() construct has indeed been deprecated, but I was talking about Array.forEach() (also note that the former has a space in it, while the latter has not)


View Postedjoks, on 17 May 2013 - 01:58 PM, said:

as for the adding the "options" attribute to the "<select>" i got it from this link.

if it works, then this is OK.
Was This Post Helpful? 0
  • +
  • -

#5 edjoks  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 17-May 13

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 05:17 AM

Dunno if it works, can you give me your suggestions on how to make my code work???
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 06:51 AM

suggestion #1, read the messages in the error console and resolve the issues.

suggestion #2, I have given you a couple of links, which, upon reading, should teach you how to solve certain of your current problems.
Was This Post Helpful? 0
  • +
  • -

#7 edjoks  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 17-May 13

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 06:53 AM

There is no error in the console, i only get a "request failed: parsererror".
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 06:54 AM

thatís because you fail to deliver a valid JSON response from your server (as I have already said in post #2).
Was This Post Helpful? 0
  • +
  • -

#9 edjoks  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 17-May 13

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 06:58 AM

But the links you posted are with handling arrays on the client side and yet you say "thatís because you fail to deliver a valid JSON response from your server (as I have already said in post #2). "

I'm confused.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 07:03 AM

what about this one?

View PostDormilich, on 17 May 2013 - 12:52 PM, said:

- your AJAX request expects a JSON string, but you feed it only a single number ("123"). there is this neat little function called json_encode() you should make use of.

Was This Post Helpful? 0
  • +
  • -

#11 edjoks  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 17-May 13

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 09:51 AM

Okay i changed it to:
<?php
	if (isset($_POST["add"]))
	{

		$result = array("apples", "peaches", "pie");

		echo json_encode($results);
		die();
	}
?>



But it still says "request failed : parsererror". Or do i need to import something for "json_encode" to work?
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 09:54 AM

where did you define the variable $results?
Was This Post Helpful? 0
  • +
  • -

#13 edjoks  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 17-May 13

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 10:07 AM

Just updated it:
<?php
	if (isset($_POST["add"]))
	{
                $result = array();
		$result = array("apples" => 1, "peaches" => 2, "pie" => 3);

		echo json_encode($results);
		die();
	}
?>


Still get "request failed : parsererror".
Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,225
  • Joined: 08-June 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 10:08 AM

same question as above.
Was This Post Helpful? 0
  • +
  • -

#15 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,864
  • Joined: 30-April 10

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 10:13 AM

take the "s" off of "results", you defined "result", not "results"
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2