Hi all,

My apologies if this has been asked before, but I am unable to find any help with this problem I am experiencing.

I have currently developed a mysql database with a php form that allow me to fill out all fields and check boxes which works fine and updates the database ok.

My problem is when I call the data back on a view/edit page (form), I get all data in the text boxes but not in the check boxes, I am stumbled on how to do this and I also want to be able to update the check boxes if required,

Any help would be great,

Martin

Recommended Answers

All 20 Replies

If the checkbox should be checked, add checked="checked" to the input tag attributes:

<input type="checkbox" name="foo" value="1" <?= $foo ? 'checked="checked"' : '' ?> >

Thanks for your reply deceptikon,

I have added this to existing code but I am still having problems - when I open the form to edit it, the tick boxes are unticked,

On submitting the form the check boxes are ticked

Martin

This is a stupid question, but taking my example above, are you making sure that $foo contains the value you expect?

Simply this:

<input type="checkbox" name="variablename" value="yourrequiredvalue" <?php if (!empty($variablename) && $variablename == 'yourrequiredvalue') { echo 'selected'; } ?> >

Obviously you need to replace the name and value in the checkbox to match your variables and values and then compare the value of the variable that matches what is stored in your database for this checkbox with the value you have stipulated for the checkbox and if they match echo selected.

This is a stupid question, but taking my example above, are you making sure that $foo contains the value you expect?

My apologies if this is a stupid question, I am quite new the php development and have never used checkboxes in any my projects before

Thanks simplypixie for the replay,

I will try this later

Martin

Thanks for your replies,

Have managed to figure it out, got some of it working

Hi,

I have used the following code

<input class="auto-style6" name="intro" type="checkbox" value="yes"  <?php if (!empty($intro) && $intro == 'yes') { echo 'selected'; } ?> />

And declared intro at the top of my page and set as below

$intro = ('yes');

But still having problems when I load the page and the checkboxes staying blank, in the database intro is marked as yes, but some on my other checkboxes are blank so the checkboxes should remain blank on my page,

Any thoughts to where I'm going wrong

The variable declaration should be in brackets:

$intro = 'yes';

The variable declaration should be in brackets:

$intro = 'yes';

Hi simplypixie,

Thanks for coming back to me,

I have put the variables in brackets but still getting the same problem

Sorry, that should have said should NOT be in brackets as in the example code I posted.

Sorry, that should have said should NOT be in brackets as in the example code I posted.

Thanks,

Have tried both ways still not working for me I'm afraid

Thanks for your assistance

My bad again - it should be

echo 'checked';

, not

echo 'selected';

My bad again - it should be

echo 'checked';

, not

echo 'selected';

Thanks,

It is now showing them ticked in my form which is great,

Only got one last problem, all ones ones which are blank in the database are also being ticked in the form, is there way of only showing the ones which are ticked and leaving the ones which are blank empty

If each checkbox has a corresponding field name in the table and you are assigning the values stored in the database to corresponding variable names then they should only be checked if the value = 1. I cannot help any more without your new code (php that is querying the db and assigning values to variables and your form code) to see what you are doing now after the changes.

Hi,

Thanks for coming back to me,

I have included my code below

PHP

<?php
$intro = 'yes';
?>

<?php
mysql_connect("localhost", "user", "pass")or die("cannot connect");
mysql_select_db("dbname")or die("cannot select DB");

$id=$_GET['migid'];

$sql="SELECT * FROM tabe_db WHERE migid='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>

HTML

<form action="proc.php" method="post">
			
Intro : 

<input class="auto-style6" name="intro" type="checkbox" value="yes" <?php if (!empty($intro) && $intro == 'yes') { echo 'checked'; } ?> />
<input class="auto-style6" name="Submit1" type="submit" value="Update" />
</form>

Thanks,

Your code makes no sense I am afraid - you are not displaying the results from your query anywhere and you only have one checkbox in your form. I am confused.

Your code makes no sense I am afraid - you are not displaying the results from your query anywhere and you only have one checkbox in your form. I am confused.

Sorry for the confusion,

I only copied in some of the code as there is allot of it,

I have included all code below

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<?php

session_start();

if (!isset($_SESSION['usersession'])) {
        header('Location: index.php');
}

?>
<?php
$intro = 'yes';
$pos_domain_add  = 'yes';
$dns_mx  = 'yes';
$dns_srv = 'yes';
$org_pro = 'yes';
$mbox_created = 'yes';
$mbox_renamed = 'yes';
$aliase_add = 'yes';
$help_desk = 'yes';
$dns_prop = 'yes';
$pos_moved = 'yes';
$mbox_tested = 'yes';
$bes_add = 'yes';
$smartphone_add = 'yes';
$mbox_deleted = 'yes';
$reminder = 'yes';
?>
<?php
mysql_connect("localhost", "user", "pass")or die("cannot connect");
mysql_select_db("db")or die("cannot select DB");

