Forum Moderators: coopster

Message Too Old, No Replies

Sending variables from one page to another with checkboxes

PHP Form checkbox issues

         

var123

7:01 am on Dec 21, 2008 (gmt 0)

10+ Year Member



I am having an issues trying to send information gathered from a MySql query to another page. My 3 pages are as follows: page 1 = search, page 2 = results with checkboxes for each row, page 3 = refined results.

I don't have any problems with the searching part or looping of the search to display multiple results. The problem is when I select a checkbox and try to get the results from the 2nd page onto the 3rd. I want the user to be able to select several results from the 2nd page and have them show on the 3rd page.

Here is some of the code from the 2nd (sql result) page :

$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUM_ROWS($result);

if ($numberOfRows==0) {
?>

Sorry. No records found !

<?
}
else if ($numberOfRows>0) {

$i=0;
?>

<body>
<form method="POST" action ="show.php">
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="0" WIDTH="100%">
<tr>
<td>Bedrooms</td>
<td>Bathrooms</td>
<td>Price</td>
<td>Address</td>
<td>City</td>
<td>Add</td>
</tr>

<?
while ($i<$numberOfRows)
{

if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }

$bed = MYSQL_RESULT($result,$i,"bed");
$bath = MYSQL_RESULT($result,$i,"bath");
$price = MYSQL_RESULT($result,$i,"price");
$address = MYSQL_RESULT($result,$i,"address");
$city = MYSQL_RESULT($result,$i,"city");
$add = '<input type ="checkbox" name="cb[]" value="<? echo $k++; ?>">';
$fName = MYSQL_RESULT($result,$i,"fName");
$lName= MYSQL_RESULT($result,$i,"lName");
$phone= MYSQL_RESULT($result,$i,"phone");

?>

<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName" value="<? echo $fName; ?>" />
<input type="hidden" name="lName" value="<? echo $lName; ?>" />
<input type="hidden" name="phone" value="<? echo $phone; ?>" /></TD>
</TR>

<?
$i++;

} // end while
?>
</TABLE>

<input name="Submit" type="submit" value="Showing Agenda" />

</form>

<?
} // end of if numberOfRows > 0
?>

</body>

And Here is the code from page 3 (show.php):

<body>
<table cellpadding="3" cellspacing="0" width="100%" border="0">
<tr>
<td>Bedrooms</td>
<td>Bathrooms</td>
<td>Price</td>
<td>Address</td>
<td>City</td>
<td>Name</td>
<td>Contact</td>
</tr>

<?
foreach ($_POST['cb'] as $k){
$bed = $_POST['bed']['$k'];
$bath = $_POST['bath']['$k'];
$price = $_POST['price']['$k'];
$address = $_POST['address']['$k'];
$city = $_POST['city']['$k'];
$fName= $_POST['fName']['$k'];
$lName= $_POST['lName']['$k'];
$name= $fName. ' ' . $lName;
$phone= $_POST['phone']['$k'];
?>

<tr>
<TD nowrap><? echo $bed; ?></TD>
<TD nowrap><? echo $bath; ?></TD>
<TD nowrap><? echo $price; ?></TD>
<TD nowrap><? echo $address; ?></TD>
<TD nowrap><? echo $city; ?></TD>
<TD nowrap><? echo $name; ?></TD>
<TD nowrap><? echo $phone; ?></TD>
</tr>
<?
;}
?>
</table>

</body>

Right now its not displaying properly at all. I had it only display the last record before no matter which checkbox was selected. But I need it to display a row for each checkbox selected.

I'm sorry, I am new to PHP and would appreciate any help.

andrewsmd

4:02 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your post will only work on the page that the form is submitted from. What I mean is, when the user submits the form on page 2, it can only access the results on page 2. Going to page three will clear those. What you can do is set some session variables and pull them on page three. Here is some psuedo
<?php
//start the session
session_start();

