-2

I have a syntax error at my console: unexpected token 's' at handle result (signup.php:193:22. I don't know where to debug I am not that very much knowledgeable in javascript. I am creating a chat application whereby I am currently stack and the error is in my signup page. Please help me by editing on my code where i might have missed something. Thanks!

<script type="text/javascript">

    function _(element){

        return document.getElementById(element);
    }

    var signup_button = _("signup_button");
    signup_button.addEventListener("click",collect_data);

    function collect_data(){

        signup_button.disabled = true;
        signup_button.value = "Loading...Please wait..";

        var myform = _("myform");
        var inputs = myform.getElementsByTagName("INPUT");

        var data = {};
        for (var i = inputs.length - 1; i >= 0; i--) {

            var key = inputs[i].name;

            switch(key){

                case "username":
                    data.username = inputs[i].value;
                    break;

                case "email":
                    data.email = inputs[i].value;
                    break;

                case "gender_male":
                case "gender_female":
                    if(inputs[i].checked){
                        data.gender = inputs[i].value;
                    }
                    break;

                case "password":
                    data.password = inputs[i].value;
                    break;

                case "password2":
                    data.password2 = inputs[i].value;
                    break;

            }
        }

        send_data(data,"signup");

    }

    function send_data(data,type){

        var xml = new XMLHttpRequest();

        xml.onload = function(){

            if(xml.readyState == 4 || xml.status == 200){

                handle_result(xml.responseText);
                signup_button.disabled = false;
                signup_button.value = "Signup";
            }
        }

        data.data_type = type;
        var data_string = JSON.stringify(data);

        xml.open("POST","api.php",true);
        xml.send(data_string);
    }

    function handle_result(result){

        var data = JSON.parse(result);
        if(data.data_type == "info"){

            window.location = "index.php";
        }else{

            var error = _("error");
            error.innerHTML = data.message;
            error.style.display = "block";
 
        }
    }

</script>



//This is my Api file//

<?php 

session_start();

$DATA_RAW = file_get_contents("php://input");
$DATA_OBJ = json_decode($DATA_RAW);

$info = (object)[];

//check if logged in
if(!isset($_SESSION['userid']))
{

    if(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type != "login" && $DATA_OBJ->data_type != "signup")
    {
        
        $info->logged_in = false;
        echo json_encode($info);
        die;    
    }
    
}

require_once("classes/autoload.php");
$DB = new Database();

$Error = "";

//proccess the data
if(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "signup")
{

    //signup
    include("includes/signup.php");

}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "login")
{
    //login
    include("includes/login.php");

}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "logout")
{
    include("includes/logout.php");
}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "user_info")
{

    //user info
    include("includes/user_info.php");
}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "contacts")
{
    //user info
    include("includes/contacts.php");
}elseif(isset($DATA_OBJ->data_type) && ($DATA_OBJ->data_type == "chats" || $DATA_OBJ->data_type == "chats_refresh"))
{
    //user info
    include("includes/chats.php");
}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "settings")
{
    //user info
    include("includes/settings.php");
}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "save_settings")
{
    //user info
    include("includes/save_settings.php");
}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "send_message")
{
     //send message
    include("includes/send_message.php");
}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "delete_message")
{
     //send message
    include("includes/delete_message.php");
}elseif(isset($DATA_OBJ->data_type) && $DATA_OBJ->data_type == "delete_thread")
{
     //send message
    include("includes/delete_thread.php");
}


function message_left($data,$row)
{
    $image = ($row->gender == "Male") ? "ui/images/user_male.jpg" : "ui/images/user_female.jpg";
    if(file_exists($row->image)){
        $image = $row->image;
    }
    
    $a = "
    <div id='message_left'>
    <div></div>
        <img  id='prof_img' src='$image'>
        <b>$row->username</b><br>
        $data->message<br><br>";

        if($data->files != "" && file_exists($data->files)){
            $a .= "<img src='$data->files' style='width:100%;cursor:pointer;' onclick='image_show(event)' /> <br>";
        }
        $a .= "<span style='font-size:11px;color:white;'>".date("jS M Y H:i:s a",strtotime($data->date))."<span>
    <img id='trash' src='ui/icons/trash.png' onclick='delete_message(event)' msgid='$data->id' />
    </div> ";

    return $a;
}

