Everything seems fine, why doesn't test.php work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
magdi
Forum Newbie
Posts: 9
Joined: Sat Mar 13, 2004 4:36 am

Everything seems fine, why doesn't test.php work

Post by magdi »

Who can help?

All I get on my test.php page is a white page. Everything seems to be fine though when I look at the html source of my first page.
What am I doing wrong?
What I want is a listing of the products that have been checked on my first page (for now, I'd like to send a confirmation email later).

This is the first page:

<form method="get" action="test.php">

Code: Select all

<?php # Script 2 - lijst.php

//shows all the products that are still available

$page_title = 'look at the list';

//connect to the database
require_once ('../mysql_connect.php');

//make the query
$query = "SELECT lijst.l_id, lijst.l_prod, lijst.l_cat, lijst.l_prijs
FROM lijst
WHERE ((Not (lijst.l_sold_yn)=1)) ORDER BY lijst.l_cat, lijst.l_prod";

//run the query
$result = @mysql_query ($query);

//if it ran ok, display the records
if ($result)
{
	echo '<table align="left"
    cellspacing="2" cellpadding="2" width="90%" border="0">
    <tr bgcolor="#CCFFFF"> 
	<td align="center" width="20"><b><font face="Arial, Helvetica, sans-serif" size="2">id</font></b></td>
    <td align="center" width="200"><b><font face="Arial, Helvetica, sans-serif" size="2">product</font></b></td>
    <td align="center" width="200"><b><font face="Arial, Helvetica, sans-serif" size="2">waarvoor</font></b></td>
    <td align="center" width="80"><b><font face="Arial, Helvetica, sans-serif" size="2">prijs</font></b></td>
    <td align="center" width="150"><b><font face="Arial, Helvetica, sans-serif" size="2">kadootje(s) aanvinken</font></b></td>
    </tr>
  ';
 //fetch and print all the records 

  while ($row = mysql_fetch_assoc($result))
  { 
    ?><tr bgcolor="CCFFFF"> 
	<td align="left" <font face="Arial, Helvetica, sans-serif" size="1" ><?echo $row[l_id]; ?></td>
	<td align="left" <font face="Arial, Helvetica, sans-serif" size="1" ><?echo $row[l_prod]; ?></td>
    <td align="left" <font face="Arial, Helvetica, sans-serif" size="1" ><?echo $row[l_cat]; ?></td>
    <td align="right" <font face="Arial, Helvetica, sans-serif" size="1"><?echo $row[l_prijs]; ?></td>
    <td <div align="center"><input type="checkbox" name="<?echo $row['l_id'];?>"></td>
    </tr>
<?
} 
echo'</table>';

//Free up the resources
  mysql_free_result ($result);

} else {//if it did not run ok
  echo '<p>Oops! Mum made a technical error, please try again later</p>
        <p>'  .mysql_error() .'</p>';
}
//close the database
mysql_close();
?>
<input type="submit">
</form>
    </td>
  </tr>
</table>
<?php

This is the test.php page

Code: Select all

<? 
echo $HTTP_POST_VARS['chk[]'];
/*
$aantal = count($_POST['chk']); 
echo 'aangevinkt zijn:<br>'; 
for($i=0; $i<$aantal;$i++) 
{ 
echo $i .'----' . $_POST['chk'][$i] .'<br>'; 
} 
*/
?>
Thank you for reading and eventually answering this question.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this may or may not be related to the problem:

Code: Select all

<tr bgcolor="CCFFFF"> 
   <td align="left" <font face="Arial, Helvetica, sans-serif" size="1" ><?echo $row&#1111;l_id]; ?></td> 
   <td align="left" <font face="Arial, Helvetica, sans-serif" size="1" ><?echo $row&#1111;l_prod]; ?></td> 
    <td align="left" <font face="Arial, Helvetica, sans-serif" size="1" ><?echo $row&#1111;l_cat]; ?></td> 
    <td align="right" <font face="Arial, Helvetica, sans-serif" size="1"><?echo $row&#1111;l_prijs]; ?></td> 
    <td <div align="center"><input type="checkbox" name="<?echo $row&#1111;'l_id'];?>"></td> 
    </tr>
all your "<td"'s have no terminating ">" to start the cell. Most browsers will forgive the error though. Most of the array index references aren't quoted either.. Although that just creates warnings normally.

Regarding the blank page.. if your form start code is:
<form method="get" action="test.php">
as you say.. then $HTTP_POST_VARS will never have the data you are looking for. Switch method to post and it should work.. I think. :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What does this look like in the source? It seems like you probably need to change the name of the checkbox, maybe to something like:
* note that the forum software is filtering out the closing PHP tag for some reason, it should occur before the closing ]

