6 Replies - 583 Views - Last Post: 29 August 2011 - 08:29 AM Rate Topic: -----

#1 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

PHP and JSON

Posted 28 August 2011 - 07:02 PM

I'm having a little bit of trouble using these two things together. I think I'm close, but not getting expected results.

Here's my Javascript .ajax call:
$.ajax({
				type: "GET",
				url: "ajaxEditResume.php",
				data: {"status" : "personal"},
				success: function(html){
					$("." + idValue).html("<input type='text' maxlength='50' placeholder='Street Address' name='street' value='" + html.street + "id='street'>");
				}
			});



And here's the php that gets called in that:
session_start();
include_once("common/includes/util/appleFunctions.php");
include_once("common/includes/util/resumeValues.php");

//echo $_GET['status'];
if($_GET['status'] == "personal")
{
	$chars = array('"', "'");
	$replace = array('&quot;', ''');
	$objective = str_replace($chars, $replace, $objective);
	$array = array('street' => str_replace($chars, $replace, $street), 
					'city' => str_replace($chars, $replace, $city), 
					'state' => str_replace($chars, $replace, $state), 
					'zip' => str_replace($chars, $replace, $zip), 
					'phone' => str_replace($chars, $replace, $phone));
	echo json_encode($array);
}



So what does this do? I'll walk through how I understand it so that maybe you'll pick up if I'm missing something.

First the .ajax call is made, and it uses ajaxEditResume.php and sends to it with a GET request. So in ajaxEditResume.php the $_GET variable $_GET['status'] is set as "personal".

We get in the PHP file, I get some values that are set in resumeValues.php (included at the top), do some formatting, and put them in an associative array. I encode this array into a json object and echo it thus sending it back to the .ajax call.

In this I change the html into a textbox. The value of this textbox SHOULD be whatever the street variable was from the php call. However, on my site in the textbox I see this:
undefinedid=

This is not right.

If however instead of doing the .html call in the success function and do this instead:
alert(html);



I see this on my site:
{"street":"931 Sheely St", "city":.......}
It's all correct. So the php encoding is working. All the correct data is getting sent back to the Javascript call.

So it makes me think this is wrong:
html.street


I'm doing something wrong there?

Thanks for the help! It's appreciated.

This post has been edited by eZACKe: 28 August 2011 - 07:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP and JSON

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: PHP and JSON

Posted 28 August 2011 - 07:29 PM

Try html[0].street. Remember you're encoding an array.
Was This Post Helpful? 0
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: PHP and JSON

Posted 28 August 2011 - 07:37 PM

Thanks for the input, figured out my problem though. If I include the dataType in the .ajax call, it works.

Like this:
$.ajax({
				type: "GET",
				url: "ajaxEditResume.php",
				dataType: "json",
				data: {"status" : "personal"},
				success: function(html){
					$("." + idValue).html("<input type='text' maxlength='50' placeholder='Street Address' name='street' value='" + html.street + "' id='street'> ");


Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: PHP and JSON

Posted 29 August 2011 - 05:39 AM

jQuery's a pain in the ass along with overkill for this crap.

You can do the whole request and simply pass over the JSON encoded string as a POST/GET variable along with a callback in plain JS.
Was This Post Helpful? 0
  • +
  • -

#5 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: PHP and JSON

Posted 29 August 2011 - 07:00 AM

View PostRudiVisser, on 29 August 2011 - 08:39 AM, said:

jQuery's a pain in the ass along with overkill for this crap.

You can do the whole request and simply pass over the JSON encoded string as a POST/GET variable along with a callback in plain JS.


I've never really done any work with this kind of stuff.

Do you mind writing up a quick example for me to look at?

Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: PHP and JSON

Posted 29 August 2011 - 07:54 AM

$.getJSON('ajaxEditResume.php', array('status' => 'personal'), function(resp, status) {
   alert(resp.street);
});


I think that jQuery would work, and that's pretty damn easy, unless you already have a written-up library that does all the browser-specific XHR initialization and setup, as well as JSON encoding.
Was This Post Helpful? 1
  • +
  • -

#7 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: PHP and JSON

Posted 29 August 2011 - 08:29 AM

View PostJackOfAllTrades, on 29 August 2011 - 10:54 AM, said:

$.getJSON('ajaxEditResume.php', array('status' => 'personal'), function(resp, status) {
   alert(resp.street);
});


I think that jQuery would work, and that's pretty damn easy, unless you already have a written-up library that does all the browser-specific XHR initialization and setup, as well as JSON encoding.


Okay that definitely looks nicer than what I'm doing.

Is there any added benefits using that method over my .ajax call though? Yes of course less lines of code is always better, but anything else? Just curious.

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

Page 1 of 1