//if they clicked a checkbox
for(all of the checkboxes in your form){

if(isset($_POST['valueOfCheckBox'])){
$_SESSION['valueOfCheckBox'] = true;
}//if

}//for

?>
You need to run that on a submit and run it for every checkbox.
It looks like to me that you are naming your checkboxes on an increment. I would recommend putting in a little more code and naming them with text values (it will make identifying bed, bathrooms, etc. easier if the value is bed)
Now in page 3 you would have to check if the session variables are set
so basically
if($_SESSION['valueOfCheckBox']){
//code to print out that part of the table
}//if
You need to do that for all of your checkboxes again in page 3.
Let me know if you have any problems.

var123

4:20 am on Dec 23, 2008 (gmt 0)

10+ Year Member



Thank you for your reply. How I want the checkboxes to work is like this: Whenever a record is pulled from the database, I add a checkbox into the html row. If that box is checked, I want the info plus a little more details to show in the 3rd page.

I don't know how I can make the code work for all the possible checkboxes because the number of checkboxes that appear on page 2 is a result from a sql query. So I tried incremented and looping the variables from page 2.

Like I said in my first post, I am new to PHP, so please forgive me for being ignorant. But I tried your code and I got a parsing error. So I guess I couldn't understand what you were trying to say with some of it. Sorry for being a n00b lol.

Can I post a link to the site on this forum? I don't wanna break any rules. Maybe if you saw the whole thing it might make more sense to you what I am trying to do.

Thanks again for your reply, I still am gonna try to mess around for a little bit with what you wrote.

Andy1991

4:50 am on Dec 23, 2008 (gmt 0)

10+ Year Member



I think it is because you are assuming the postdata for all of the inputs as array data types when infact you are only specifiying the cb postdata as an array type, so when you post the data you are just overwriting the values in the $_POST superglobal array.

<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName" value="<? echo $fName; ?>" />
<input type="hidden" name="lName" value="<? echo $lName; ?>" />
<input type="hidden" name="phone" value="<? echo $phone; ?>" /></TD>
</TR>

I think this should work.

<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed[<?= $k; ?>]" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath[<?= $k; ?>]" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price[<?= $k; ?>]" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address[<?= $k; ?>]" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city[<?= $k; ?>]" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName[<?= $k; ?>]" value="<? echo $fName; ?>" />
<input type="hidden" name="lName[<?= $k; ?>]" value="<? echo $lName; ?>" />
<input type="hidden" name="phone[<?= $k; ?>]" value="<? echo $phone; ?>" /></TD>
</TR>

<?

