Jump to content

json - script not working


Destramic

Recommended Posts

hey guys what im trying to do is to get the value from the query input field and when value changes it will load the json and just throw an alert...but its not...i know what im doing after the alert works its just im new to json and everything ive done looks right if anyone can help me to where i am going wrong please....thank you

 

<!DOCTYPE html>
<html>
<head>
    <script src="media/scripts/library/jquery.js" type="text/javascript"></script>
</head>
<body>
<form>

<?php 

$array = array("users" => array("username" => "Destramic", "username" => "Process"), 
                          array("email" => "destramic@hotmail.co.uk", "email" => "pro@whatever.com")); 

      ob_clean();
header('Content-Type: application/json');
json_encode($array);
                          
?>

<script>

$(document).ready(function()
{		
    $('#query').keyup(function () {

	var post_string = 'query=' + $(this).val();

	$.ajax({
		  type: 'POST',
		  data: post_string,
		  cache: false,
		  dataType: 'json',
		  url: 'json.php',
		  timeout: '2000',
	      error: function(data) { 
	       		console.log(data);
	         },
		    success: function(data) {

                     alert('hi');
                     // match field value with array above
		   }
      });
});
});

</script>

<input name="query" id="query" type="text" />
</form>

</body>
</html>

Link to comment
Share on other sites

You can't send output (your form) then send output again.  Not a php thing.... that is an html thing.

 

Besides that fact, json_encode takes the parameter and returns the json encoded version.  At very least you need to

 

$str = json_encode($array);
echo $str;

 

im already using json_encode...im just having a problem running the script on value change

Link to comment
Share on other sites

I'm responding to your code.  json_encode($array) does nothing if you don't get the return value.

 

PHP scripts run serverside -- they can't "run on value change".  If you want to react to clientside events you 'll need javascript.

 

Otherwise, you'll need to provide more code.

Link to comment
Share on other sites

oh i see now that its javascript i need thank you...can i still use that array for javascript?

 

Yes, but since it's static, the easiest thing would be to turn it into a javascript array that you emit at the top of the page in a javascript script block.  I also would use a different format of array.  If the array has no value being in php you might want to just code it up in javascript. I would also suggest a different structure for your array. Since it seems that what you have is "username" => "email", i'd structure your php array that way.  Here's what I'd suggest:

 

 

$users = array('destramic' => 'destramic@hotmail.co.uk', 'Process' => 'pro@whatever.com');
?>



    
     <br />
          var users = <?php echo json_encode($users); ?>;<br />
     


</pre>
<form>
<

 

FYI, one of the issues with using json_encode is that you need a recent version of php ( > 5.2) and you have to install a pecl addon.  You could just output the javascript array ready to use:

 

var users = { 'destramic' : 'destramic@hotmail.co.uk', 
                       'Process': 'pro@whatever.com'
                    };

 

The main value of having a php array first is if you will also use those values in the serverside portion of the script.  Hopefully it is apparent that you could use the php script to output it in javascript array format using a foreach() loop if that was the case.  Json is wonderful for many things but it is not essential here, so I just wanted to clarify that.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.