Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从表单读取的Ajax_Php_Jquery_Ajax - Fatal编程技术网

Php 从表单读取的Ajax

Php 从表单读取的Ajax,php,jquery,ajax,Php,Jquery,Ajax,我正在为我的游戏编写一个存储系统, 它工作得相当好,直到我发现它只需要第一次输入项目的数量 function pbuy(buyitem) { var amountc = "amount-"+buyitem, var amount = $("#"+amountc+"").val(); $.ajax({ type: "POST", url: "store2.php", data: "buyitem="+ buyitem+"&amount="+amount,

我正在为我的游戏编写一个存储系统, 它工作得相当好,直到我发现它只需要第一次输入项目的数量

function pbuy(buyitem) {
 var amountc = "amount-"+buyitem,
 var amount = $("#"+amountc+"").val();
   $.ajax({
   type: "POST",  
   url: "store2.php",  
   data: "buyitem="+ buyitem+"&amount="+amount,  
   success: function(resp){  
   document.getElementById('ajaxDiv').innerHTML =  resp;

   },  
   error: function(e){  
     alert('Error: ' + e);  
   }  
 });  
}  
我试着给它表单的Id,如下所示:

function pbuy(buyitem) { var amountc = "amount-"+buyitem, var amount = $("#"+amountc+"").val();
但什么也没发生

创建表单的代码为:

<tr> 
    <td class='items' width='80%' align='center' valign='top'>
        <?PHP echo $itemstore->itemname;?> 
    </td> 
    <td width="20%">  
        Price:<?PHP echo $itemstore->newprice;?>          
        <form  method="post"> 
            <input type='text' id='amount-<?PHP echo $row;?>)' />   
            <input name="itembid" type="button" onclick="pbuy(<?PHP echo $row;?>)"  value="Buy" />
        </form>
    </td> 
</tr>

如果我在ajax函数中硬编码了金额,它会正常运行。

您是否检查了ajax请求的响应?

在发送ajax请求之前,您会收到一个javascript错误,因为您正在定义用逗号分隔的金额和金额变量。或者

 var amountc = "amount-"+buyitem;
 var amount = $("#"+amountc+"").val();
分号分隔,或保留逗号并跳过第二个变量


只要是这样,什么也不会发生。不过如果我为数量设定一个值,它会很有效。谢谢,这很有效!这似乎是一个简单的错误,但我花了一个小时观察代码,仍然没有看到它;然而,对于javascript,使用firebug之类的工具——甚至是大多数浏览器的内置控制台——会有很大帮助,如果你还没有这样做的话。这样的语法错误会立即弹出。默认工具在Firefox中通过Ctrl+Shift+J打开,在IE中通过Chrome和F12打开
 var amountc = "amount-"+buyitem,
 amount = $("#"+amountc+"").val();