Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the debugger shows \"SyntaxError: JSON.parse: unexpected character at line 1 col

ID: 3700749 • Letter: T

Question

the debugger shows "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON dat" How can I fix it

<html>
<head>
<style>
table {
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
}
</style>

<script>

function ajax_post() {
  
     
       var letter = document.getElementById("input_letter").value;
     
       var xmlhttp = new XMLHttpRequest();
     
    
     
           xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
               var to_show;
               var results = JSON.parse(this.responseText)
               if (results.length > 0)
               {
                 
                    to_show = "<table>";
                    to_show += "<tr>";
                  
                    to_show += "<th>"; to_show += "Email"; to_show += "</th>";
                    to_show += "<th>"; to_show += "Password"; to_show += "</th>";
                    to_show += "<th>"; to_show += "Birthday"; to_show += "</th>";
                    to_show += "</tr>";
                    for (var i = 0; i < results.length; i++)
                    {
                        var json_result = results[i];
                        to_show += "<tr>";
                        to_show += "<td>"; to_show += json_result[1]; to_show += "</td>";
                        to_show += "<td>"; to_show += json_result[2]; to_show += "</td>";
                        to_show += "<td>"; to_show += json_result[3]; to_show += "</td>";
                        to_show += "</tr>";                      

                    }
                    to_show += "</table>";
               }
               else {
                    to_show = "There is no email address starts with '"+letter+"'";
               }
              
                document.getElementById("display_records").innerHTML = to_show;
                            
            }
         
       
       }
       
         xmlhttp.open("POST", "display.php", true);
         xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
         xmlhttp.send("q="+ letter);
  
}
</script>
</head>
<body>

<form>
<h3>Please enter the first letter of the email: </h3>

<input type="text" id="input_letter" />
<input type="button" value="Search" />
</form>

<div id="display_records">Students information will be listed here...</div>

</body>
</html>

<?php

// Receive input value 'q' from $_POST in a variable $q
$q = $_POST["q"];

// Connect to MySQL and save connection in a variable $con
// Check for errors in the connection
$con = mysqli_connect("localhost", "gaoha202", "JIAYI521","gaoha202");


if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// Perform a query, check for error
if (!mysqli_query($con))
{
   echo("Error description: " . mysqli_error($con));
  
  
}

// Create string variable $sql with SQL selecting users where email LIKE $q
// Execute query in MySQL using "$con->query($sql)" and save in $result

$sql = " SELECT * FROM User WHERE email LIKE '$q%' ";
$results = $con->query($sql);


// Define empty array $allRows
// Iterate through results while $row = $result->fetch_array()
// Append $rows to $allRows with $allRows[] = $row

$allRows = array();

while($row = $results->fetch_array())
{

   $allRows[] = $row;
}

// Echo $allRows in json format using the function json_encode

echo json_encode($allRows);

mysqli_close($con);

?>

Explanation / Answer

In php file

mysqli_query() fucntion takes two arguments but you provide only one argument.This lead to give json.parse error

place the query in that function like below

$sql = " SELECT * FROM User WHERE email LIKE '$q%' ";

// Perform a query, check for error
if (!mysqli_query($con,$sql)) // required two arguments
{
   echo("Error description: " . mysqli_error($con));
  
}