0

I'm trying to get a simple SQL query result and echo it to JS, but the data that returns is showing up as "Result".

How do I make the SQL query result usable in JS? The SQL query result should be a simple '32'.

I tried using the suggestion in the duplicate thread, but I cant get it working. I don't think I need an array to echo a single result.

PHP:

<?php 

require "../connectionPages/localConnect.php";

$fName  = $_POST["fName"];
$lName  = $_POST["lName"];
$email  = $_POST["email"];
$pw     = $_POST["pw"];

if($fName == null || $lName == null || $email == null || $pw == null)
    $message = "missing required data";
else
{

    $SQL =  "INSERT INTO `customer/User` (custFName,
                                custLName,
                                custEmail,
                                custPassword)
    VALUES ('$fName', '$lName','$email', '$pw')";

    $mysqli->query($SQL);

    if($mysqli->affected_rows > 0)
        $message = "Record successfully inserted <br><a href='..'>Back to Main Page</a>";

        $SQL = "SELECT max(custID) as ID FROM `customer/User`";
        $result = $mysqli->query($SQL);

        echo $result;

        if($mysqli->affected_rows > 0) 
        {
            $message .= "<input type='text' value='id' id='userID' hidden />";
        }
    else
        $message = "Unable to insert record: " . $mysqli->error;

    $mysqli->close();
}

?>

JS:

$("#SignupSubmit").click(function()
{
    var fName = $("#txtSignFName").val();
    var lName = $("#txtSignLName").val();
    var email = $("#txtSignEmail").val();
    var pw    = $("#txtPW").val();

    if( fName == "" || lName == "" || email == "" || pw == "" )
    {
        alert();
    }

    else 
    {
        var URL = "actionPages/signUp.php";
        $.get(URL, {
                fName:fName,
                lName:lName,
                email:email,
                pw:pw},
                function(response) {
                    $('#hiddenUserID').html(response);
                });

        $.ajax(
        {
            method: "post",
            url: URL,
            data: {fName:fName, lName:lName, email:email, pw:pw}, 
            success: function(data)
            {
                $("#hiddenUserID").html(data);
            }
        });
    }
});
Calisto
  • 323
  • 7
  • 19
  • 1
    When you call `mysqli->query()`, you are getting back a `mysql_result` object. You can't just echo it out -- its not a string. Fetch the data and display the rows: http://php.net/manual/en/class.mysqli-result.php – Jeremy Harris Dec 12 '16 at 22:24
  • 1
    @Machavity, I disagree that this is a duplicate of that question. – Jeremy Harris Dec 12 '16 at 22:25

0 Answers0