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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.