本文介绍了当使用jquery时,表单字段不会发布到php文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两个字段的表单的HTML文件。这些字段的值应该发布到PHP中,并且应该使用JQuery从HTML获取这个PHP。这是我实现的。

I have an HTML file that has a form with two fields. These fields' value should be posted to a PHP and this PHP should be fetched from the HTML using JQuery. This is what I implemented.

我的HTML文件:

My HTML file:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
    <script>
        $(document).ready(function(){
           $("button").click(function(){
                $("#first").load("result_jquery.php");
            });
        });

    </script>
</head>
<body>
    <div id="first"></div>
    <div>
        <form method="POST" id="myForm">
        Name: <input type="text" name="name"/><br/>
        Number: <input type="text" name="number"/><br/>
        <button>submit</button>
        </form>
    </div>
</body>

这是我的 result_jquery.php

This is my result_jquery.php

<?php
$n = $_POST["name"];
echo "hello ".$n;
?>

当我点击提交按钮时,hello正在打印。但名字没有被印刷。你能帮我解决这个问题吗?我不知道我在哪里出错。

When I click the submit button, the hello is getting printed. But the name is not getting printed. Can you please help me with this. I don't know where I am going wrong.

推荐答案

我认为使用按钮元素是令人担忧的代码,我会把它现在工作正常,因为你需要所以试试这个,并告诉我结果:)

I think that the use of the button element is the worry and the code that i will put now it is working properly as you need so try this and tell me the result :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
<script>
    $(document).ready(function(){
       $("#button").click(function(){
            var n = $('[name="namee"]').val();
            var nb = $('[name="number"]').val();
            $("#first").load("result_jquery.php",{'namee':n,'number':nb},function(data){});
        });
    });

</script>
</head>
<body>
    <div id="first"></div>
    <div>
        <form method="POST" id="myForm">
            Name: <input type="text" name="namee"/><br/>
            Number: <input type="text" name="number"/><br/>
            <input type="button" value="Submit" id="button" />
        </form>
    </div>
</body>
</html>

这篇关于当使用jquery时,表单字段不会发布到php文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:15