Jump to content

Output not displaying correct value


ploppy

Recommended Posts

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;

?>

Link to comment
Share on other sites

<?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);
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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


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.