Jump to content

checkboxes already checked


XpertWorlock

Recommended Posts

Hi, I'm trying to have it when the page is loaded up, anything with '1' equals checkbox checked, '0' not checked.

 

Right now I have :

 

$result = mysql_query("SELECT * FROM image");

 

while($row = mysql_fetch_array($result))

  {

  echo $row['img_brownhair'];

  echo $row['img_blackhair'];

    }

 

 

I'm getting no errors, it works fine, but it displays 1's and 0's.  I'm new to php/MySql forms.  It would need an update button, but I think I can figure that out  :-\  .. well maybe.

 

MySQL version 5.0.45-community   

 

Thanks for any help

it is greatly appreciated.

Mike

 

Link to comment
Share on other sites

this piece of code will create a checkbox, $checked is either NULL or the word 'checked' which will cause the checkbox to be checked when the page is loaded

shouldnt be too difficult for you to alter it.

<?php

if($row['checked']==1) {

    $checked = "checked";

} else {

    $checked = "";

}

echo "<input name=\"image[$row['img]\" type=\"checkbox\" value=\"true\" $checked >"

?>

Link to comment
Share on other sites

What does an "image" row look like?

 

Name  img_name

Size (Bytes)  img_size

Type (JPG,GIF) img_type

File History  img_filehistory

Brown Hair  img_brownhair 

 

 

Now I have this.....

 

$result = mysql_query("SELECT * FROM image");

 

while($row = mysql_fetch_array($result))

 

   

if($row['checked']==1) {

    $checked = "checked";

} else {

    $checked = "";

}

echo "<input name=\"image[$row['img_brownhair]\" type=\"checkbox\" value=\"true\" $checked >"

 

 

 

and I get an error on the last line (the echo) 

T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

it works if I do this :

I took out the ' in the [img_brownhair] and added a ; at the end of the echo, although if img_brownhair already equalled "1" (checked) it doesn't show up checked.

Link to comment
Share on other sites

while($row = mysql_fetch_array($result))

if($row['checked']==1) {
    $checked = "checked";
} else {
    $checked = "";
}
echo "<input name=\"image[$row['img_brownhair]\" type=\"checkbox\" value=\"true\" $checked >"[/b]

 

This makes no sense -- you don't have a column called checked; and your life will be much simpler if you use different quoting inside & out.

Link to comment
Share on other sites

I don't know where I am going wrong, I search and search but most people are using arrays to do their checkboxes, which is okay, if I could figure out how to do it.

 

What I'm looking for

 

by default every image uploaded has no checkboxes checked == 0

one table with multiple rows (or just one if the array method works) and an update button

when update is clicked, it updates the database and refreshes the page.

 

All the help I've received is greatly appreciated, I'm trying to learn as much as I can.

 

Thanks

Mike

 

Link to comment
Share on other sites

So far everything is working like it is supposed to.  1= checked 0=unchecked.

if($row['img_brownhair']==1) {
    $checked = "checked";
} else {
    $checked = "";
}
echo "<input name=\"img_brownhair\" type=\"checkbox\" value=\"true\" $checked >";

 

 

My last part to this question is how do I update the database, if it is changed on screen? 

echo "<input type=submit value=Update>";

that creates the update button.  Do I have to change the input name on the checkbox? 

Link to comment
Share on other sites

What does an "image" row look like?

 

Name  img_name

Size (Bytes)  img_size

Type (JPG,GIF) img_type

File History  img_filehistory

Brown Hair  img_brownhair 

 

 

Now I have this.....

 

$result = mysql_query("SELECT * FROM image");

 

while($row = mysql_fetch_array($result))

 

   

if($row['checked']==1) {

    $checked = "checked";

} else {

    $checked = "";

}

echo "<input name=\"image[$row['img_brownhair]\" type=\"checkbox\" value=\"true\" $checked >"

 

 

 

and I get an error on the last line (the echo) 

T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

it works if I do this :

I took out the ' in the [img_brownhair] and added a ; at the end of the echo, although if img_brownhair already equalled "1" (checked) it doesn't show up checked.

 

Get rid of the first single quote where it says img_brownhair and it should work

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.