简体   繁体   English

带有Ajax的jQuery帖子不起作用

[英]Jquery with Ajax post not working

I am trying to make a simple game and I'm not very good with jQuery. 我试图做一个简单的游戏,但我对jQuery不太满意。 The code I have is: 我的代码是:

<script type="text/javascript">
$(document).ready(function(){
    $('#deposit').click(function(){       
        $.ajax({
            type: 'POST',
            url: 'update.php',
            dataType: 'json',
            data: {
                Money : $('#input_money').val()
            },
            success: function(data){ 
                $('#display').html(data.value);
            }
        });
    });
});
</script>

And the display is this: 显示是这样的:

<input id="input_money" name="input_money" type="text" size="40"><br><br>
<button id="deposit">Deposit Money</button>
<div id="display"></div> 

For the back end, I am using this: 对于后端,我正在使用:

if(isset($_POST['Money'])){
    $value = $_POST['Money'];   
} else {
    $value = "";
}
echo json_encode(array("value"=>$value));  

Can anyone help me out? 谁能帮我吗? I plan to add the $value into a database after it shows up on the main page. 我计划在主页上显示$value后将其添加到数据库中。

Thanks 谢谢

I fired this on my serv, find it at: this link 我解雇这对我SERV,在找到它: 此链接

It works just fine. 它工作正常。 Heres the source, just like yours. 就像您一样,这里是来源。

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
   $('#deposit').click(function(){      
  $.ajax({
            type: 'POST',
            url: 'update.php',
            dataType: 'json',
            data: {
                Money : $('#input_money').val()
            },
            success: function(data){ 
                $('#display').html(data.value);
            }
        });
   });
});
  </script>


<input id="input_money" name="input_money" type="text" size="40"><br><br>
<button id="deposit" type="button">Deposit Money</button>

<div id="display"></div> 

Hope youll get it to work. 希望您能正常使用。 Best regards Jonas 最好的问候乔纳斯

Try this (untested and without Json, but should work straight). 尝试一下(未经测试,没有Json,但应直接使用)。

HTML: HTML:

<form method="post" action="" id="myform">
<p><input id="input_money" name="input_money" type="text" size="40"></p>
<button id="deposit" type="submit" name="deposit">Deposit Money</button>
</form>

<div id="display"></div> 

JS: JS:

    <script type="text/javascript">
      $(document).ready(function()
      {
       $('#myform').submit(function()
       {   
        var dataString = $(this).serialize();   
        $.ajax({
            type: 'POST',
            url: 'update.php',
            data: dataString,
            success: function(response){ 

                $('#display').html(response);
            },
            error: function(){
            alert('There was an error in AJAX call!');
            }
        });
        return false;
       });
    });
</script>

PHP: PHP:

echo isset($_POST['input_money']) ? htmlentities($_POST['input_money']) : 'no value';

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

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