Link to home
Start Free TrialLog in
Avatar of EICT
EICTFlag for United Kingdom of Great Britain and Northern Ireland

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
    <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>

  

Open in new window


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(); 

Open in new window



Thanks for your Help
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EICT

ASKER

Thanks. I still can't get this to work though.  I'm assuming  line 6 should be $.each(err, function(i){
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
$(this).dialog("destroy");

Open in new window

into the else block.  Then it should work.
Avatar of EICT

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
Avatar of EICT

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(response); to collect the echoed response and replace the html within the paragraph tag with id errors.