Code: Select all

<input type="checkbox" name="chk[<?php echo $row['l_id']; ?>]" />
Then if you change your form method to POST (as feyd pointed out) then you can do this on test.php:

Code: Select all

// to see all of the posted variables:
echo '<pre>';
print_r($_POST);
echo '</pre>';
Mac
magdi
Forum Newbie
Posts: 9
Joined: Sat Mar 13, 2004 4:36 am

ok, it works

Post by magdi »

Yes! I got results. Thanks! The page shows me:
--------------
Array
(
[14] => on
[6] => on
[10] => on
)
--------------

So now I'm going to start the fight to figure out how to display the field name and how to update my database so that checked records will be marked as sold (l_sold = 1).

Tips are welcome, else I'll post -and ofcourse check if I can't find the answer in older posts - everytime I face a problem I can't resolve.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The only checkbox information that will get passed through is that for those that are selected (all the on's as you've got above). There are two approaches you can then take to use this data in the next query (note this assumes that l_id is the unique id for each record). If we use the array you got from the current form code you can do:

Code: Select all

// need all the key values from the array as these are the ids
$sold_items = array_keys($_POST['chk']);

// implode these numbers into a comma delimited string for use in the UPDATE query
$sold_items = implode(',', $sold_items);

// now the UPDATE query, with WHERE clause using IN() to ensure the
// right records are updated
$sql = "UPDATE lijst SET l_sold = 1 WHERE l_id IN($sold_items)";

// now you can run the query and your records should hopefully be updated
If you changed:

Code: Select all

<input type="checkbox" name="chk[<?php echo $row['l_id']; ?>]" />
to

Code: Select all

<input type="checkbox" name="chk[]" value="<?php echo $row['l_id']; ?>">
then you can just do

Code: Select all

$sold_items = implode(',', $_POST['chk']);
and then the SQL query as before.

Mac
magdi
Forum Newbie
Posts: 9
Joined: Sat Mar 13, 2004 4:36 am

Oops, something wrong

Post by magdi »

I just noticed it but the values are mixed up.
Instead of taking the key values (14,11,9,7,17,15 and 22) the script takes the nr of the array.
This way the correct records won't be updated.

How can I solve this?

----
Array
(
[chk] => Array
(
[0] => 14
[1] => 11
[2] => 9
[3] => 7
[4] => 17
[5] => 15
[6] => 22
)

)

Array0,1,2,3,4,5,6
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What does your code look like - much easier for us to help you make changes if we know what we're dealing with.

Mac
magdi
Forum Newbie
Posts: 9
Joined: Sat Mar 13, 2004 4:36 am

The code, sorry

Post by magdi »

So the code of the checkbox is now:

Code: Select all

<td <div align="center"> <input type="checkbox" name="chk[]" value="<?php echo $row['l_id']; ?>"
And I printed out the results of the new code you provided me

Code: Select all

<?
// to see all of the posted variables:  
echo '<pre>'; 
print_r($_POST); 
echo '</pre>'; 
?>
<?
// need all the key values from the array as these are the ids 
$sold_items = array_keys($_POST['chk']); 
// implode these numbers into a comma delimited string for use in the UPDATE query 
$sold_items = implode(',', $sold_items); 
echo $sold_items;

// now the UPDATE query, with WHERE clause using IN() to ensure the 
// right records are updated 
$sql = "UPDATE lijst SET l_sold = 1 WHERE l_id IN($sold_items)"; 

// now you can run the query and your records should hopefully be updated
?>
Which gives me as a result on test.php:

Array
(
[chk] => Array
(
[0] => 6
[1] => 8
[2] => 23
[3] => 25
)

)

First part that you helped me with, the unique id of the items I chose is indeed 6,8,23,25

Echo of the imploded $sold_items gives me:
0,1,2,3

Which is not the Id of my product but the one of the array.

So I can't use this data to update my database cause it should be 6,8,23,25 instead of 0,1,2,3.

Thanks again for the help so far.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Aah, you changed the checkbox name and value - but you didn't replace:

Code: Select all

// need all the key values from the array as these are the ids
$sold_items = array_keys($_POST['chk']);
// implode these numbers into a comma delimited string for use in the UPDATE query
$sold_items = implode(',', $sold_items);
with

Code: Select all

$sold_items = implode(',', $_POST['chk']);
it's taking the array keys when it needs the values, sorry I wasn't more clear on that originally.

Mac
magdi
Forum Newbie
Posts: 9
Joined: Sat Mar 13, 2004 4:36 am

Yes! Me happy.

Post by magdi »

:lol:
Post Reply