0

I don't have experience using JSON with php and ajax and I need your help.

I have a php page that validates user input... Before using json, I used to store them in an array. I was able to parse the json when I store my values in an array like this $data = array("success"=>true, "msg"=>"my masg"); json_decode($data);

and then in my ajax call on success function I data.success and data.msg

How do I store multipe msgs or errors in my array and parse them in my ajax call? here is my code

here I want to validate user input and I would like to validate many fields

    if(isset($_POST['txt_username']) &&($_POST['txt_username']!=""))
    {
        $username =trim( $_POST['txt_username']);
        $username  = filter_input(INPUT_POST, 'txt_username',
                    FILTER_SANITIZE_STRING);
    }
    else
    {

             }

So how do I do it in the else statement?

here is my ajax call

 $.ajax({       
            type: "POST",
            dataType: "json",
            cache: false,
            data: {txt_username:$("#username").val(), txt_password:$("#password").val()},

            beforeSend: function(x) {

                if(x && x.overrideMimeType) {

                    x.overrideMimeType("application/json;charset=UTF-8");
                }

            },

            url: 'proccessing_forms/admin_login.php',

            success: function(data) {

                // parse the data
            }
    });


    }

2 Answers 2

1

You can use multidimensional arrays for this:

$msg = array("success" => array(), "error" => array());

// ... DO you processing on if and else

//When you need to add to an success do
$msg['success'][] = "My Success";

//When you need to add to an error do
$msg['error'][] = "My error";


echo json_encode($msg); exit;

On the ajax's success event, you can access both of these variables individually.

success: function(data) {
    var errors = data.error;
    var succesess = data.success

    //Process each msg
    $.each(errors, function(index, msg) {
         console.log(msg);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

In PHP you can create a JSON string like this:

<?php

  $array["error"][] = "message1";
  $array["error"][] = "message2";

  echo json_encode($array);

?>

Then in JavaScript callback function you can use it like this:

  success: function(data) 
  {
    $.each(data, function(key, val) {
      alert(key + " - " + val);
    });
  }

Of course you should notice that now val is an array due to my change in PHP and you have to run another loop on it to get your content:

  success: function(data) 
  {
    $.each(data, function(key, val) {
      $.each(val, function(key2, val2) {
        alert(key2 + " - " + val2);
      }
    });
  }

2 Comments

But how do you populate the array with more than one input? like what do I do when I validate the next user input? if I do the same as you did with all user input, then it will overwrite it
@user2589904 Actually I'm not sure what are you asking for about more than one input. Think it's better that you provide an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.