Postback in PHP with JQUERY/AJAX and JSON

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:20 AM

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

#17 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,030
  • Joined: 12-December 12

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 10:58 AM

Did you make the correction identified by laytonsdad (and Dormilich)?
Was This Post Helpful? 0
  • +
  • -

#18 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 - 11:01 AM

Yes i did.

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

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


Was This Post Helpful? 0
  • +
  • -

#19 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 11:13 AM

what do you see in your browser’s debugger as Response?
Was This Post Helpful? 0
  • +
  • -

#20 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 - 11:18 AM

No errors in the debugger.
Was This Post Helpful? 0
  • +
  • -

#21 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,030
  • Joined: 12-December 12

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 11:26 AM

Well you could study the following which works.

<!DOCTYPE html>
<html>
<head>
    <title>Untitled Document</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.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;
              var sel =  $('#sel');
			  for (var arrItem in msg) {
			     i = i + 1;
			     sel.append(new Option(arrItem, i, false, false));
			  }
					
			});
			  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>


You omitted a closing <script> tag and I would lose the die() in the php page - it is not needed.
Was This Post Helpful? 0
  • +
  • -

#22 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

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

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 17 May 2013 - 11:12 PM

View Postedjoks, on 17 May 2013 - 08:18 PM, said:

No errors in the debugger.

I meant the Network Console, not the Error Console.
Was This Post Helpful? 0
  • +
  • -

#23 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 18 May 2013 - 10:51 AM

Dunno how it worked for you but itz not working for me @Andrews, but at least now i don't get the "request failed: parsererror" but it still doesn't populate the select dropdown.

And i checked my code and i can't seem to understand what you meant by "You omitted a closing <script> tag" All my script tags each have a closing tag.
Was This Post Helpful? 0
  • +
  • -

#24 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 18 May 2013 - 11:06 AM

Scratch that, it works in ie but not in firefox, could someone please explain to me why and help me fix this problem to make it work across all browser platforms?

Weird, it started working in firefox after i tested it in ie, could someone please explain to me whats going on?
Was This Post Helpful? 0
  • +
  • -

#25 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3522
  • View blog
  • Posts: 12,030
  • Joined: 12-December 12

Re: Postback in PHP with JQUERY/AJAX and JSON

Posted 18 May 2013 - 11:12 AM

Ignore me about the script tag (it must have originated from my code).

My code works in Google Chrome and Firefox. So, as Dormilich suggested, check the Net (or Network) tab in your browser's console.

I assume that you are still looking at the Console for any errors, but you should also learn how to use this to debug, and step-through, your code.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2