Jump to content

[SOLVED] checkbox


kokkentor

Recommended Posts

I am diving straight into it... (following http://www.kitebird.com/mysql-book/3ed.php) learning the hard way..., and after much searching and trying without success, I would appreciate if someone would help me on the way to achieve the following :-)

 

I want a (one for a starter) checkbox to be populated/checked depending on a field value in a database table -- "equivalent" to populating text fields:

 

function text_field ($name, $value, $size)
{
printf ("<input type=\"%s\" name=\"%s\" value=\"%s\" size=\"%s\" />\n",
			"text",
			htmlspecialchars ($name),
			htmlspecialchars ($value),
			htmlspecialchars ($size));
}

function display_column ($label, $row, $col_name, $editable = TRUE)
{
print ("<tr>\n");
printf ("<td>%s</td>\n", htmlspecialchars ($label));
print ("<td>");
if ($editable)	# display as edit field
	text_field ("row[$col_name]", $row[$col_name], 80);
else			# display as read-only text
	print (htmlspecialchars ($row[$col_name]));
print ("</td>\n");
print ("</tr>\n");
}

function display_entry ($conn) 
      //prints a form displaying fields from DB and with a button to update
{
        .
        .
printf ("<form method=\"post\" action=\"%s?action=%s\">\n",
			script_name (),
			urlencode (UPDATE_ENTRY));
        .
        .
display_column ("Last name", $row, "last_name");
display_column ("First name", $row, "first_name");
        .
        .
       submit_button ("button", "Submit");
print "</form>\n";
}

 

The relevant field name (column/variable) in the relevant table (member) is "henvis".

NULLS are allowed, and value 1 should cause the checkbox to be checked.

 

So something like if($henvis == "1"){echo " CHECKED";} somewhere in a check_box function, equivalent to the text_field function?

 

Would appreciate if any of you gurus could point me in the right direction. :-)

 

Thanks in advance!

 

 

Link to comment
https://forums.phpfreaks.com/topic/155834-solved-checkbox/
Share on other sites

I think I have to start over again. Having tried and tried and seen this post: http://www.phpfreaks.com/content/print/131 I still can't get it working. So if someone can help me, I'll be happy.

 

I am calling a function:

display_printBoxColumn ("Henvisning nødvendig?", $row, "henvis");

 

The function is the follwing:

 

function display_printBoxColumn ($label, $row, $col_name, $editable = TRUE)
{
print ("<tr>\n");
printf ("<td>%s</td>\n", htmlspecialchars ($label));
print ("<td>");
if ($editable)	# display as edit field
	checkBox ("row[$col_name]", $row[$col_name], "$row[$col_name]");
else			# display as read-only text
	print (htmlspecialchars ($row[$col_name]));
print ("</td>\n");
print ("</tr>\n");
}

 

But I can't get the checkbox function below to read the value (0 or 1) from the database and tick off (check) the checkbox if the value of the relevant database field is 1 (in this case column "henvis"), otherwise leave the checkbox unchecked/unticked.

 

function checkbox ($name, $value, $checked)
{
printf ("<input type=\"%s\" name=\"%s\" value=\"%s\"%s />%s\n",
			"checkbox",
			htmlspecialchars ($name),
			htmlspecialchars ($value),
			($checked = ? ($row[$col_name]==1) ? 'checked="checked"' : ''),
			htmlspecialchars ($label));
}

 

Anyone?

Link to comment
https://forums.phpfreaks.com/topic/155834-solved-checkbox/#findComment-826599
Share on other sites

Figured it out. The checkbox function should be like this:

function checkbox ($name, $value, $checked)
{
printf ("<input type=\"%s\" name=\"%s\" value=\"%s\"%s />%s\n",
			"checkbox",
			htmlspecialchars ($name),
			htmlspecialchars ($value),
			($checked = ($value==1) ? 'checked="checked"' : ''),
			htmlspecialchars ($label));
}

Link to comment
https://forums.phpfreaks.com/topic/155834-solved-checkbox/#findComment-827305
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.