ploppy Posted September 10, 2011 Share Posted September 10, 2011 I have a script that is working apart from I can only get json to send 1 result instead of multiple. The values are being correctly processed and inserted into the db, apart from the echo $json. For example, a user will input 3 input elements in a form. item1.item2,item3, Jquery will serialize and then send to the php page using $.ajax. I have created a foreach loop to handle the post, but I can only get 1 result in the echo $json. I would be grateful if someone check my code and show me where I am going wrong?. Many thanks <?php session_start(); $new = 1; $activity = 'Box Retrieval'; $mobile = 'Submitted from mobile'; $company = $_SESSION['idcode']; $authorised = mysql_real_escape_string($_POST['BRV_brtrvrb']); $service = mysql_real_escape_string($_POST['BRV-id-service-type']); $department = mysql_real_escape_string($_POST['BRV-brtrv-department']); $address = mysql_real_escape_string($_POST['BRV-brtrv-address']); $boxcount = mysql_real_escape_string($_POST['BRV-brtrv-slider']); foreach ($_POST['BRVbrtrv_boxnumber'] as $box) { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); header("Cache-Control: no-cache, must-revalidate" ); header("Pragma: no-cache" ); header("Content-type: application/json"); $json = ""; if(empty($service)) { $json .= "{\"ErrorService\": \"ERROR: You mest select a service level\"}"; } else if($department=="Choose Department") { $json .= "{\"ErrorService\": \"ERROR: You must select a department\"}"; } else if($address=="Choose Address") { $json .= "{\"ErrorService\": \"ERROR: You must select a retrieval address\"}"; } else if(empty($box)) { $json .= "{\"ErrorService\": \"ERROR: You must enter a box for retrieval\"}"; } else { $json .= "{\n"; $json .= "\"boxnumber\": \"".$box."\",\n"; $json .= "\"boxcount\": \"".$boxcount."\"\n"; $json .= "}\n"; $query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroydate`, `date`, `notes`, `new`) VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.strtoupper($box).'\', NULL, NOW(), \''.$mobile.'\', \''.$new.'\');'; mysql_query($query) or die('Error, query failed'); } } echo $json; ?> Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/ Share on other sites More sharing options...
Pikachu2000 Posted September 10, 2011 Share Posted September 10, 2011 You're re-initializing an empty $json variable on each iteration of the foreach() loop. Initialize the variable outside the loop. $json = ""; Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267737 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 pikachu thank you for that. However, when I move outside the loop, for example before the foreach, I get unexpected token error. Whats that mean? thanks Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267743 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 sounds like a simple syntax error. can you post the current code? Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267746 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 <?php session_start(); $new = 1; $activity = 'Box Retrieval'; $mobile = 'Submitted from mobile'; $company = $_SESSION['idcode']; $authorised = mysql_real_escape_string($_POST['BRV_brtrvrb']); $service = mysql_real_escape_string($_POST['BRV-id-service-type']); $department = mysql_real_escape_string($_POST['BRV-brtrv-department']); $address = mysql_real_escape_string($_POST['BRV-brtrv-address']); $boxcount = mysql_real_escape_string($_POST['BRV-brtrv-slider']); $json = ""; foreach ($_POST['BRVbrtrv_boxnumber'] as $box) { header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); header("Cache-Control: no-cache, must-revalidate" ); header("Pragma: no-cache" ); header("Content-type: application/json"); if(empty($service)) { $json .= "{\"ErrorService\": \"ERROR: You mest select a service level\"}"; } else if($department=="Choose Department") { $json .= "{\"ErrorService\": \"ERROR: You must select a department\"}"; } else if($address=="Choose Address") { $json .= "{\"ErrorService\": \"ERROR: You must select a retrieval address\"}"; } else if(empty($box)) { $json .= "{\"ErrorService\": \"ERROR: You must enter a box for retrieval\"}"; } else { $json .= "{\n"; $json .= "\"boxnumber\": \"".$box."\",\n"; $json .= "\"boxcount\": \"".$boxcount."\"\n"; $json .= "}\n"; /*$query = "UPDATE boxes SET status = 9 WHERE customer = '$company' AND department = '$department' AND custref = '$boxnumber'"; mysql_query($query) or die('Error, query failed'); $query = "UPDATE files SET boxstatus = 0 AND filestatus = 0 WHERE customer = '$company' AND boxref = '$boxnumber'"; mysql_query($query) or die('Error, query failed'); $query = 'INSERT INTO `act` (`service`, `activity`, `department`, `company`, `address`, `user`, `item`, `destroydate`, `date`, `notes`, `new`) VALUES (\''.$service.'\', \''.$activity.'\', \''.$department.'\', \''.$company.'\', \''.$address.'\', \''.$authorised.'\', \''.strtoupper($box).'\', NULL, NOW(), \''.$mobile.'\', \''.$new.'\');'; mysql_query($query) or die('Error, query failed');*/ } } echo $json; ?> And a portion of the relevant jquery. data: send, success: function (data) { if (data.ErrorService){ $('#brtv-result').addClass("result_msg").html(data.ErrorService).show(1000).delay(1000).fadeOut(1000); } else { //location.reload(true); $('#brtv-result').addClass("result_msg").html("You have successfully retrieved: "+data.boxnumber+" Boxes").show(1000).delay(4000).fadeOut(4000); } Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267753 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 can you paste the exact error message and line number? Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267754 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 There is no line number just an alert in chrome. syntaxError: unexpected token { . I shall try firebug and see if that offers more info. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267756 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 Ah its a javascript error. Your json seems malformed. can you post the json string that you get from this script? Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267760 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 problem. now I have moved $json = ""; out of the loop, there is no json tab in chrome. Should I put it back and try? Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267762 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 well no, that should be outside the loop. I'm not sure what tab your are talking about (I use firefox for development so I don't know about chrome) try using firebug to get more information on the problem Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267764 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 basically, the same tabs as firebug. This is the result in the json tab if i put it back: boxcount: "2" boxnumber: "iii" and i am inputting 2 values, should be iii, eee. I am developing using jquery mobile and this doesn't want to play ball in firefox. Dosen't show the layout so cannot enter any values. Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267765 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 FYI. If i take $json = ""; out of the loop in the, content tab after the alert error, the correct values are present. { "boxnumber": "rrr", "boxcount": "2" } { "boxnumber": "ttt", "boxcount": "2" } Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267767 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 I did a var_dump($_POST['BRVbrtrv_boxnumber']); and this is the result. array(2) { [0]=> string(3) "eee" [1]=> string(3) "ddd" } { "boxnumber": "eee", "boxcount": "2" } { "boxnumber": "ddd", "boxcount": "2" } Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267770 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 The easiest thing to do in my opinion, would be to scrap trying to create a json string by yourself, and instead create a php array and just use json_encode (this function translates a php objects like an array into a valid json notation.) so for example, instead of an empty string you would set $json to an empty array and add things to it like you would a normal associative array, so $json = array() for (...){ .... if(empty($service)) { $json['ErrorService'] = "ERROR: You must select a service level"; } .... ... }//end of for $json_string = json_encode($json);//now this is a valid json string of your php array Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267775 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 hi. I am getting error using that code: <b>Fatal error</b>: Call to undefined function json_encode() in line 66 which is: $json_string = json_encode($json);. I am using php 5.1.6. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267790 Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 Oh, json_encode is available in php version 5.2.0 and up Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267792 Share on other sites More sharing options...
ploppy Posted September 10, 2011 Author Share Posted September 10, 2011 is there an alternative with my version? thanks Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267793 Share on other sites More sharing options...
ploppy Posted September 11, 2011 Author Share Posted September 11, 2011 I am really stuck with this guys. Any further help please? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/246852-output-not-displaying-correct-value/#findComment-1267950 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.