Jump to content

Simple way to toggle on/off field


Grant Holmes

Recommended Posts

I have a "Status" field called Active that is a TinyINT. The only values will be ONE or ZERO designating on/off (or yes/no).

 

At first I thought it would be easy to show/change the status (value) using a checkbox. So far, I'm easily showing the value in a check box:

<?php    $checked = $Active ? "checked" : "";
echo "<input type=\"checkbox\" name=\"foo\" $checked>";
if ($foo == "checked") $value = 1; else $value = 0;
?>

 

The user will click an "edit" link to change any part of the record (like name, addr., etc) as well as changing status. I cannot figure out on my edit page how to modify the above to not only show the current status, but change it.

 

I'm open to radio buttons a On/Off pull down list (that would show correct status upon load) or another simple answer. I could just bring the box up as INPUT and let the user change 1 to 0, etc. but we all know how that will end up.

 

Help?

Link to comment
Share on other sites

This is how I toggle my online booking Paid status for a person

 

if ($row2[0] == "Yes") {

$paid2 = "No";

}

else {

$paid2 = "Yes";

}

 

So when I click on the word Yes, it changes to No.  If I click on No, it changes to Yes.

Link to comment
Share on other sites

In this post, I've not admitted my noob status. I'm so sorry, but I'm not sure where to put the snippets.

 

I can show the status on one screen. I need to change it on another. So if it is unchecked, I need to return a zero to the table, on checked the value needs to be one.

 

I'm not sure if the solutions offered are to replace mine or augment it. After the check/uncheck, the user will hit an update button.

Link to comment
Share on other sites

Using your code...

 

<?php
$checked = $_POST["foo"];

if ($checked == "checked")
{
    $value = 0; // It's checked so make it 0
}
else
{
    $value = 1; // It isn't checked, value is 1
}

echo "The value is ".$value;
?>

 

HTML:

<form method="post">

<input type="checkbox" name="foo" />

<input type="submit" name="submit" />

</form>

 

Why are you using things such as ternary conditions as a noob? Keep it simple and easily readable! I haven't tried the code above but it should work.

Link to comment
Share on other sites

Thanks emehrkay,

 

However, the reason I admitted to Newbie status above is that when you very proficient people provide snippets like you did, doesn't help me as I have no clue what to do with it. Even your helpful answer means nothing as I don't know where to put it. Sigh.

 

Here's what I have that is showing the value, but doesn't allow me to change anything. Above this point I've already selected the record on the page and established the table it is in:

<TD>
<!-- // Set the variable $foo from the database or from form data. -->
<?php    $checked = $Active ? "checked" : "";
echo "<input type=\"checkbox\" name=\"foo\" $checked>";
?>
</TD>
<TD><CENTER><input type="submit" name="submit" value="Update">

 

Can someone please show me the code again with correct editing? I really appreciate the help.

Link to comment
Share on other sites

so $Active is set to the value from the Database, being one or zero?

 

$checked = ($Active) ? 'checked="checked"' : '';  //this is a ternary - (condition) ? value if true : value if false;

echo "<input type=\"checkbox\" name=\"foo\" value=\"". $Active ."\" ". $checked .">";

 

try that

 

I dont know what you mean by "change anything" the only change that will happen when the checkbox is checked or not is when you process the form unless you do some JavaScripting

Link to comment
Share on other sites

We're getting closer! At least now, when I uncheck the button, it passes that value (ZERO), but will not pass the "Check" or ONE.

 

My page didn't like the () around my field, so I removed them.

And to answer your question, yes: pass the value "1" if checked; "0" if unchecked. Here's what I have now:

<?php    $checked = $Active ? 'checked="checked"' :'';  //this is a ternary - (condition) ? value if true : value if false;
echo "<input type=\"checkbox\" name=\"foo\" value=\"". $Active ."\" ". $checked .">";
?>

Link to comment
Share on other sites

This morning I am still stuck halfway. I have ONE checkbox in my example for ONE field.

 

Thanks to all of your help, I've modified my code and "most" of it works:

1) I'm pulling the value of a TinyINT field (containing a 1 or 0) from MySQL- That Works

2) I'm displaying the correct DB value in a checkbox- Works

---If the field value=1 it's checked and if the field value=0 it's unchecked.

3) You click an edit link that takes you to another page that allows you to update any field in that record- Works

4) On the edit page I can update any INPUT field upon SUBMIT- Works

5) If the single checkbox is checked and I uncheck it-(changing the 1 to a 0)-- upon SUBMIT, That Works.

 

Here's the issue.

During an edit, IF the checkbox (or stored value) =0... I change any input field and I check the box (wanting to change the 0 to a 1), all the input fields are updated, But nothing is returned to that (Active) field-the value remains zero.

 

Here's where I am Code-wise:

<?php    $checked = $Active ? 'checked="checked"' :'';  //this is a ternary - (condition) ? value if true : value if false;
echo "Active? <input type=\"checkbox\" name=\"foo\" value=\"". $Active ."\" ". $checked .">";
?>

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.