简体   繁体   English

如何在$ .ajax方法提交的表单中使用$ _POST方法获取数据

[英]how to get data using $_POST method when form submitted by $.ajax method

JavaScript function JavaScript函数

function sub()
{
var url = "showpdf.php";
$.ajax({
    type: "post",
    url: url,
    success: function(response)
    {
        document.getElementById("alldata").innerHTML = response;
    }
});
}

I am calling this function in html like this: 我在html中调用这个函数是这样的:

<input type = "button" value="Go" onclick="sub()">

in showpdf.php file I write bellow code to get form data showpdf.php文件中,我写了下面的代码来获取表单数据

$academic = $_POST['academic'];
$uni = $_POST['University'];

but I am getting this error 但是我收到了这个错误

Undefined index: academic 未定义的索引:学术

You need to put in your data into the ajax call, you aren't actually sending anything to the php script at the moment. 您需要将数据输入到ajax调用中,此时您实际上并未向php脚本发送任何内容。

you can add data to your ajax call as follows 您可以按如下方式向ajax调用添加数据

function sub()
{
var url = "showpdf.php";
$.ajax({
    type: "post",
    url: url,
    data: {'academic': 'wiiiieee'},
    success: function(response)
    {
        document.getElementById("alldata").innerHTML = response;
    }
});
}

where data is a Object containing key->value pairs 其中data是包含key-> value对的Object

Pass you data into the ajax call: 将数据传递给ajax调用:

Ex: 例如:

//get your post data into these variables //将您的帖子数据输入这些变量

var University; 
var academic;

//and pass it like //并传递它

var dataString = 'academic='+ academic+ '&University=' + University;

$.ajax({
        type: "post",
        data: dataString,
        url: url,
        success: function(response)
        {
            document.getElementById("alldata").innerHTML = response;
        }
    });

You should get the data from the input fields and send through the ajax post call 您应该从输入字段获取数据并通过ajax post调用发送

function sub()
{
var url = "showpdf.php";
var university  = jQuery("#university").val(); //id of the university input field
var academic  = jQuery("#academic").val(); // id of the academic input field
$.ajax({
    type: "post",
    url: url,
    data: { university: university, location: academic },
    success: function(response)
    {
        document.getElementById("alldata").innerHTML = response;
    }
});
}

You're not sending any data through with the ajax call. 您没有通过ajax调用发送任何数据。

function sub(data)
{
    var url = "showpdf.php";

    $.ajax({
        type: "post",
        data: data,
        url: url,
        success: function(response)
        {
            document.getElementById("alldata").innerHTML = response;
        }
    });
}

var data = {"academic": "something", "University": "something else"};
sub(data);

you're getting or sending data? 你收到或发送数据? I do not understand, if you are sending lack DATA, you can send serialized form as follows: $("#form").serialize(); 我不明白,如果您发送缺少DATA,您可以发送序列化形式如下:$(“#form”)。serialize(); , #form is ID ,#form是ID

function sub(){ function sub(){

var url = "showpdf.php"; var url =“showpdf.php”;

var dataString= $("#form").serialize(); var dataString = $(“#form”)。serialize();

$.ajax({ $就({

type: "post", 类型:“post”,

url: url, url:url,

data: dataString, data:dataString,

success: function(response) 成功:功能(响应)

{ {

document.getElementById("alldata").innerHTML = response; document.getElementById(“alldata”)。innerHTML = response;

} }

}); });

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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