foreach ($_POST['cb'] as $k){
$bed = $_POST['bed']['$k'];
$bath = $_POST['bath']['$k'];
$price = $_POST['price']['$k'];
$address = $_POST['address']['$k'];
$city = $_POST['city']['$k'];
$fName= $_POST['fName']['$k'];
$lName= $_POST['lName']['$k'];
$name= $fName. ' ' . $lName;
$phone= $_POST['phone']['$k'];
?>

var123

3:06 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



Thank you for your reply Andy1991. Unfortunately it didn't work for me either. I will keep trying to play around with it and see if it works. If anyone has any other possible solutions, I am open to try them. I am really stuck like chuck lol. Thanks and Merry Christmas!

Mahabub

5:06 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



var123 WELCOME AT WEBMASTERWORLD

$add = '<input type ="checkbox" name="cb[]" value="<? echo $k++; ?>">';

This Lines Have Syntax error for php. Actually not error but it will not print the value of $k. It will be zero so you need to fix that.

<TD nowrap><input type="hidden" name="bed" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>

And input name for that case must be array. Like the below one

<TD nowrap><input type="hidden" name="bed[// Here u have to use some value]" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>

Think you will understand for more clarification use the below code-------------------------------

<?
$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUM_ROWS($result);

if ($numberOfRows==0) {
?>

Sorry. No records found !

<?
}
else if ($numberOfRows>0) {

$i=0;
?>

<body>
<form method="POST" action ="show.php">
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="0" WIDTH="100%">
<tr>
<td>Bedrooms</td>
<td>Bathrooms</td>
<td>Price</td>
<td>Address</td>
<td>City</td>
<td>Add</td>
</tr>

<?
while ($i<$numberOfRows)
{

if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }

$bed = MYSQL_RESULT($result,$i,"bed");
$bath = MYSQL_RESULT($result,$i,"bath");
$price = MYSQL_RESULT($result,$i,"price");
$address = MYSQL_RESULT($result,$i,"address");
$city = MYSQL_RESULT($result,$i,"city");
$add = '<input type ="checkbox" name="cb['.$i.']" value="'.$i.'">';
$fName = MYSQL_RESULT($result,$i,"fName");
$lName= MYSQL_RESULT($result,$i,"lName");
$phone= MYSQL_RESULT($result,$i,"phone");

?>

<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><input type="hidden" name="bed[<?=$i;?>]" value="<? echo $bed; ?>" /><? echo $bed; ?></TD>
<TD nowrap><input type="hidden" name="bath[<?=$i;?>]" value="<? echo $bath; ?>" /><? echo $bath; ?></TD>
<TD nowrap><input type="hidden" name="price[<?=$i;?>]" value="<? echo $price; ?>" /><? echo $price; ?></TD>
<TD nowrap><input type="hidden" name="address[<?=$i;?>]" value="<? echo $address; ?>" /><? echo $address; ?></TD>
<TD nowrap><input type="hidden" name="city[<?=$i;?>]" value="<? echo $city; ?>" /><? echo $city; ?></TD>
<TD nowrap><? echo $add; ?><input type="hidden" name="fName[<?=$i;?>]" value="<? echo $fName; ?>" />
<input type="hidden" name="lName[<?=$i;?>]" value="<? echo $lName; ?>" />
<input type="hidden" name="phone[<?=$i;?>]" value="<? echo $phone; ?>" /></TD>
</TR>

<?
$i++;

} // end while
?>
</TABLE>

<input name="Submit" type="submit" value="Showing Agenda" />

</form>

<?
} // end of if numberOfRows > 0
?>

</body>

and the above one should work if u dunt make any mistake in show.php file.

Thanks
MAHABUB

var123

5:53 pm on Dec 27, 2008 (gmt 0)

10+ Year Member



Thanks for the reply and for the warm welcome, Mahabub. I entered in your code, and it seems to work ok, but now I am not getting any results on the show.php page. Do you have any idea how I should go about looping the results from the selected checkboxes on the 3rd (show.php) page?

Mahabub

4:55 pm on Dec 28, 2008 (gmt 0)

10+ Year Member



Dear var123,

Sorry for late reply. Actually was on vacation on the largest sea beach of the world. Try the below one it should work and let us know.

<?php

foreach ($_POST['cb'] as $k){
echo $bed = $_POST['bed'][$k];
echo "<br/>";
echo $bath = $_POST['bath'][$k];
echo "<br/>";
echo $price = $_POST['price'][$k];
echo "<br/>";
echo $address = $_POST['address'][$k];
echo "<br/>";

}
?>

Thanks
Mahabub

var123

2:57 am on Dec 31, 2008 (gmt 0)

10+ Year Member



Hey Mahabub,

Hope you had a nice vacation! Well I'm still having a problem. The page isn't displaying anything now. Do you have any other possible solutions?

thx,

var123

Mahabub

7:00 am on Dec 31, 2008 (gmt 0)

10+ Year Member



Dear var123,

please put only this on ur show.php file.

<?php
print_r($_POST);
?>

and take a look at the post array. Also look at the source of your search.php file I mean the HTML source and take a look that php script generated output correctly.

Also check that that short_open_tag are on in your php.ini
please let us know about your final output.

Thanks
Mahabub