$ .ajax数据未将指定值发布到$ _POST [英] $.ajax data not posting specified value to $_POST

查看:66
本文介绍了$ .ajax数据未将指定值发布到$ _POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我在这里做错了什么? 我正在尝试使用JQuery $ .ajax函数从表单中发布数据并返回json数据.但是每次发布数据为空.

Okay, what have I done wrong here? I'm trying to .POST data from a Form with JQuery $.ajax function and return json data. But every time the post data is null.

<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
</head>

<body>
<form action="testform" method="post">
OLD ID:
<input id="OldID" name="OldID" type="text"><br>
NewID:
<input id="NewID" name="NewID" type="text"><br>

<input id="do" name="do" type="button" value="Get New ID">

</form>

<script type="text/javascript">

            var CID = $('#OldID').val();

        $(document).ready(function(){
                $('#do').click(function(){
                    sendValue();   
                }); 

        function sendValue(CID){

               $.ajax({
                  type: 'POST',
                  url: 'getNewID.php',
                  data: {OldID: CID},
                  dataType: 'json',
                  cache: false,
                  success:  
                    function(data){
                    $('#NewID').val(data.NewID);
                    }
              }) }
        });

</script>


</body>
</html>

这是PHP页面

<?php 

if (isset($_POST['OldID'])){

    $value = $_POST['OldID'];   

}else{
    $value = "ELSE VALUE";
}
echo json_encode(array("NewID"=>$value));  

?>

我确定这是一个简单的错误,但是我现在只是转圈.

I'm sure this is a simple mistake on my part but I'm just going in circles at this point.

提前谢谢!

推荐答案

您的错误是您在脚本中而不是某些事件处理程序方法上获取了OldID字段的值.这导致在加载脚本时(正在查看页面时),文本输入的值保留在变量CID上.当用户单击#do按钮时,CID的值仍与加载页面时的值相同.因此,没有值存储在CID变量中,因此没有值通过AJAX发送. 定义另一个函数(说sendOldID),该函数读取CID变量中输入的文本的值,然后使用CID参数调用sendValue()方法.将该功能用作#do按钮的事件处理程序.

Your mistake is that you are getting the value of the OldID field in your script, and not on some event handler method. This causes when the script is loaded (when the page is being viewed), the value of the text input is kept on variable CID. and when the user clicks on the #do button, then the value of CID is still the same as the one when the page was loaded. So no value is stored in CID variable, so no value is sent via AJAX. Define another function (Say sendOldID) that reads the value of the text input in the CID variable, and then calls the sendValue() method with CID paramter. The use that function as the event handler of the #do button clicked.

<script type="text/javascript">
    $(document).ready(function(){
            $('#do').click(function(){
                sendOldID();   
            });

    function sendOldID() {
        var CID = $('#OldID').val();
        sendValue(CID);
    }

    function sendValue(CID) {
           $.ajax({
              type: 'POST',
              url: 'getNewID.php',
              data: {OldID: CID},
              dataType: 'json',
              cache: false,
              success:  
                function(data){
                $('#NewID').val(data.NewID);
                }
          }) }
    });
</script>

这篇关于$ .ajax数据未将指定值发布到$ _POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