本文介绍了获得从PHP foreach循环阿贾克斯变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的AJAX(jQuery的版本)剧本,很短的PHP函数,它们工作正常没有问题。当我提交的表单输入的值是,阿贾克斯将努力派,并得到结果PHP脚本,在这种情况下拿到这本书订单的总金额。阿贾克斯脚本和HTML部分如下:

I have a simple ajax (jquery version) script and very short php function and they works fine without problem.When I submit the value from the form input are, the ajax will work to send and get result from the php script, in this case to get a total amount of the book order.The Ajax script and html section are as follows:

     <script language="JavaScript">
   $(document).ready(function() {
   $("form").mouseout( function() {
      // get field value

      var qtyVal = $('#qty').val();
      // use HTTP GET get ajax 
        $.ajax({
        type: 'GET',
        url:  'getSunBody.php',
        data: { qty : qtyVal,
                 }, 
        success: function(data) {
           //get xml value
                    $('#result').html($(data).find('qty').text()); 
           $('#result1').html($(data).find('caution').text());   

        } 
      });    
      return false;
   });
});
</script>


<body>
Total price:<div id="result" class="box" style="height=350px;"></div><div id="result1" class="box" style="height=350px;"></div>
<form>
  <p>
    <label>quantity: </label>
    <input type="text" id="qty" name="qty"/> 
    <br/>
       <input type="submit" value="submit">
    total price:</p>
  <p>&nbsp;</p>
</form>

和作为XML下面的PHP脚本也能正常工作与上面的Ajax请求:

And the following php script serving as xml also works fine with above ajax request:

<?php
// XML document
header("Content-Type: text/xml");
header("Content-Type:text/html; charset=utf-8");
// get field values
$qty = (isset($_POST["qty"]) ) ? $_POST["qty"] : $_GET["qty"];

echo "<?xml version=\"1.0\" ?>";

echo "<datetime>"; 
echo "<qty>" . $qty*100 . "</qty>";

$total=$qty*100;
if ($total==0)
    echo "<caution>"."please input number!"."</caution>";
    else if ($total<=500)
    echo "<caution>"."you shoud buy more!"."</caution>";
    echo "";

echo "</datetime>";

?>

然而,当我结合上面的脚本我的购物车foreach循环,它不工作和AJAX脚本未能从表单输入区域的变量。我不知道这是否是一个变量范围的问题(全局或局部)?还是别的什么?

However when I combine the above scripts with my shopping cart foreach loops, it doesn't work and the ajax script failed to get variables from the form input area. I don't know if it is a variable scope issue (globals or local)? or anything else?

以下是总的剧本,我想固定:

The following is the total script I would like to fixed with:

<script language="JavaScript">
$(document).ready(function() {
   $("form").mouseout( function() {
      // get value from the form

      var qtyVal = $('#qty').val();
      // get 
        $.ajax({
        type: 'GET',
        url:  'getSunBody.php',
        data: { qty : qtyVal,
                 }, 
        success: function(data) {
           // get XML value
           $('#result').html($(data).find('qty').text()); 
           $('#result1').html($(data).find('caution').text());   

        } 
      });    
      return false;
   });
});
</script>
</head>

<body>
<table border="1" align="center">
<tr>
  <th>no</th>
  <th>name</th>
  <th>price</th>
  <th>qty</th>
  <th>update</th>
</tr>
<?php
foreach( $_SESSION["psn"] as $i => $data ){
?>
<form action="sessionCartUpdate.php">
<input type="hidden" name="psn" value="<?php echo $_SESSION["psn"][$i];?>">
<tr>
  <td><?php echo $_SESSION["psn"][$i];?></td>
  <td><?php echo $_SESSION["pname"][$i];?></td>
  <td><?php echo $_SESSION["price"][$i];?></td>
  <td><input type="text" id="qty" name="qty" value="<?php echo $_SESSION["qty"][$i];?>"></td>
  <input type="submit" name="qty" 
  <td><input type="submit" name="btnUpdate" value="update" />
      <input type="submit" name="btnDelete" value="delete" />
      </td>
</tr>
</form>
<?php
}
?>
<tr><td colsan="5">total amount:<div id="result" class="box" style="height=350px;"></div><div id="result1" class="box" style="height=350px;"></div></td></td>
</table>
<p><a href="sessionProdList.php">continue to shop</a>
<p><a href="sessionCartToDb.php">Put your order</a>
</body>
</html>

我会很感激,如果任何人都可以提供实物或可能的建议或意见?我的目标是把不同的数字(变量)在输入区域(名称或ID为数量)throught采用Ajax来获得价格的总额,并显示结果的DIV框(ID =结果或RESULT1)。

I would be very grateful if anyone can offer kind or possible suggestion or advice?My goal is to put different number (variables) in the "input area" (name or id as "qty") throught the using of ajax to get a total amount of price and show the result in the div box (id="result" or "result1").

推荐答案

您应该替换 ID 属性与班级,因为ID应该是在DOM,并使用独特的类,你可以做一个循环来获得的项目数量都在车

You should replace the id attribute with class because id is supposed to be unique in the dom and using class you can do a loop to get all quantities of the items in the cart

另一件事我注意到,您所做的个人形式的foreach项的车应该有一个表具有多个字段,也删除此行&LT;输入类型=提交名称=数量这doesent有道理

Another thing i have noticed that you have made the an individual form foreach item in the cart there should be one form having the multiple fields,also remove this line <input type="submit" name="qty" it doesent makes sense

<form action="sessionCartUpdate.php">
<?php
foreach( $_SESSION["psn"] as $i => $data ){
?>

<input type="hidden" name="psn" value="<?php echo $_SESSION["psn"][$i];?>">
<tr>
  <td><?php echo $_SESSION["psn"][$i];?></td>
  <td><?php echo $_SESSION["pname"][$i];?></td>
  <td><?php echo $_SESSION["price"][$i];?></td>
  <td><input type="text" class="qty" name="qty[]" value="<?php echo $_SESSION["qty"][$i];?>"></td>

  <td><input type="submit" name="btnUpdate" value="update" />
      <input type="submit" name="btnDelete" value="delete" />
      </td>
</tr>

<?php
}
?>
</form>

<script language="JavaScript">
$(document).ready(function() {
   $("form").mouseout( function() {
var qtyVal =0;
$( ".qty" ).each(function() {
  qtyVal =qtyVal + parseInt($(this).val()); 
});      
      // get 
        $.ajax({
        type: 'GET',
        url:  'getSunBody.php',
        data: { qty : qtyVal,
                 }, 
        success: function(data) {
           // get XML value
           $('#result').html($(data).find('qty').text()); 
           $('#result1').html($(data).find('caution').text());   

        } 
      });    
      return false;
   });
});
</script> 

这篇关于获得从PHP foreach循环阿贾克斯变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 02:12