Page 1 of 1

PHP string in checkbox

Posted: Sat Apr 12, 2014 4:59 am
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 


Re: PHP string in checkbox

Posted: Sat Apr 12, 2014 11:24 am
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).

Re: PHP string in checkbox

Posted: Sun Apr 13, 2014 11:33 am
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