$id=$_GET['migid'];

$sql="SELECT * FROM tabe WHERE migid='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
</head>

<body>

<table align="center" cellpadding="4" class="auto-style1" style="width: 60%">
	<tr>
		<td style="width: 70px">
		<img height="48" src="img/image001.png" width="429" class="auto-style6" /></td>
		<td class="auto-style8">
		<a href="view-edit.php">View/Edit</a> - <a href="main.php">Control Panel </a></td>
	</tr>
	<tr>
		<td class="auto-style2" style="height: 48px" colspan="2"><strong>&nbsp;Edit Log </strong></td>
	</tr>
	<tr>
		<td colspan="2">
		<form action="proc.php" method="post">
			<table style="width: 100%">
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;Status&nbsp;: </td>
					<td>
					<select name="status" class="auto-style6" style="height: 24px">
					<option><? echo $rows['status']; ?></option>
					<option>-- Update --</option>
					<option>Open</option>
					<option>On Hold</option>
					<option>Work in Progress</option>
					<option>Completed</option>
					</select></td>
				</tr>
				<tr>
					<td class="auto-style3" colspan="2">&nbsp;<span class="auto-style4"><strong><span class="auto-style7">-- 
					Detials</span></strong><span class="auto-style7"> -- </span>
					</span></td>
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;ID 
					: </td>
					<td>
					<input class="auto-style6" name="id" type="text" style="width: 250px" value="<? echo $rows['id']; ?>" /></td>
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;Customer 
					: </td>
					<td>
					<input class="auto-style6" name="customer" type="text" style="width: 250px" value="<? echo $rows['customer']; ?>" /></td>
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;Primary 
					Domain : </td>
					<td>
					<input class="auto-style6" name="pri_domain" type="text" style="width: 250px" value="<? echo $rows['pri_domain']; ?>" /></td>
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;Accepted 
					Domains (Aliases) : </td>
					<td>
					<textarea class="auto-style6" name="acp_domains" style="width: 250px; height: 80px"><? echo $rows['acp_domains']; ?></textarea></td>
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;Agreed 
					Migration Date :&nbsp;</td>
					<td>
					<input class="auto-style6" name="agree_date" type="text" style="width: 250px" value="<? echo $rows['agree_date']; ?>" /></td>
				</tr>
				<tr>
					<td class="auto-style5" colspan="2"><strong>&nbsp;--&nbsp;Checklist&nbsp;-- </strong></td>
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;Introduction/Questions 
					: </td>
					<td class="auto-style6">
					<input class="auto-style6" name="intro" type="checkbox" value="yes" <?php if (!empty($intro) && $intro == 'yes') { echo 'checked'; } ?> /></td>
					
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;Postini 
					(Domain Added if Req) : </td>
					<td class="auto-style6">
					<input class="auto-style6" name="pos_domain_add" type="checkbox" value="yes" <?php if (!empty($pos_domain_add) && $pos_domain_add == 'yes') { echo 'checked'; } ?> /></td>
				</tr>
				<tr>
					<td class="auto-style6" style="width: 226px">&nbsp;DNS : </td>
					<td class="auto-style6">MX
					<input class="auto-style6" name="dns_mx" type="checkbox" value="yes" <?php if (!empty($dns_mx) && $dns_mx == 'yes') { echo 'checked'; } ?> />/>SRV
					<input class="auto-style6" name="dns_srv" type="checkbox" value="yes" <?php if (!empty($dns_srv) && $dns_srv == 'yes') { echo 'checked'; } ?> /> /></td>
				</tr>
				</table>
		</form>
		</td>
	</tr>
	<tr>
<?
mysql_close();
?>
		<td class="auto-style6" colspan="2">&nbsp;</td>
	</tr>
</table>

</body>

</html>

Thanks

I can quite clearly see the problem now. You have set all your variables to equal yes when what you should be using the information from your database. So in the checkboxes you need to use the data like you do in your other form fields, for example:

<input class="auto-style6" name="intro" type="checkbox" value="yes" <?php if (!empty($rows['intro']) && $rows['intro'] == 'yes') { echo 'checked'; } ?> />

I can quite clearly see the problem now. You have set all your variables to equal yes when what you should be using the information from your database. So in the checkboxes you need to use the data like you do in your other form fields, for example:

<input class="auto-style6" name="intro" type="checkbox" value="yes" <?php if (!empty($rows['intro']) && $rows['intro'] == 'yes') { echo 'checked'; } ?> />

Thanks You simplypixie so much for your help on this one, it is most appreciated, this worked like a dream, a knew it could be done but didn't have a clue as I have not used checkboxes before

Thanks Again,
Martin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.