Destramic Posted October 4, 2011 Share Posted October 4, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/248439-json-script-not-working/ Share on other sites More sharing options...
gizmola Posted October 4, 2011 Share Posted October 4, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/248439-json-script-not-working/#findComment-1275782 Share on other sites More sharing options...
Destramic Posted October 4, 2011 Author Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248439-json-script-not-working/#findComment-1275788 Share on other sites More sharing options...
gizmola Posted October 4, 2011 Share Posted October 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248439-json-script-not-working/#findComment-1275812 Share on other sites More sharing options...
Destramic Posted October 5, 2011 Author Share Posted October 5, 2011 oh i see now that its javascript i need thank you...can i still use that array for javascript? Quote Link to comment https://forums.phpfreaks.com/topic/248439-json-script-not-working/#findComment-1276118 Share on other sites More sharing options...
gizmola Posted October 5, 2011 Share Posted October 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248439-json-script-not-working/#findComment-1276192 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.