Hi All,

I am new to PHP and I got stuck on how to display a variable "txtQty" in my PHP COde. What happen is user will key in a number and I just try to display the number in the same PHP program. Here is the program.

Tks in advance

<?php
if (!defined('WEB_ROOT')) {
    exit;
}

$product = getProductDetail($pdId, $catId);

// we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url
extract($product);
?> 
<table width="100%" border="0" cellspacing="0" cellpadding="10">
 <tr> 
  <td align="center"><img src="<?php echo $pd_image; ?>" border="0" alt="<?php echo $pd_name; ?>"></td>
  <td valign="middle">
<strong><?php echo $pd_name; ?></strong><br>
Price : <?php echo displayAmount($pd_price); ?><br>
Posting : <?php echo displayAmount($pd_posting); ?><br>
Quantity : <input [COLOR="Red"]name="txtQty"[/COLOR] type="text" id="txtQty" size="3"  class="box" onKeyUp="checkNumber(this);"> <br>

<?php
// if we still have this product in stock
// show the 'Add to cart' button
[COLOR="Red"]echo "This is the qty : " . $txtQty;
echo "This is the 2nd line qty : " . $_REQUEST["txtQty"];
echo "This is the 3nd line qty : " . $_POST["txtQty"];[/COLOR]
if ($pd_qty > 0) {
?>
<input type="button" name="btnAddToCart" value="Add To Cart &gt;" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton">
<?php
} else {
    echo 'Out Of Stock';
}
?>
  </td>
 </tr>
 <tr align="left"> 
  <td colspan="2"><?php echo $pd_description; ?></td>
 </tr>
</table>

Keep in mind that PHP is running on the server while your HTML code (including the <input statement ) runs in the browser on the desktop / laptop / phone etc. The only way that the PHP program on the server gets to see the user input (unless you are using some sort of Ajax approach) is for a form to be submitted from the code in the browser with the PHP program as the "action". This sends the fields from the form to the server addressed to the program that is defined in the action. You haven't defined a form in your code even though you have some of the elements that could be contained within a form. Your button may trigger the program that is defined as $cart_url but it isn't going to pass txtQty to the server as a Get or Post variable unless you define a proper form.

Keep in mind that PHP is running on the server while your HTML code (including the <input statement ) runs in the browser on the desktop / laptop / phone etc. The only way that the PHP program on the server gets to see the user input (unless you are using some sort of Ajax approach) is for a form to be submitted from the code in the browser with the PHP program as the "action". This sends the fields from the form to the server addressed to the program that is defined in the action. You haven't defined a form in your code even though you have some of the elements that could be contained within a form. Your button may trigger the program that is defined as $cart_url but it isn't going to pass txtQty to the server as a Get or Post variable unless you define a proper form.

Correct, he must defined form between input field. Look like this:

<?php
if (!defined('WEB_ROOT')) {
exit;
}

$product = getProductDetail($pdId, $catId);

// we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url
extract($product);
?> 
<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td align="center"><img src="<?php echo $pd_image; ?>" border="0" alt="<?php echo $pd_name; ?>"></td>
<td valign="middle">
<strong><?php echo $pd_name; ?></strong><br>
Price : <?php echo displayAmount($pd_price); ?><br>
Posting : <?php echo displayAmount($pd_posting); ?><br>
Quantity : <input name="txtQty" type="text" id="txtQty" size="3" class="box" onKeyUp="checkNumber(this);"> <br>

<?php
// if we still have this product in stock
// show the 'Add to cart' button
[I]echo "This is the qty : " . $txtQty; // 
echo "This is the 2nd line qty : " . $_REQUEST["txtQty"];[/I]
echo "This is the 3nd line qty : " . $_POST["txtQty"]; //just use $_POST because form defined in POST method
if ($pd_qty > 0) {
?>
<input type="button" name="btnAddToCart" value="Add To Cart &gt;" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton">
<?php
} else {
echo 'Out Of Stock';
}
?>
</td>
</tr>
<tr align="left">
<td colspan="2"><?php echo $pd_description; ?></td>
</tr>
</table> 
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.