Forum Moderators: coopster

Message Too Old, No Replies

php variable not passing into HTML table.

         

indiguy

2:03 am on Oct 19, 2009 (gmt 0)

10+ Year Member



echo "<table width=100% align=\"center\">";
// return results line by line
while ( $row = mysql_fetch_array($result) )
{
// pass database ($row['blah'];) values into var's.. $id= etc. The '$id' is used to uniquly identify
// each returned database row for identifing the values seperatly for integrity.
$id=$row['id'];
$price=$row['tea_price'];
$choice= $row['tea_choice'];
$desc= $row['tea_desc'];
// display the database results to the user. The submit button sends valuse to the js function.
echo "<tr>";
echo "<input type=\"hidden\" name=\"_id_$id\" value=\"$id\">"; ?>
<tr><td width="20%" align="center" colspan="4"><font size="3" color="#FF0000">$ <? echo $price ?></font></td></tr>

<tr><td align="center">Quantity: <input type="text" size=2 maxlength=3 name="QUANTITY" onChange='this.value=CKquantity(this.value)' value="1" >
<input type="button" value=' Add to Cart ' onClick='AddToCart(this.form)' ></tr></td>
<input type="hidden" name="PRICE" value="<? echo $price; ?>" >
<input type="hidden" name="NAME" value="<? echo $choice; ?>" >
<input type="hidden" name="ID_NUM" value="<? echo 'Num_".$id."'; ?>" >
<?php
}//while
echo "</tr>";
echo "</table>";

=======================================================

thats the code, ive also tested <? echo "blah"; ?> within the html table and nothing echos.
It seems to get blocked by the html table row tags or something.
The table opening and closing tags are out side the while loop and are in php.
these $price variables needs to be in the html as shown in the code, its for a shopping cart im integrating, which need this html.

Any suggestions or help plz?

Thanks

TheMadScientist

2:11 am on Oct 19, 2009 (gmt 0)

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



It looks like something silly like:
<?php echo $price; ?>

rather than:
<? echo $price ?>

If that doesn't solve the issue, just wrap the whole thing into the php...

echo '<tr>';
echo '<input type="hidden" name="_id_'.$id.'" value="'.$id.'">' .
'<tr><td width="20%" align="center" colspan="4">' .
'<font size="3" color="#FF0000">$ '.$price.'</font></td>' .
'etc.';

NOTE: Switched to single-quotes so variables will not be expanded...

It makes concatenation necessary, but is IMO much easier to read / work with, and according to the benchmarking in the library, accumulation + concatenation is faster for parsing than expanding variable within quotes or switching in and out of html / php.

Personally, I store everything in a variable and echo it when the script is finished...

$table = '<tr>';
$table .= '<input type="hidden" name="_id_'.$id.'" value="'.$id.'">' .
'<tr><td width="20%" align="center" colspan="4">' .
'<font size="3" color="#FF0000">$ '.$price.'</font></td>' .
'etc.';

echo $table;

indiguy

2:44 am on Oct 19, 2009 (gmt 0)

10+ Year Member



thanks very much... lol missing <?PHP

indiguy

3:15 am on Oct 19, 2009 (gmt 0)

10+ Year Member



JS:
function AddToCart(thisForm) {

iNumberOrdered = 0; //Integer number of products already ordered

iNumberOrdered = GetCookie("NumberOrdered");
iNumberOrdered++;

if ( iNumberOrdered > 12 )
alert("I'm Sorry, your cart is full, please proceed to checkout.");
else {
dbUpdatedOrder = thisForm.QUANTITY.value + "¦"
+ thisForm.PRICE.value + "¦"
+ thisForm.ID_NUM.value + "¦"
+ thisForm.NAME.value;

NewOrder = "Order." + iNumberOrdered;
SetCookie (NewOrder, dbUpdatedOrder, null, "/");
SetCookie ("NumberOrdered", iNumberOrdered, null, "/");

notice = thisForm.QUANTITY.value + " "
+ thisForm.NAME.value
+ " added to your shopping cart.";

alert(notice);
}
}

------------------------------------------------------------
PHP:

<tr><td align="center">Quantity: <input type="text" size=2 maxlength=3 name="QUANTITY" onChange='this.value=CKquantity(this.value)' value="1" >
<input type="button" value=' Add to Order ' onClick='AddToCart(this.form)' >
<input type="hidden" name="PRICE" value=<?php echo $price ;?> >
<input type="hidden" name="NAME" value=<?php echo $choice ;?> >
<input type="hidden" name="ID_NUM" value=<?php echo "Num_".$id.""; ?> ></tr></td>
-----------------------------------------------------------

When i click on the Add to Order button, the JS alert says "undefined undefined added to cart."
where undefined should be $price (PRICE) and $choice.
Ive proved that $price and $choice (NAME) are loaded variables (working), however, its undefined in the JS.
I believe the JS is not liking the php $variables.

Am i correct to think this? if so, what can i do to feed the JS with these php dynamic variables?

Thanks for your help :)

TheMadScientist

4:56 am on Oct 19, 2009 (gmt 0)

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



The first thing I see, off the top of my head, is:

<input type="hidden" name="PRICE" value=<?php echo $price ;?> >
<input type="hidden" name="NAME" value=<?php echo $choice ;?> >
<input type="hidden" name="ID_NUM" value=<?php echo "Num_".$id.""; ?> ></tr></td>

I think it should be:
<input type="hidden" name="PRICE" value="<?php echo $price ;?>" >
<input type="hidden" name="NAME" value="<?php echo $choice ;?>" >
<input type="hidden" name="ID_NUM" value="<?php echo "Num_".$id."";" ?> ></tr></td>

I might need some more detail, unless someone sees something else and this doesn't fix the issue...

indiguy

9:44 am on Oct 19, 2009 (gmt 0)

10+ Year Member



i played with the form tags, and added new form tags in this loop. now it works

thanks