I have a javascript method which is doing some calculation. At the end, I want to store it in a hidden field. Here is the method:
<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>
I have checked here, everything is working fine. The last alert is printing the correct value.
Here is the form, in which there is a button, for which I want to submit fields on submit through method 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>
My query string shows:
http://localhost/blazorange/Customer/CalculateTotal.php?hdnQuantityArray=&CheckOut=Submit
I want this hdnQuantityArray to have some value in query string.
P.S. this javascript method is called from another form’s submit button in the same file. And this form is a PHP form.
Here is the code for the other form’s submit button:
<input type="submit" value=" Total " id="total" onClick="return getQuantity()">
EDIT:
<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('hidden')should be
document.getElementById('hdnQuantityArray')There’s no element with an id of “hidden” in the code you posted.
EDIT:
You’re saying the JS only runs when the CartForm is submitted? That reloads the page, right? When the page reloads, hdnQuantityArray is empty. So when you submit qForm, there’s nothing there, because the page loaded with a blank field, and the JS didn’t run again. Does that sound right?