PHP string in checkbox

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
Gavski1
Forum Newbie
Posts: 7
Joined: Thu Jan 16, 2014 4:59 pm

PHP string in checkbox

Post by Gavski1 »

I'm trying to display an array in HTML and next to each record is a checkbox, as below. I then want to be able to select a record by checking the checkbox and posting the 'value' of box2[ ] for that record. The problem I am having is the checkbox doesn't seem to post the value ($id), it only seems to handle static values (1,2,3 etc) instead of a string.

Any ideas how to work this, or suggesions for marking an individual record to then post it's id?
thanks

Code: Select all

$linkid=dbconnect("db"); 

mysql_select_db("db");
	
	if(!($result = mysql_query("SELECT * FROM table WHERE id = '$id'"))) die ("Result error");
	{
echo "<table>\n";


while ($myrow = mysql_fetch_array($result)) 
    {
		
printf("<tr><td>%u</td><td><input type='checkbox' name='box2[ ]' value='$id'></td><td>%s</td><td>%s</td></tr>\n",
		
  $myrow["id"],$myrow["descr"],$myrow["batno"],"]);

    }

echo "</table>\n";
   
   
       }//endwhile
    		}//endif 

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP string in checkbox

Post by Christopher »

The values for a checkbox can be a string or a number (all HTTP values are passed as strings anyway). You are setting the values for each element all to the same value of $id? It seems like you should set the values to $myrow["id"].

You have some syntax errors in the code above, like the last parameter in printf(). Also, HTML uses double quotes, not single quotes. And your checkbox name should be just box[] (no space between brackets).
(#10850)
Gavski1
Forum Newbie
Posts: 7
Joined: Thu Jan 16, 2014 4:59 pm

Re: PHP string in checkbox

Post by Gavski1 »

Sorry about sntx errors, cut it from my longer script

value $myrow["id"]. was just what i was looking for, works fine,

thanks
Post Reply