简体   繁体   中英

pass parameters from jquery to php

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.

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.

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);
    }
});

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.

在此行:type:'POST'之后,添加此行:data:dataString,

`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);
            }
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM