I'm working on a project for a university to register the students. I'm using PHP and Ajax. I created a page to show every student data and there is a button to edit the registers (you can either update or delete one of the registers). One of the parameters that every student has is where he is either active or not. So, when you click at the edit button it sends this information via URL to the edit student page (edit.php?active=yes if the student is active or, edit.php?active=no if the student is not active).
The problem is the following: at this edit student page I have a checkbox that says "Active student" and I want it to be checked if the active=yes and not checked if active=no (maybe the admin just want to change the student's email and if this checkbox is in a different state, it will result in a wrongly activated or deactivated student.
I tried a lot of solutions but none seems to be working for me. Can you guys help me please?
Thanks in advance,
Alan
I would adjust Dialex answer to:
<input type="checkbox" name="status" value="active" <?php if(isset($_GET['active']) && $_GET['active'] == 'yes') { echo 'checked'; } ?> > Active student
With some research, you could have found the answer. You can just check if the GET variable is set, and with a condition, echo the checked propriety of the input.
<input type="checkbox" name="status" value="active" <?php if(isset($_GET['active'])){ echo 'checked'; } ?> > Active student
If you have a checkbox on a form, the name=value pair in the $_GET variable will only be defined if the checkbox was checked. If the checkbox is unchecked, then nothing is returned. So the correct PHP code to check for the value of a checkbox is:
if (isset($_GET['tagName'])) {
// do some stuff here
}
Related
First my English not good enough.I will try to explain my problem.
My problem is I need a check box on register form. I am not good in Php. I added new lines for register, names, surnames etc. but I nned a check box and I nned to control that if checked or not too. And a link to Terms and Conditions too. when the users click it has to open as light box. is it possible?
I want something like this
html=> <input type="checkbox" name="checkbox1"/>
<input type="checkbox" name="checkbox2"/>
<?php
if(!isset($_POST['checkbox1']))&&(!isset($_POST['checkbox2'])
{
echo "select term & service also check box";
}?>
I have found a PHP form that I have edited to suit my needs.
I have 12 check boxes. Each has a different option to select. They all email fine, but it says "on" next to the option in the email so I know who has selected what.
The only problem is that I'd rather have it say something else instead of "on"; is this possible?
Just add value='whatever' to the html.
<input type="checkbox" name="food1" value="Beans">I would like beans
<input type="checkbox" name="food2" value="Chips">I would like chips
I am pulling data down from a MySQL table and loading it into a form for editing(updating) a record. Everything is working great until I come the the check boxes. The checkboxes in the form accurately reflect the values in the appropriate columns in the db. But when the person editing changes the checkbox in the edit form it does not pass the data to the database. I have read a ton of checkbox Q&A on stack overflow but don't seem to find what I am looking for. Sorry if this is a redundant Question. Here is the code.
<label for="amenities-beach">
<input class="choose" name="amenitiesB" id="amenities-beach" type="checkbox"
value="<?php echo $row1["amenitiesB"]; ?>"
<?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
Close to Beach</label>
Where amenitiesB in:
value="<?php echo $row1["amenitiesB"]; ?>
is what has been returned from the DB with a SELECT statement with:
$row1 = mysql_fetch_array($result);
But when I change the value in the form and submit it nothing is passed to the variable in the UPDATE statement. Any idea what I am missing? I have 6 of these checkboxes,amenitiesB, amenitiesK, amenitiesS, amenitiesP, amenitiesF, and preferred all with the same code. Any help would be appreciated.
Thank You,
Dave
Ok here is the code: Everything else in the form updates fine. I attempt to pass it to:
$amenitiesB = $_POST['amenitiesB'];
then I put it into the update statement
Hotels.amenitiesB='".$amenitiesB."',
My UPDATE statement is,
$query="UPDATE Hotels
JOIN surfcup_Rates ON Hotels.id = surfcup_Rates.hotelid
SET Hotels.hotel='".$hotel."',
More columns, then
Hotels.amenitiesB='".$amenitiesB."',
Hotels.amenitiesB='".$amenitiesK."',
Hotels.amenitiesB='".$amenitiesS."',
Hotels.amenitiesB='".$amenitiesP."',
Hotels.amenitiesB='".$amenitiesF."',
Hotels.amenitiesB='".$preferred."',
More columns then:
WHERE Hotels.id='".$id."'";
The problem you have comes because when a checkbox is unchecked, by default its data is not transmitted to your PHP, and that's why you have problems by having the UPDATE query parameter empty.
So before your update statement you should have:
$fieldenabled=(bool)(isset($_POST['CHECKBOXNAME']) ? TRUE : FALSE;
And call your UPDATE query with that.
EDIT: Of course you can change $_POST with $_GET depending on the sending method of the <form>
Edit: I think I get the problem. When the box is initially unchecked, the input has an empty value, then when you check it, it passes an empty value in... it will never fill with what you intend the checked value to be. So, instead you need something like this:
<input class="choose" name="amenitiesB" id="amenities-beach" type="checkbox" value="amenity B Selected" <?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
... don't make the "value" attribute dynamic, or else once it becomes empty it will always be empty.
Original Answer:
I assume when you say "change the value in the form" you mean that you uncheck the checkbox... unchecked checkboxes never send any data when you submit the form. You check for "unchecked" status by checking to see if the form variable has been passed at all.
For example:
if (isset($_GET['amenitiesB'])) {
// process with the knowledge that "amenitiesB" was checked
}
else {
// process with the knowledge that "amenitiesB" was unchecked
}
If you mean that you somehow dynamically change the "value" of the checkbox to something else, then I'll need to see the code that accomplishes that.
The main purpose of the "value" attribute in a checkbox input is when you're passing the variable as an array:
<label for="amenities-beach">
<input class="choose" name="amenities[]" id="amenities-beach" type="checkbox" value="<?php echo $row1["amenitiesB"]; ?>" <?php echo $row1["amenitiesB"] ? 'checked="checked"' : ''; ?> />
Close to Beach
</label>
... note specifically that I've changed the "name" attribute from "amenitiesB" to "amenities[]", which, if carried through all of your amenities checkboxes, will give you access to them all in your processing script through the $_GET['amenities'] array (or $_POST, if applicable). Otherwise, there's not much reason to use the "value" attribute of the checkbox input, because you can get all you need just by knowing $_GET['amenitiesB'] is checked (and thus sent with the form) or unchecked (and not sent with the form).
I have a small problem with my PHP Code and as always, any help is hugely appreciated.
The code was originally designed by another person and I seem to have come across a problem that I can't fix.
The structure of the page is as follows:
A while loop to output the data, each of course, has a unique id.
A checkbox that must be ticked before pressing the 'update' button
(several update buttons present pending on how many rows are
returned.
This is what i'm confused on, he has designed it to they must tick the 'checkbox' before pressing update, otherwise, the script doesn't know which 'id' to update. Why is it like this?!
<?php
if($_POST['accept']){
if(!$_POST['check']){
echo "<div class='error_input'>Error! please tick box to confirm </div>";
}else{
$form_id=$_POST['fid'];
$customer=$_POST['customer'];
mysql_query("UPDATE job SET customer='$customer'WHERE f_id=$form_id ");
echo "<div class='form_ok'>Job has been updated.</div>";
}
?>
If I just comment out the check box, and the user clicks the 'accept' button, it doesn't work, so clearly the 'checkbox' is pointing to the unique ID of the row, but I can't see it?
<input type="checkbox" name="check" value="1" required/>
<input type="submit" name="accept" value="Accept Job">
My issue is, I want to be able to remove this annoying checkbox, and once the user has pressed 'accept' add the unique ID into a session, which I know can be done by:
$_SESSION['user_id'] = $user_id;
As they then get redirected to a pop-up box to which I need this unique ID to pull further data from the database.
I've had a go at implemented this:
<input type="submit" name="accept" value="<?php echo"$user_id"; ?>" class="sbutton">
which works in a nutshell, how ever, it's not user friendly and I can't access the user_id, when I use the $_POST feature, it saves the 'name' field.
I know this is long winded, I hope i've given you enough information, many thanks in advance.
It seems like there is another hidden field in there with the job ID, and each row has it's own <form> tags - so if you'll remove the check for the check field than you're fine.
Like that:
<?php
if($_POST['accept'])
{
$form_id=$_POST['fid'];
$customer=$_POST['customer'];
mysql_query("UPDATE job SET customer='".$customer."' WHERE f_id=".$form_id);
echo "<div class='form_ok'>Job has been updated.</div>";
}
?>
By the way - it's best that you'll check and secure the values from the $_POST with check for numbers and securing against SQL Injection with mysql_real_escape_string function
Why can't the data in mysql not updated when I use checkbox, I already tried removing the input type hidden, but still didn't work. I think the problem must be in this part, because when I try to add records and check all of the checkboxes.
Then I will try to update it. By removing the checks in the other entry. The data is updated. the checks that are removed are also reflected in the database, but when I try to add checks on them again using the update module, it will not be updated. Please help, I'm just a beginner.
<td><input name="stats3" type="checkbox" id="sh"
value="<?php echo $row["STAT3"]; ?>" <?php echo $row["STAT3"] ? 'checked="checked"' : ''; ?> >Stockholder</td>
When you have a checkbox and you tick it, you get a key/value pair returned in the post to the server.
When the checkbox is not checked, it actually doesn't come back in the post (you can check this using Firebug, or by print_r($_POST)
This could be causing your problem.
You can either use:
if(isset($_POST['stats3'])) {
// checked is true
}
Or another easy solution is to have a select list with yes and no as options, which will always give you a value back - otherwise you need to set and unset the value based on the presence of the tick box in the post.
or you could check
if (isset($_POST['stats3']))
// value received
else
// value not received
<input name="stats3" type="checkbox" id="sh"
value="
<?php echo $row["STAT3"]; ?>
"
<?php echo $row["STAT3"] ? 'checked="checked"' : ''; ?>
>Stockholder
Its just working fine for me,
are if you are posting multiple values in the same name as "stats3" then you will have a problem