Note that there are some explanatory texts on larger screens.

plurals
  1. POretrieving Message from a json array
    primarykey
    data
    text
    <p>I want to check the username availability while users register. I am working on the front end. The backend code was given to me. </p> <p>These are the php code in signup.php</p> <pre><code>if (isset($_GET['chkusername'])) JSON_username_avail($_GET['chkusername']); function JSON_username_avail($username) { $ret = array(); print json_encode(validate_username($username, $ret)); die(); } function validate_username($username, &amp; $retval_arr) { if ($username == NULL) $retval_arr['E_UserName'] = "NULL_USERNAME"; else if (!username_validation($username)) $retval_arr['E_UserName'] = "INVALID_USERNAME"; else if (!data_not_exists("user", "username", $username, TRUE)) $retval_arr['E_UserName'] = "USERNAME_EXISTS"; return $retval_arr; } function username_validation($user) { $username = str_split($user); foreach($username as $i) { $i = ord($i); if ($i &gt;= 48 and $i &lt;= 57) continue; if ($i &gt;= 65 and $i &lt;= 90) continue; if ($i &gt;= 97 and $i &lt;= 122) continue; return FALSE; } return TRUE; } function data_not_exists($table, $field, $data, $CSense = FALSE) { $conn = connect_db(); $data = filter_var($data, FILTER_SANITIZE_STRING); if ($CSense == TRUE) $sql = "SELECT * FROM ".$table. " WHERE ".$field. "='".$data. "'"; else $sql = "SELECT * FROM ".$table. " WHERE upper(".$field. ")='".$data. "'"; $result = mysqli_query($conn, $sql); switch ($result - &gt; num_rows) { case 0: return TRUE; break; case 1: return FALSE; break; default: die("500 Internal Server Error: 122"); } //switch } </code></pre> <p>Now I dont know that much of php. I created a javascript function to send the username to the signup.php page for validation. </p> <p>Here is my function</p> <pre><code>function submit_form() { var u = document.getElementById("username").value; $.post("signup.php", { "chkusername": u }, function (data) { var x = data; //here i dont know how to get the return string. Whether it is NULL_USERNAME OR INVALID_USERNAME OR USERNAME_EXISTS. }, "json"); } </code></pre> <p>here i am getting the value of x as [object Object]. But i need to store the return message in variable x. I want to know whether it is NULL_USERNAME OR INVALID_USERNAME OR USERNAME_EXISTS. Kindly help me with that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload