将参数从jquery传递到php


pass parameters from jquery to php

我有这个jquery脚本

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

这个php将第一个脚本中的参数插入到mysql数据库中:

<?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();
?>

当我试图运行它时,它失败了,出现了错误:

注意:第3行C:''wamp''www''insert.php中的未定义索引:名称
注意:第4行C:''wamp''www''insert.php中的未定义索引:last_name
错误:(1064)您的SQL语法有错误;查看与MySQL服务器版本对应的手册,了解在第1行的")"附近使用的正确语法

这里出了什么问题,对不起这个愚蠢的问题,这是我第一次使用php和jQuery。

您没有向AJAX调用提供dataString变量,因此没有发送任何数据。您需要将其添加到$.ajax:的data属性中

$("#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);
        }
    });
});

注意,我还修复了对象定义中的name=拼写错误。

您正在尝试从$_POST读取,但您正在发出GET请求。

根据规范,JSONP是始终GET。您不能使用该技术发出POST请求。

type: 'POST',将被忽略,因为您说dataType: 'jsonp',


您也在尝试从name读取,但您已将字段称为name=。您需要始终使用相同的名称。


当您收到响应时,您将收到一个错误,因为您的PHP脚本没有使用JSONP进行响应。

你需要有header("Content-Type: application/javascript");,然后是类似的东西

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

…但您应该通过清除回调名称来为Rosetta漏洞添加保护。


或者,删除dataType: 'jsonp',并将header("Content-Type: text/plain");添加到PHP中。

生成有效的dataString。尝试使用-

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

并将其传递给呼叫-

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

在这行: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);
            }
        });
    });
});