Jump to content

update record using checkbox


palacios

Recommended Posts

Hi,

 

I'm trying to use check-boxes in a webpage to update the information kept in a database. I managed to understand how it works with text but with checkbox i dont even know if it possible.

 

<form id="form1" name="form1" method="post" action="">
    <p>
        <label for="name">name:</label>
        <input name="name" type="text" class="widebox" id="name" value="<?php echo htmlentities($row['name']); ?>" />
<!-- so far so good -->
    </p>
    <p>
    	<label for="air_conditioning">air-conditioning</label>
    	<input name="air_conditioning" value="air_conditioning" type="checkbox" name="air_conditioning" id="air_conditioning"
<?php 
// i came up with this code but it doenst work, the display from the db comes correct but if i want to update using the checkbox, nothing really happens.
$yesorno = ($row['air_conditioning']);
$option = $yesorno == 'Y' ? "checked" : "unchecked";
echo ($option);
?>
/>
    </p> 
    <p>
        <input type="submit" name="update" value="save changes!" />
<input name="id" type="hidden" value="<?php echo $row['id']; ?>" />
    </p>    
</form>

please advice and thank you in advance,

 

giulio

 

 

Link to comment
Share on other sites

What are the possible values for the 'air_conditioning' field from the DB? It SHOULD be 1/0 for true/false, not Y/N. Anyway, there were some other minor errors. This should work, but change the 'Y' to '1' if you get around to fixing the DB values.

 

<?php

$name = htmlentities($row['name']);
$checked = ($row['air_conditioning']=='Y') ? 'checked="checked"' : '';

?>
<form id="form1" name="form1" method="post" action="">
    <p>
        <label for="name">name:</label>
        <input name="name" type="text" class="widebox" id="name" value="<?php echo $name; ?>" />
    </p>
    <p>
    	<label for="air_conditioning">air-conditioning</label>
    	<input name="air_conditioning" value="true" type="checkbox" id="air_conditioning"<?php echo $checked; ?> />
    </p> 
    <p>
        <input type="submit" name="update" value="save changes!" />
<input name="id" type="hidden" value="<?php echo $row['id']; ?>" />
    </p>    
</form>

Link to comment
Share on other sites

Thank you all for your help.

 

I think I have some errors in the code somewhere else, if I use your revised code I have on the db some other characters like 'm' or 'c', for example:

linen_change: l

maid_service: m

cctv: c

actually -now that i'm writing it - looks like the db is filling in the cells with the firs letter of the field name... strange...

 

my questions now are:

* is it really necessary to grasp the value of the database and link it to a variable (as shown in mjdamao post)? does it bring any advantage?

 

* i understand the suggestion to use 1-0 instaed of Y-N in the database, but shall i change also the field type? at the moment is set up as 'char' with lenght '1', I dont see any boolean option for the field type or am I missing something?

 

 

thanks a lot again

giulio

 

 

Link to comment
Share on other sites

my questions now are:

* is it really necessary to grasp the value of the database and link it to a variable (as shown in mjdamao post)? does it bring any advantage?

If you don't compare the value from the database how would you make the determination of whether to show the checkbox as checked or not to indicate the currently saved value?

 

* i understand the suggestion to use 1-0 instaed of Y-N in the database, but shall i change also the field type? at the moment is set up as 'char' with lenght '1', I dont see any boolean option for the field type or am I missing something?

You should use a field type of tinyint

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.