0

I have this jquery script

$(function() { // <----doc ready starts
    $("#btn1").click(function(e) {
        e.preventDefault();
        var name = $("#id1").val(); 
        var last_name = $("#id2").val();
        var dataString = {'name=':name, 'last_name': last_name};
        $.ajax({
            type: 'POST',
            dataType: 'jsonp',
            url: 'http://localhost/insert.php',
            success: function(data) {
                alert(data);
            }
        });
    });
});

And this php one inserting parameters from the first script into mysql database:

<?php
    $conn = mysqli_connect('localhost', 'root', '');
    $name = $_POST['name'];
    $last_name = $_POST['last_name'];
    $mysqli = new mysqli('localhost','root','','os');

    if ($mysqli->connect_error) {
        die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }
    $insert_row = $mysqli->query("INSERT INTO table_info (name, name2) VALUES($name, $last_name)");

    if ($insert_row){
        print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />'; 
    }
    else {
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }
    $mysqli->free();
    $mysqli->close();
?>

When I'm trying to run it, it is failing with that error:

Notice: Undefined index: name in C:\wamp\www\insert.php on line 3
Notice: Undefined index: last_name in C:\wamp\www\insert.php on line 4
Error : (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' )' at line 1

What's wrong here, sorry for stupid question, this is my first experience with php and jQuery both.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Andrey Saleba
  • 2,167
  • 4
  • 20
  • 27

5 Answers5

6

You're not providing your dataString variable to the AJAX call, so no data is being sent. You need to add it in the data property of $.ajax:

$("#btn1").click(function(e) {
    e.preventDefault();
    var name = $("#id1").val(); 
    var last_name = $("#id2").val();
    var dataString = { 'name': name, 'last_name': last_name };

    $.ajax({
        type: 'POST',
        dataType: 'jsonp',
        url: 'http://localhost/insert.php',
        data: dataString,
        success: function(data) {
            alert(data);
        }
    });
});

Note that I also fixed the name= typo in the object definition.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

Generate the valid dataString.Try with -

var dataString = "{'name=':"+name+", 'last_name': "+last_name+"}";

And pass it to the call -

$.ajax({
    type: 'POST',
    data: dataString,
    dataType: 'json',
    url: 'http://localhost/insert.php',
    success: function(data) {
        alert(data);
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

You are trying to read from $_POST but you are making a GET request.

JSONP is, by specification, always GET. You cannot make a POST request using that technique.

type: 'POST', will be ignored since you say dataType: 'jsonp',


You are also trying to read from name but you have called your field name=. You need to use the same names throughout.


When you get a response, you will get an error because your PHP script is not responding with JSONP.

You need to have header("Content-Type: application/javascript"); and then something along the lines of:

echo $_GET['callback'] . "(" . json_encode($your_response) . ");";

… but you should add protection for the Rosetta vulnerability by sanitising the callback name.


Alternatively, remove dataType: 'jsonp', and add header("Content-Type: text/plain"); to the PHP.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I tried that, but errors are the same, I think I'll it like it is done here: [link]http://stackoverflow.com/questions/5143191/inserting-into-mysql-from-php-jquery-ajax[/link] – Andrey Saleba Feb 05 '15 at 11:58
0

After this line :type: 'POST', add this line: data:dataString,

ainasma
  • 79
  • 4
-2
`enter code here`try to change the ajax call like this
`enter code here`$(function() { // <----doc ready starts
   `enter code here` $("#btn1").click(function(e) {
      `enter code here`  e.preventDefault();
        var name = $("#id1").val(); 
        var last_name = $("#id2").val();
        var dataString = {'name=':name, 'last_name': last_name};
        $.ajax({
            type: 'POST',
            dataType: 'jsonp',
            url: 'http://localhost/insert.php',
            data: JSON.stringify(dataString),
            success: function(data) {
                alert(data);
            }
        });
    });
});
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
Wintergreen
  • 236
  • 1
  • 9