php - 隐藏字段值未出现在查询字符串中

标签 php javascript json hidden-field

我有一个正在做一些计算的 javascript 方法。最后,我想将它存储在一个隐藏字段中。方法如下:

<script>
function getQuantity(){
 var count = document.getElementById('hidden').value;
 var Quantity=new Array();
 var i=0;
 for(i=0; i<count; i++)
 {
    Quantity[i]=document.getElementById(i).value;
 }
var myJSONQuantity = JSON.stringify(Quantity,'');
alert(myJSONQuantity);

document.qForm.getElementById('hdnQuantityArray').value = myJSONQuantity;
alert(document.qForm.getElementById('hdnQuantityArray').value);
}
</script>

我在这里检查过,一切正常。最后一个警报是打印正确的值。 这是表单,其中有一个按钮,我想通过 GET 方法在提交时提交字段:

<form  name="qForm" method="GET" action="CalculateTotal.php">
    <input type="hidden" id="hdnQuantityArray" name="hdnQuantityArray">
    <input type="submit" value="CheckOut" id="CheckOut" name="CheckOut">
</form>

我的查询字符串显示:

http://localhost/blazorange/Customer/CalculateTotal.php?hdnQuantityArray=&CheckOut=Submit

我希望这个 hdnQuantityArray 在查询字符串中有一些值。

附言从同一文件中另一个表单的提交按钮调用此 javascript 方法。而这个表格是一个PHP表格。 这是另一个表单的提交按钮的代码:

<input type="submit" value=" Total " id="total" onClick="return getQuantity()">

编辑:

<table border=1>
    <form id="CartForm">    
        <tr>
            <td>
                <h2> <font color='Grey'>Item Name</font> </h2>
            </td>
            <td>
                        <h2> <font color='Grey'>Item Price</font> </h2>
            </td>
            <td>
                    <h2> <font color='Grey'>Quantity</font> </h2>
            </td>
        </tr>
    <?PHP

        /*     Displaying the total and purchased cart's items    */

            $j=0;
            $temp=new Item();
            while(isset($ItemsArray[$j])) {
        ?>
        <tr>
                        <?PHP

                    if (is_string($ItemsArray[$j])) {
                $temp=unserialize($ItemsArray[$j]);
                 }

                $ItemName=$temp->getItemName();
                $Price=$temp->getPrice();
            ?>

            <td>
                <font color='Black'><?PHP echo $ItemName; ?>
            </td>

            <td>
                <font color='Black'><?PHP echo $Price;  ?></font>
            </td>

            <td>
                <input type="text" id="<?PHP echo $j;?>"/> 
            </td>
            </tr>


        <?PHP $j++; }?>


        <table>
            <input type="button" value=" Total " id="total" onClick="getQuantity()">
            <input type="hidden" value="<?PHP echo $j; ?>" id="hidden">

        </form>
    </table>

最佳答案

document.getElementById('隐藏')

应该是

document.getElementById('hdnQuantityArray')

您发布的代码中没有 ID 为“隐藏”的元素。

编辑:

您是说 JS 仅在提交 CartForm 时运行?重新加载页面,对吗?当页面重新加载时,hdnQuantityArray 为空。所以当你提交qForm时,什么都没有,因为页面加载了一个空白字段,JS没有再次运行。听起来对吗?

关于php - 隐藏字段值未出现在查询字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10840962/

相关文章:

java - 将 Java 映射转换为 JSON

javascript - 最短路径算法js报错

php - 获取组中 ID 最高的值 - MySQL

php - 数组和循环管理

javascript - 如何在Jquery中编辑元素的数据属性

javascript - 从输入框中使用 jquery 在 ajax 中传递参数

javascript - 在 jQuery 中,使用 $.extend(),这是多余的吗?

php - 错误 : No available formula for php54-mcrypt on Mac OS X Mavericks

php - 如何在购物车中传递自定义变量?

json - 在 Ruby 中解析非常大的 JSON 文件的正确方法是什么?