Forum Moderators: coopster

Message Too Old, No Replies

php: post too late

         

haru_chan

4:53 pm on Nov 30, 2008 (gmt 0)

10+ Year Member



hi, I used javascript with my PHP code to transfer the value of textfield from popuop window to parent page after adding the value of two textfields (the one in the popup window & in the parent page)

the problem is when I click "Submit", I have to click it twice in order for the current value to be passed to the parent page, if I click only once it post the previous value:

here is the code:

<script langauge="javascript">
function post_value(){
opener.document.form.textfield2.value = document.form1.textfield.value;
val3=parseInt(document.form1.textfield.value)+ parseInt(opener.document.getElementById('listPrice1').value);
opener.document.getElementById('listPrice1').value=val3;
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<table>
<?
$sum=0;
do{
?>
<tr><td>
<? $cost=$row_product['cost'];?>
<input type="checkbox" name="pro[]" value="<? echo $cost;?>" <?php
$OK = isset($_POST['pro']) ? true : false;
if ($OK && in_array($cost, $_POST['pro'])) { ?>
checked="checked"
<?php }?>
/>
<? echo ($row_product['name']); ?>
</td>
<td>
<? echo $cost;?>
</td></tr>
<?
}while($row_product = mysql_fetch_assoc($product));

if(isset($_POST['Submit'])){
$pro=$_POST['pro'];
$how_many = count($pro);
$spec=" ";
for ($i=0; $i<$how_many; $i++) {

$sum=$sum+$pro[$i];
$spec .="1"; $insertSQL3 = mysql_query("INSERT INTO spec (spec) VALUES ('$spec')");

}
}
else {$spec .="0"; $insertSQL3 = mysql_query("INSERT INTO spec (spec) VALUES ('$spec')");}

?>

<input type="text" name="textfield" value="<? echo $sum; ?>">

</table>

<input type="submit" name="Submit" value="Submit" onClick="post_value()"/>

</form>
</body>
</html>

hope this is clear enough to describe my problem. Please Help!

phparion

3:03 pm on Dec 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<input type="text" name="textfield" value="<? echo $sum; ?>">

when page is loaded, you are assigning value using PHP, php is event dependent. now when you change the value in FORM FIELD you need to change the value in REAL time of this textfield to point to the new field, you can use onchange event etc. so when you submit through javascript, it reads the new typed values and not the old value which is assigned by PHP on page loading.