title: How to send and recieve multivariables to php using json and array
body: <p>I want to send some data from a form to a PHP file using jQuery. I've searched and I noticed I have to send the data as JSON and receive multiple variables as an array. However I'm Completely confused it's not working.</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;input id="username" type="text" class="inputBox"&gt;
  &lt;input id="password" type="password" class="inputBox"&gt;
  &lt;button id="submitLogin" class="submitLogin"&gt;login&lt;/button&gt;
  &lt;div id="test"&gt;&lt;/div&gt;
  &lt;div id="test1"&gt;&lt;/div&gt;
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>$(document).ready(function() {
    $("#submitLogin").click(function() {
      var superuser = $("#username").val();
      var superpass = $("#password").val();
      $.ajax({
        type: "POST",
        url: 'http://localhost/mainclinic/controllers/login/login.php',
        dataType: 'application/json',
        data: {
          loginid: superuser,
          loginpass: superpass
        },
        cache: false,
        success: function(result) {
          $('#test').html(result[0]);
          $('#test1').html(result[1]);
        }
      })
    })
  })
  </code></pre>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
    include "../config.php";
  
    if (!$db) 
    {
      die("Connection failed: " . mysqli_connect_error());
    }
  
    if ($_SERVER['REQUEST_METHOD'] === 'POST') 
    {
      $username = mysqli_real_escape_string($db, $_POST['loginid']);
      $password = mysqli_real_escape_string($db, $_POST['loginpass']);
      $sql = "SELECT id FROM superusers WHERE docid = '$username' and doccpass = '$password'";
      $result = mysqli_query($db, $sql);
      $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  
      if(mysqli_num_rows($result) &gt; 0)
      {
        $array = array(success, $username);
        echo json_encode($array);
      }
      else
      {
        $array = array(failed, nousername);
        echo json_encode($array);
      }
    }
  ?&gt;
  </code></pre>