0

I'm sending JSON to my server via jQuery's ajax method. Here's my jquery, and im pretty sure this is fine:

function stuffs() {
    this.fname = document.getElementById("fname").value;
    this.lname = document.getElementById("lname").value;
    this.email = document.getElementById("email").value;    
}
$(function() {
function ajaxhelper(data){
    //console.log(JSON.stringify(data));
    //this returns "{"fname":"mike","lname":"smith","email":"[email protected]"}" which is what i expect
        $.ajax({
        url: 'postdb.php',
        type: 'POST',
        data: {data : JSON.stringify(data)},
        success: function(data) {
            console.log(JSON.stringify(data));
            console.log("Success");
        },
        error: function(e) {
            console.log(e);
        }

    });
}
$("form").on('submit', function(e){
    e.preventDefault();
    var data = new stuffs();
    ajaxhelper(data);
    //window.location = "postdb.php";
    });
    });
</script>

I get back an 500 server error. Here's my php code. (yes i'm only sending it an fname that i've preloading into my database, $con is valid i just didnt share the code to connect to my database)

$obj = json_decode($_POST['data']);

$sql = "SELECT * FROM testtable WHERE fname = \"$obj->{'fname'}\"";
$query = $con->query($sql);

I think my sql is incorrect due to the quotes? This is where im stuck.

4
  • Please don't directly query user input and try $sql = "SELECT * FROM testtable WHERE fname = '{$obj->fname}'"; Otherwise please check error logs. Commented Jan 15, 2016 at 23:13
  • Try without decode of your $_POST Commented Jan 15, 2016 at 23:19
  • That didn't work. How can i view the php page with data sent over? with ajax I just stay on one page, otherwise I would do like print_r($_POST) on the php page to see what I'm getting sent. Commented Jan 15, 2016 at 23:20
  • Andreas, what do you mean? just have $obj = $_POST['data'], same sql tho? Commented Jan 15, 2016 at 23:22

2 Answers 2

2

Try using $obj->fname instead of $obj->{'fname'}.

Sign up to request clarification or add additional context in comments.

3 Comments

Is your DB working properly? Try calling a query for SELECT that you try to run, but with data written into the query instead of data from the JSON.
Also, you stuffs() function doesn't seem to return any value, try changing it to something like that: function stuffs() { var data = { fname: document.getElementById("fname").value, lname: document.getElementById("lname").value, email: document.getElementById("email").value }; return data; }
YES!!! THANK YOU SO MUCH! So the database was broken. permission issues, blah blah blah. Thank you for bringing up that i should just hard code something to test it!!
0

Here is the correct syntax for your SQL Statement

$sql = "SELECT * FROM testtable WHERE fname = '".$obj->fname."'";

1 Comment

This looks good! but it still gives a 500 error, which does not happen when the php doesn't have last 2 lines. it must be something else now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.