Question: I have a dialog coded in php. On submit, I send the data back to the dialog to store via ajax (I don't want the dialog to close). This is mostly working but the format of the posted data isn't what I would like. Here is the ajax code

```
    $('#but1').click(function() {  
      var apply_to = $("input[name='group-apply']:checked").val();
      var el = document.getElementById('multi-selector');
      var selected = GetSelectValues(el);

      var data = [];
      data[0] = 'apply-to:'+ apply_to;
      data[1] = 'selected:' + selected;
      data = {data};         

       $.ajax({
            url : "example.php",
            type: "POST",
            data : data,
            success:function(result){
              $("#form-container").html(result);
            }
       });
    });   

```

When the above is ran, the php in the dialog displays this

```
    Array
    (
        [data] => Array
            (
                [0] => apply-to:Include
                [1] => selected:8,9,5,1
            )

    )

```

In order to use the above, I need to access it like this and then explode that to get the value. 

```
    $_POST['data'][0]

```

I would like the data to display like this

```
    Array
    ( 
        [0] => apply-to:Include
        [1] => selected:8,9,5,1
    ) 

```

or, ideally:

```
    Array
    ( 
        [apply-to] => Include
        [selected] => 8,9,5,1
    ) 

```

Would someone please explain how to do that, if possible?

Answer: 
In the *Add* button action listener's `actionPerformed` method, you can try this:

```
public void actionPerformed(ActionEvent e) {

    if (! textFieldsValid()) {
        // one or more of the text fields are empty
        // may be display a message in a JOptionPane
        System.out.println("The text fields are not filled with data, etc...");
        return;
    }

    // else, text fields have valid data, so do whatever processing it further...
}

/*
 * This method checks if the text fields are empty and sets their borders as red. Returns
 * a boolean false in case any of the text fields are empty, else true.
 */
private boolean textFieldsValid() {

    boolean validTextFields = true;

    if (textField1.getText().isEmpty()) {
        validTextFields = false;
        textField1.setBorder(new LineBorder(Color.RED));
    }

    if (textField2.getText().isEmpty()) {
        validTextFields = false;
        // textField2.setBorder(...)
    }

    // ... same with textField3 and textField4

    return validTextFields;
}

```
> 
> but were should put the else
> 
> 
> 

It is not mandatory to follow `if` with `else`. The purpose of specifying `else` is to allow your code execution flow to go through all other case when `if` was not satisfied (true).

> 
> if i use this only the last if statement only works for the else
>  statement
> 
> 
> 

Because, `if` might have satisfied, so it executes `else` case. I would suggest to include `return` in each `if` case. So that, if any of the `if` case was satisfied. Then, it won't execute further code.