Hey guys i have the following code below for a shopping cart

just wondering what this hidden input text would be used for and the echo at the end of it.


I know it reads from a text file with : as delimeters and it as an ID for each product.

If you could assist me that would be great

<input name="prodId" type="hidden" value="<? echo $['idRoute']?>"></td>
<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>

<?php

?>
<body>
<form id="fSample" name="fSample" method="post" action="products.php">
<select name="product">
  <option value="mobiles">Mobiles</option>
  <option value="radios">Radios</option>
  <option value="navigators">Navigators</option>
  <option value="guitars">Guitars</option>

</select>
<input type="submit" name="submit" id="submit" value="Show products">
<input name="prodId" type="hidden" value="<? echo $['idRoute']?>"></td>
</form>


<?php
if (isset($_POST['submit']))
{
  echo "<br/><br/>";
  echo "<table border='1'>";
  echo "<tr><th>Name</th><th>Description</th><th>Price</th></tr>";
  $file = "products.txt";
  $fp = fopen($file, 'r');
  //$counter=0;

  while(!feof($fp))
  {
  $data = fgets($fp);
  $chunk = explode(":",$data);

  if($_POST['product']==$chunk[0])
  {
  echo "<tr><td>$chunk[1]</td><td>$chunk[2]</td><td>$chunk[3]</td><td><a href='$chunk[4]'><input type='button' value='View details'/></a></td></tr>";
  }
  //$counter = $counter + 1;
  //echo $data;
  }

fclose($fp);



 echo "</table>";
}
?>
</body>
</html>

Recommended Answers

All 6 Replies

hidded field are used to for additional information , that user may not need to enter
or sometime it is used to store some parameters passed by previous page,

that will be used along with other input elements in the processing page.

Okay what i dont understand is how its passing the hidden variable below into the
other php pages.

<input name="prodId" type="hidden" value="<? echo $['idRoute']?>"></td>

*******listproductdetails.php*******

<?php
session_start();
?>
<html>
<body>
<?php


if (isset($_POST['submit']))
{

  if($_SESSION['item']==$_POST['h1'])
{
  $_SESSION['qty'] = $_SESSION['qty'] + 1;
}
else
{
  $_SESSION['item'] = $_POST['h1'];
  $_SESSION['price']= $_POST['h2'];
}

 $_SESSION['itemname'][$_SESSION['item']] = $_SESSION['item'];
 $_SESSION['itemqty'][$_SESSION['item']] = $_SESSION['qty'];
 $_SESSION['itemprice'][$_SESSION['item']] = $_SESSION['price'];

}




?>
<table>
<tr>
<td>
<img src="eric.jpg"/>
</td>
<td>This product is on clearance. New stock needs to come in and all the old stock needs to go. Hurry till offers end!!</td></tr>
<tr><td><form action="erricsson.php" method="post"><input type="submit"
name="submit" value="Add to cart"/><input type="hidden" name="h1"
value="k508i" /><input type="hidden" name="h2" value="400"/></form> </td></tr>
</table>

<a href="cart.php">View your cart</a>
</body>
</html>

*********** add cart / delete product page

<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>

<body>


<?php


  echo "The shopping cart looks as follows:--";

  if(isset($_POST['submit']))
  {

  $itemname = $_POST['h1'];

  //echo $_SESSION['itemname'][$itemname];
  unset($_SESSION['itemqty'][$itemname]);
  unset($_SESSION['itemprice'][$itemname]);
  unset($_SESSION['itemname'][$itemname]);
  }

  echo "<br/><br/>";
  echo "<table border='1'>";
  echo "<tr><th>Name</th><th>Quantity</th><th>Price</th></tr>";
  foreach($_SESSION['itemname'] as $key=>$value)
{

echo
'<tr><td>'.$_SESSION['itemname'][$key].'</td><td><input
type="text" name="t1" value='.$_SESSION['itemqty'][$key].'></td><td>'.$_SESSION['itemprice'][$key].'</td><td><form
id="f1" method="post" name="f1"><input type="submit" name="submit" value =
"delete"><input type="hidden" name="h1" value='.$key.'></td></tr>';

}




  echo "</table>";

?>
</body>
</html>

its not like below or above,
The important thing is that input elements must belong to some form element like

<form name=frm id=frm action='processpage.php' method='post'>
<input type=text value='' name=t1>
<input type=text value='' nam2=t2>
<input type=hidden value='1223' name=h1>
<input type=submit value='Proceed' name=submit1 >
</form>

In above form code, when user press submit, the form values like t1, t2 and h1 will be submitted to processpage.php

ok so what happens to the value='1223'.

Usually that is visible but not in a hidden form.

but what i dont get is

<? echo $['idRoute']?>

in the input field how is it passing that information when its using an echo.

And i cannot see used anywhere else in any of the other php pages

if form method is post then in processing page u can access submitted form values bye following way

echo $_POST['t1'];
echo $_POST['t2'];
echo $_POST['h1'];

in case of get method

echo $_GET['t1'];
echo $_GET['t2'];
echo $_GET['h1'];
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.