如何使用jquery $ .post()方法提交表单值[重复]

时间:2022-11-23 20:03:07

This question already has an answer here:

这个问题在这里已有答案:

I have 1 main page with a form and another page to process the form value here are source codes of the 2 pages

我有一个带有表单的主页面和另一个处理表单值的页面,这里是2页的源代码

Form Page:

表格页面:

<meta charset="UTF-8">
<title>Form Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="process.php" method="post" id="reg-form">
        Username: <input type="text" id="username" name="username">
        <br>
        Password: <input type="password" id="password" name="password">
        <br>
        <button type="submit" id="submit-btn">Traditional Submit</button>
        <button type="button" id="post-btn">$.Post Submit</button>
</form>
<script>
    $("#post-btn").click(function(){        
        $.post("process.php",function(data){
            alert(data);
        });
    });
</script>

Process Page:

流程页面:

<?php
$username=$_POST["username"];
$password=$_POST["password"];
echo "Username: ".$username;
echo "<br>";
echo "Password: ".$password;?>

if I click the "Traditional Submit" buttton, it works perfectly well.

如果我点击“传统提交”按钮,它的效果非常好。

but when I click the "$.Post Submit" button, I just keep getting error msg "Notice: Undefined Index ..."

但当我点击“$ .Post Submit”按钮时,我只是不断收到错误信息“通知:未定义索引......”

I can not figure out where the problem is, please kindly help check and fix, thanks in advance!

我无法弄清楚问题出在哪里,请提前帮助检查和修复,提前谢谢!

3 个解决方案

#1


50  

You have to select and send the form data as well:

您还必须选择并发送表单数据:

$("#post-btn").click(function(){        
    $.post("process.php", $("#reg-form").serialize(), function(data) {
        alert(data);
    });
});

Take a look at the documentation for the jQuery serialize method, which encodes the data from the form fields into a data-string to be sent to the server.

查看jQuery serialize方法的文档,该方法将表单字段中的数据编码为要发送到服务器的数据字符串。

#2


7  

Get the value of your textboxes using val() and store them in a variable. Pass those values through $.post. In using the $.Post Submit button you can actually remove the form.

使用val()获取文本框的值并将它们存储在变量中。通过$ .post传递这些值。在使用$ .Post提交按钮时,您实际上可以删除表单。

<script>

    username = $("#username").val(); 
    password = $("#password").val();

    $("#post-btn").click(function(){        
        $.post("process.php", { username:username, password:password } ,function(data){
            alert(data);
        });
    });
</script>

#3


5  

Yor $.post has no data. You need to pass the form data. You can use serialize() to post the form data. Try this

Yor $ .post没有数据。您需要传递表单数据。您可以使用serialize()发布表单数据。尝试这个

$("#post-btn").click(function(){
    $.post("process.php", $('#reg-form').serialize() ,function(data){
        alert(data);
    });
});

#1


50  

You have to select and send the form data as well:

您还必须选择并发送表单数据:

$("#post-btn").click(function(){        
    $.post("process.php", $("#reg-form").serialize(), function(data) {
        alert(data);
    });
});

Take a look at the documentation for the jQuery serialize method, which encodes the data from the form fields into a data-string to be sent to the server.

查看jQuery serialize方法的文档,该方法将表单字段中的数据编码为要发送到服务器的数据字符串。

#2


7  

Get the value of your textboxes using val() and store them in a variable. Pass those values through $.post. In using the $.Post Submit button you can actually remove the form.

使用val()获取文本框的值并将它们存储在变量中。通过$ .post传递这些值。在使用$ .Post提交按钮时,您实际上可以删除表单。

<script>

    username = $("#username").val(); 
    password = $("#password").val();

    $("#post-btn").click(function(){        
        $.post("process.php", { username:username, password:password } ,function(data){
            alert(data);
        });
    });
</script>

#3


5  

Yor $.post has no data. You need to pass the form data. You can use serialize() to post the form data. Try this

Yor $ .post没有数据。您需要传递表单数据。您可以使用serialize()发布表单数据。尝试这个

$("#post-btn").click(function(){
    $.post("process.php", $('#reg-form').serialize() ,function(data){
        alert(data);
    });
});