function message_right($data,$row)
{
    $image = ($row->gender == "Male") ? "ui/images/user_male.jpg" : "ui/images/user_female.jpg";
    if(file_exists($row->image)){
        $image = $row->image;
    }
    
    $a = "
    <div id='message_right'>

    <div>";
    
    if($data->seen){
        $a .="<img src='ui/images/tick.png' style=''/>";
    }elseif($data->received){
        $a .="<img src='ui/images/tick_grey.png' style=''/>";
    }

    $a .= "</div>

        <img id='prof_img' src='$image' style='float:right'>
        <b>$row->username</b><br>
        $data->message<br><br>";

        if($data->files != "" && file_exists($data->files)){
            $a .= "<img src='$data->files' style='width:100%;cursor:pointer;' onclick='image_show(event)' /> <br>";
        }
        $a .= "<span style='font-size:11px;color:#888;'>".date("jS M Y H:i:s a",strtotime($data->date))."<span>

        <img id='trash' src='ui/icons/trash.png' onclick='delete_message(event)' msgid='$data->id' />
    </div>";

    return $a;
}


function message_controls()
{
    
    return "
    </div>
    <span onclick='delete_thread(event)' style='color:purple;cursor:pointer;'>Delete this thread </span>
    <div style='display:flex;width:100%;height:40px;'>
        <label for='message_file'><img src='ui/icons/clip.png' style='opacity:0.8;width:30px;margin:5px;cursor:pointer;' ></label>
        <input type='file' id='message_file' name='file' style='display:none' onchange='send_image(this.files)' />
        <input id='message_text' onkeyup='enter_pressed(event)' style='flex:6;border:solid thin #ccc;border-bottom:none;font-size:14px;padding:4px;' type='text' placeHolder='type your message'/>
        <input style='flex:1;cursor:pointer;' type='button' value='send' onclick='send_message(event)'/>
    </div>
    </div>";
}
  • 3
    Based on the little bit of the error message you posted, this isn't a JavaScript problem. Your PHP script is returning an error for whatever MySQL query you are running, and JavaScript is saying that error message is not valid JSON (which it is not). – EssXTee Nov 11 '22 at 12:36
  • 2
    The dev tools provide a **Network** tab. Look at the response of the request there. The response clearly isn’t JSON. – Sebastian Simon Nov 11 '22 at 12:39
  • EssXTee can you help me out please.. – valentine makomere Nov 11 '22 at 12:40
  • @valentinemakomere You don’t show the code that produces the invalid JSON, so how are we supposed to help? – Sebastian Simon Nov 11 '22 at 12:46
  • 2
    @valentinemakomere Well, this code is neither minimal nor does it reproduce the issue. Please use a debugger and step through the execution of `api.php`; the mistake originates from something within your includes. Once you step through with a debugger, you should be able to see where the string starting with `SQLSTATE[H` comes from. Learn about [how to debug small programs](//ericlippert.com/2014/03/05/how-to-debug-small-programs). See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). – Sebastian Simon Nov 11 '22 at 12:58
  • 2
    This was mentioned by **Sebastian Simon** already, but you need to post the response from your request here. This response contains the actual error that is occuring on the server. Your post title only includes `SQLSTATE[H` but typically it should be something like `SQLSTATE[HY093]: invalid parameter number` or something similar. That **HY** number is the important part as it tells you what went wrong with the MySQL query and helps determine how to fix it. We can't tell just by looking at your code because we also don't know anything about the database being used. – EssXTee Nov 11 '22 at 13:06
  • 2
    Also, hopefully the rest of the stack trace might include the line number and filename of the php file which tried to execute the database command. Really, this kind of error check is basic stuff you should know how to do already if you're trying to write a moderately complex php application such as the code you've shown us. – ADyson Nov 11 '22 at 13:22
  • 2
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 11 '22 at 13:23

1 Answers1

-3

Your error should be here (signup.php:193:22). Go to your signup.php document on your code editor and check for line 193. There should be an extra 'S' anywhere, just fix it and you'll be fine