0

I tried lot of way to pass the checkbox values from page 1 to page 2. But I only get successes on fruit id, how can pass the fruit charges.

// how can I pass room charges"$result[room_charges]" to page 2

// page 1 

$sql=mysql_query("select * from fruit");
while ($result = mysql_fetch_assoc($sql)) {
  echo  "<label> <input type=checkbox name=fruit[] value=$result[fruit_id] id=Test_0 > $result[fruit_name] |  $$result[fruit_charges] </label> ";
  echo "<br>";
  
//page 2

// how to replace from page 1 $result[fruit_charges]  the charges to f_pay
// like room_id works


$room=$_POST['room'];

foreach ($fruit as $fruitname)
{
$sql="INSERT INTO reserve (f_aid, f_name, f_pay) VALUES 
('$fid', '$fruitname', '10')";
if (!mysql_query($sql,$conn))
  {
  die('Error: ' . mysql_error());
  }
}
3
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by nukabolhi
0

in your page change your input checkbox

<input type=checkbox name=fruit[] value=$result[fruit_id] id=fruit >

$room=$_POST; //look your code here

in your page 1 is name=fruit[]

it should

$room=$_POST;
$count = count($room);

for ($i=0; $i<$count; $i++)
{
$sql="INSERT INTO reserve (f_aid, f_name, f_pay) VALUES
('$fid', '$fruitname', '10')";
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
}

1

Make the following changes to the code

Line7: Encapsulate all the variables and correct the $$result[fruit_charges] the code would be

echo  "<label> <input type='checkbox' name='fruit[]' value='{$result[fruit_id]}' id='Test_0' > {$result[fruit_name]} |  {$result[fruit_charges]} </label> ";

Line 17: Add in the following line

$fruit=$_POST['fruit'];

This would pick up the fruit array from the previous page and store it in the $fruit array which can be pulled through the foreach.

Edited by sudeepjd: n/a

0

in your page change your input checkbox

<input type=checkbox name=fruit[] value=$result[fruit_id] id=fruit >

$room=$_POST; //look your code here

in your page 1 is name=fruit[]

it should

$room=$_POST;
$count = count($room);

for ($i=0; $i<$count; $i++)
{
$sql="INSERT INTO reserve (f_aid, f_name, f_pay) VALUES
('$fid', '$fruitname', '10')";
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}
}

Thanks, its a typo error. I mean how to insert fruit charges, not the $fruitname.

0

If I understand your question correctly you want to pass both the fruit id as well as the fruit charges to the second page.

In this case, you will have to create a hidden field like so on your form

<input type='hidden' name='fruitcharge[]' value='$result[fruit_charges]' />

The above would be inside the while loop so for every fruit you will have one of these hidden boxes which will have the charge of the fruit inside it. When submitting the form this will also go in as an array and can be pulled the similar was as you are now pulling the fruit_id's

$fruitcharge=$_POST['fruitcharge'];

Now however instead of using the foreach loop use the for loop like ryan311 has shown.

0

Make the following changes to the code

Line7: Encapsulate all the variables and correct the $$result[fruit_charges] the code would be

echo  "<label> <input type='checkbox' name='fruit[]' value='{$result[fruit_id]}' id='Test_0' > {$result[fruit_name]} |  {$result[fruit_charges]} </label> ";

Line 17: Add in the following line

$fruit=$_POST['fruit'];

This would pick up the fruit array from the previous page and store it in the $fruit array which can be pulled through the foreach.

I test your code. but it gives some error

Notice: Use of undefined constant fruit_id -

The fruit_id is ok, but how can I pass separately each value to insert to database

1

Since you want to transfer 2 variables across the pages with one check box the easiest way to do it is to put both the values as in value1_value2 in the value parameter of the checkbox.

<input type='checkbox' name='fruit[]' value='{$result[fruit_id]}_{$result[fruit_charges]}' id='Test_0' > {$result[fruit_name]} | {$result[fruit_charges]} </label> "

from page 2 call the array fruit which was sent over form page 1 and split it up using the explode function. You would then get both the variables as such

$fruit=$_POST['fruit'];
$ft=explode("_",$fruit);

You can then use $ft[1] and $ft[2] in the required locations.

Votes + Comments
great tips
0

Thanks.

'{$result[fruit_id]}'

Is it possible to use fruit_id array as session

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.