Forum Moderators: coopster

Message Too Old, No Replies

php and checkboxes

         

shuini

6:17 am on May 24, 2005 (gmt 0)

10+ Year Member



i have checkboxes in my script and i need to save the checked entries to my database.

i made the checkboxes an array.
the problem is, when i try to print the array, what prints is the name of the array itself and not the content of the array.

here's the code
$check_entries=array();
$check_entries=$_POST["check_entries"];

$check_count = count($check_entries);
print($check_count);

for($a=0; $a<$check_count; $a++)
{
print($check_entries[$a]);
}

output:
check_entriescheck_entries

output should be:
cat dog

dreamcatcher

7:10 am on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How is your HTML code for the array? Should be:

<input type="checkbox" name="check_entries[]" value="cat">
<input type="checkbox" name="check_entries[]" value="dog">

Is that how you have it?

..and welcome to Webmaster World shuini :)

mcibor

8:15 am on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also try to see what's really in the array:

foreach($check_entries as $name => $value)
{
echo "Name: $name &nbsp;&nbsp;&nbsp;Value: $value<br>";
}

Then you will know what is the field and it's value. Maybe as dreamcatcher suggested you have sth wrong with your html?

Best regards
Michal Cibor

PS. The code you wrote should work

shuini

5:28 am on May 25, 2005 (gmt 0)

10+ Year Member



thanx for welcoming me.

anyway, this is the whole code

<?php
require_once("db.lib.php");
$link=db_open();

$sql="select * from tblentries where fldmarker!='deleted' ORDER BY fldeid";
$result=mysql_query($sql,$link);

while($row = mysql_fetch_array($result))
{
print('<tr>');
print('<td height="20">&nbsp;</td>');
print('<td colspan="3" align="center" valign="middle">'.$row["fldename"].'</td>');
rint('<td>&nbsp;</td>');
//HERE IS MY CHECKBOX,ITS ACTUALLY IN A LOOP SO I CAN'T REALLY NAME IT ALL COZ THE NAME WOULD ACTUALLY DEPEND ON THE LOOP
print('<td align="center" valign="middle"><input type="checkbox" name="check_entries[]" value="check_entries[]"></td>');
print('<td>&nbsp;</td>');
print('</tr>');
}
?>

AND THE FORM WHO RECEIVES IT HAS THIS

$check_entries=array();
$check_entries=$_POST["check_entries"];

$check_count = count($check_entries);
print($check_count);

print($check_entries[0]);
print($check_entries[1]);

for($a=0; $a<$check_count; $a++)
{
//if (isset($_POST['check_entries[$a]' . $i]))
//{
print($check_entries[$a]);
//}
}

IT PRINTS CHECK_ENTRIES OVER AND OVER AGAIN

mcibor

6:53 am on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And it should print that out, as it's what is in the checkbox
You have:
print('<td align="center" valign="middle"><input type="checkbox" name="check_entries[]" value="check_entries[]"></td>');

I think you should have:
print('<td align="center" valign="middle"><input type="checkbox" name="check_entries[]" value="'.$row["fldename"].'"></td>');

If not this part of row, then put there something else that should be there, not the value="check_entries"

Best regards
Michal Cibor

shuini

8:15 am on May 25, 2005 (gmt 0)

10+ Year Member



yep, i tried that already.

and it prints
row["fldename"]

y can't it print the value of ename? how do i do that?

dreamcatcher

12:33 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you included the $ with the variable?

$row["fldename"]

What mcibor posted should work ok.

mcibor

8:32 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Write exactly as below (with every single quote)

value="' . $row["fldename"] . '"></td>')

that is: after value you must have double quote: ", and then single quote: '.
It is because if you write:

$v = "Kate";
echo "Welcome $v";// Welcome Kate - the parser will see that it's the variable. However

echo 'Welcome $v';// Welcome $v - the parser thinks this is only text, no variables in it!

So to correct the second:

echo 'Welcome '.$v;// Welcome Kate

Hope this cleares the things up for you!
Best regards
Michal Cibor