peter-cooper
asked on
jQuery submitHandler: function not displaying data
I am using jQuery to input data to a php file and return the results vis json. However, the json is appearing in firebug, but the variable 'messageOutput' is not displaying results on the form. If I replace 'messageOutput' with 'msg.box' then it prints fine. If I enter more than 1 item then it errors with: 'object object'. Can someone point out where my error is as I have been struggling with this for ages. If you need to see any further code, please ask. many thanks.
jQuery code:
boxesadd.php
jQuery code:
submitHandler: function() {
if ($("#BA_boxform").valid() === true) {
var data = $("#BA_boxform").serialize();
$.post('/domain/admin/requests/boxes/boxesadd.php', data, function(msg) {
var messageOutput = '';
for (var i = 0; i<msg.length; i++){
messageOutput += msg[i].box+' ';
}
$("#BA_addbox").html("You have entered box: " + "<b>" + messageOutput + "</b><br /> You may now close this window.");
$("#BA_boxform").get(0).reset();
}, 'json');
} else
{
return;
}
},
success: function(msg) {
//$("#BA_addbox").html("You have entered a box");
//$("#BA_boxform").get(0).reset();
}
boxesadd.php
<?php
$dept = mysql_real_escape_string($_POST['customerdept']);
$company = mysql_real_escape_string($_POST['BA_customer']);
$address = mysql_real_escape_string($_POST['customeraddress']);
$service = mysql_real_escape_string($_POST['BA_service']);
$box = mysql_real_escape_string($_POST['BA_box']);
$destroydate = mysql_real_escape_string($_POST['BA_destdate']);
$authorised = mysql_real_escape_string($_POST['BA_authorised']);
$submit = mysql_real_escape_string($_POST['submit']);
$boxerrortext = 'You must enter a box for intake';
$array = split('[,]', $_POST['BA_box']);
if (isset($_POST['submit'])) {
foreach ($array as $box) {
if (empty($box)) {
$error = array('boxerrortext'=>$boxerrortext);
$output = json_encode($error);
echo $output;
}
else
{
$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
$result=json_encode($form);
echo $result;
?>
where's the end bracket of your PHP if-else inside the loop line 27
where's the end bracket of your PHP loop line 16
where's the end bracket of your PHP loop line 16
ASKER
@leakim Sorry my bad. It is there just for some reason didn't post it. Here is revised code. Thanks
if (isset($_POST['submit'])) {
foreach ($array as $box) {
if (empty($box)) {
$error = array('boxerrortext'=>$boxerrortext);
$output = json_encode($error);
echo $output;
}
else
{
$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
$result=json_encode($form);
echo $result;
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@ChrisStanyon
Please see attached revised code for php script. I also attach output that appears in the json tab in firebug. The idea behind , as delimiter is if a client inputs more than 1 item. For example: demobox1000,demobox1001. Thanks
Please see attached revised code for php script. I also attach output that appears in the json tab in firebug. The idea behind , as delimiter is if a client inputs more than 1 item. For example: demobox1000,demobox1001. Thanks
dept
"DEMO"
company
"DEMO"
address
"28 Some Road London N8 6TY"
service
"Standard"
box
"demobox1001"
destroydate
"27/11/2013"
authorised
"Admin"
submit
"Add Box"
ASKER
@leakim
I have replaced code and for 1 item it displays fine, but if 1 enter more than 1 nothing is displayed in BA_boxadd. I have included the relevant code section that I changed. Thanks
I have replaced code and for 1 item it displays fine, but if 1 enter more than 1 nothing is displayed in BA_boxadd. I have included the relevant code section that I changed. Thanks
$array = split('[,]', $_POST['BA_box']);
if (isset($_POST['submit'])) {
$form = array();
foreach ($array as $box) {
if (empty($box)) {
$error = array('boxerrortext'=>$boxerrortext);
$output = json_encode($error);
echo $output;
}
else
{
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
$result=json_encode($form);
echo $result;
}
}
}
put :
$result=json_encode($form);
echo $result;
outside the loop !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@leakim
Displays 1 item but not more than 1. If i enter 2 items then the result is blank. Thanks
Displays 1 item but not more than 1. If i enter 2 items then the result is blank. Thanks
ASKER
@leakim
Sorry, my bad. Didn't have it outside the loop. It now displays multiple entries. Thanks
Sorry, my bad. Didn't have it outside the loop. It now displays multiple entries. Thanks
ASKER
Would it be ok if I split the points as both solutions work. Thanks
Yup :)
ASKER
There is one more thing. After I have returned results, would my sql query be placed after the echo $result. Here is my final code. Also, the error block is redundant because I am using jquery validation. Thanks
$array = split('[,]', $_POST['BA_box']);
if (isset($_POST['submit'])) {
$form = array();
foreach ($array as $box) {
// if (empty($box)) {
// $error = array('boxerrortext'=>$boxerrortext);
// $output = json_encode($error);
// echo $output;
// }
// else
// {
$form[] = array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destroydate,
'authorised'=>$authorised,
'submit'=>$submit);
// }
}
$result=json_encode($form);
echo $result;
}
No. The database stuff should go before echoing results, otherwise how to you respond to errors from your SQL.
FYI - you're still using split() - it's deprecated - switch to explode() instead. Also, it looks like you're trying to use the mysql_* library for your database stuff. That's also deprecated! Switch to mysqli or PDO
FYI - you're still using split() - it's deprecated - switch to explode() instead. Also, it looks like you're trying to use the mysql_* library for your database stuff. That's also deprecated! Switch to mysqli or PDO
And I ask again - why are you sending all the data back based on the number of boxes (including the submit button!). Surely you only need the info send back once, as it's the same. If you've got 10 boxes, why send the company name back 10 times??? In fact, why send it back at all? You're not using it..
ASKER
@Chris
Good point. In my scenario, I only need to send back the box value. Thanks for the heads up re split. Will check it out.
Good point. In my scenario, I only need to send back the box value. Thanks for the heads up re split. Will check it out.
ASKER
Thanks for all the help
If you only need the boxes, your script is very simple:
$boxes = explode(',', $_POST['BA_box']);
$newBoxes = array();
foreach ($boxes as $box) {
if (!empty($box)):
$newBoxes[] = $box;
endif;
endforeach;
$echo json_encode($newBoxes);
In jQuery:var boxes = msg.join(" ");
$("#BA_addbox").html("You have entered box: " + boxes + "</b><br /> You may now close this window.");
Also the logic seems a little flawed. You seem to be splitting the BA_Box data on [,] which seems like an odd delimiter. You then loop through the data and for each box try and json encode the info, over and over again, echoing it out each time. This won't produce valid json
Can you maybe explain what your trying to do (along with a sample of what might be sent through in the 'BA_box')