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

How to use jquery $.post() method to submit form values(如何使用 jquery $.post() 方法提交表单值)
本文介绍了如何使用 jquery $.post() 方法提交表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 1 个带有表单的主页面和另一个用于处理表单值的页面这是两页的源代码

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

表单页面:

<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>

流程页面:

<?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.

但是当我点击$.Post Submit"按钮时,我不断收到错误信息注意:未定义索引..."

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

我不知道问题出在哪里,请帮助检查和修复,在此先感谢!

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

推荐答案

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

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

查看 jQuery serialize 方法的文档,该方法对来自将字段转换为数据字符串发送到服务器.

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 $.post() 方法提交表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)