EICT
asked on
How do I pass an array of values back to a calling PHP page using Json and display the array values
Hi,
I have two php pages form.php and process.php The form.php page has a Jquery dialog box which when saved posts the values to the process.php page for processing (i.e. updating MySQL DB) . I'm doing my validation in the process.php page using php. If there is a error I'm passing that to an array error[]. How do I pass the error array values back to the form.php page for display in a list.
My code extracts are:
Form.php
process.php code snippet
Thanks for your Help
I have two php pages form.php and process.php The form.php page has a Jquery dialog box which when saved posts the values to the process.php page for processing (i.e. updating MySQL DB) . I'm doing my validation in the process.php page using php. If there is a error I'm passing that to an array error[]. How do I pass the error array values back to the form.php page for display in a list.
My code extracts are:
Form.php
<script>
$(function() {
$( "#opener" ).click(function() {
$( "#dialog-window" ).dialog({
autoOpen: true,
maxWidth:550,
maxHeight: 600,
width: 550,
height: 600,
draggable: true,
modal: true,
buttons: {
"Save": function () {
$.post("../management/savealert.php", $("#mainForm").serialize(), function(error)
{$("button").html();
// Take error array and apply to errors id list below.
});
$(this).dialog("destroy");
},
"Cancel": function () {
$(this).dialog("destroy");
}
}
});
});
});
</script>
<ul id="errors"></ul>
process.php code snippet
$errors[] = 'You forgot to enter an Alert.';
$errors[] = 'You forgot to enter a Type.';
echo json_encode($errors,JSON_FORCE_OBJECT);
exit();
Thanks for your Help
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You are correct. What is saying the console?
I guess the dialog is closed without displaying the errors, right?
You should add a if-else block by checking if there's any error. Move the line
You should add a if-else block by checking if there's any error. Move the line
$(this).dialog("destroy");
into the else block. Then it should work.
ASKER
I shall read Rays articles and work on this again over the next couple of days. Will feed back then.
Thanks for your help
Thanks for your help
ASKER
I read Rays article.
In the end it was very easy. Rather than using an array to pass the errors I echoed the errors from the server-side script in the html format I required (ie as a list).
I then used $("p#errors").html(respons e); to collect the echoed response and replace the html within the paragraph tag with id errors.
In the end it was very easy. Rather than using an array to pass the errors I echoed the errors from the server-side script in the html format I required (ie as a list).
I then used $("p#errors").html(respons
ASKER