PHP form check box from db table (beginner) - php

Had an a question answered on a different post; now I need to solve the next step! :)
We have a DB that has secondary_phone_number as either a 1 or a 0.
In our View (MVC); I have
{
$secondaryNumber = $company->getSecondaryPhoneNumber();
if ($secondaryNumber = 1) {
$data['secondaryphonenumber'] = TRUE;
} else {
$data['secondaryphonenumber'] = FALSE;
}
}
Then in our template (HTML), I am trying to display either the checkbox as being checked if Yes (1) or No (2), depending on the DB entry:
<label>
<input type="checkbox" name="secondary_phone_number" value="1" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
No
</label>
The problem is, both checkboxes are displaying as Checked, no matter what the DB says. Any thoughts?

I'm not sure what kind of MVC framework you're using or you made your own framework, but your approach doesn't really care what is in secondaryphonenumber when generating checkbox
try something like this
<label>
<input type="checkbox" name="secondary_phone_number" value="1" <?=$secondaryphonenumber?"checked":""?>/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" <?=$secondaryphonenumber?"":"checked"?>/>
No
</label>
But I'd suggest using radio button instead because checkbox allows user to select both options.

Try this one..
<label>
<input type="checkbox" name="secondary_phone_number" value="1" <?php if($data['secondaryphonenumber'] == TRUE){?>checked="checked"<?php }?>/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" <?php if($data['secondaryphonenumber'] == FALSE){?>checked="checked"<?php }?>/>
No
</label>
Using radio button will be a better option..

Try this :
$secondaryNumber = $company->getSecondaryPhoneNumber();
if ($secondaryNumber = 1) {
$data['secondaryphonenumber'] = TRUE;
} else {
$data['secondaryphonenumber_false'] = TRUE;
}
<label>
<input type="checkbox" name="secondary_phone_number" value="1" {{#secondaryphonenumber}}checked="checked"{{/secondaryphonenumber}}/>
Yes
</label><br>
<input type="checkbox" name="secondary_phone_number" value="0" {{#secondaryphonenumber_false}}checked="checked"{{/secondaryphonenumber_false}}/>
No
</label>

Related

Set checkboxes checked based on php cookie

I have this HTML code:
<form action="index.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" checked>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" checked>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">
</form>
Also I have this PHP code:
function create_cookies()
{
if (isset($_POST['test_submit'])) {
$checkboxes_array = $_POST['test'];
setcookie("testcookie1234556", json_encode($checkboxes_array), time()+3600);
}
}
create_cookies();
function check_cookies()
{
if (isset($_COOKIE['testcookie1234556'])) {
$my_cookie_array = json_decode($_COOKIE['testcookie1234556']);
return $my_cookie_array;
} else {
return false;
}
}
check_cookies();
As you can see, I have got all checked checkboxes and stored it to the php cookie.
Now, when someone is logging to the system, all checkboxes is checked by default. But I need them to be checked or unchecked based on the info from php cookie!
I mean, for example, if user logged on to the system and unchecked some checkboxes, logged off, then logged in again, previously unchecked checkboxes must remains unchecked.
Please, give me some advices for doing it.
Put your cookie in an array say
$cookie_aray=check_cookies();
and check it every time you load a check box, whether it was selected or not.
<html><form action="abc.php" method="post">
<input id="firstCheck" type="checkbox" name="test[]" value="first" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="firstCheck">1</label><br>
<input id="secondCheck" type="checkbox" name="test[]" value="second" <?php if(in_array('first',$cookie_aray)) echo 'checked';?>>
<label for="secondCheck">2</label><br>
<input type="submit" name="test_submit" value="Send">

Array of checkbox values in PHP

I have a HTML form with multiple checkbox selection. They are defined as:
<label class="container">Afghanistan
<input type="checkbox" id="Afghanistan" name="country[]" value="Afghanistan" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">Armenia
<input type="checkbox" id="Armenia" name="country[]" value="Armenia" checked="checked">
<span class="checkmark"></span>
</label>
...
After submitting, I call a PHP file where I want to store their values in an array.
for($i=0;$i<sizeof($_POST["country[]"]);$i++){
$country[i] = htmlspecialchars($_POST["country[i]"]);
}
But this code doesn't work. Can anyone help me to solve it?
$_POST["country"] is an array for which you can get the values using the index using $i
Try it like this:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
for($i=0;$i<sizeof($_POST["country"]);$i++){
$country[] = htmlspecialchars($_POST["country"][$i]);
}
echo $country[0];
}

How to enable or disable check boxes based on a click event?

I have a web app (PHP) and I have to make this change. I am more of a database, scripting guy, please bear with me on this one!
I have 8 check boxes (think numbered 1~8) in a form. I have to implement a condition where in :
If one of the first 4 checkboxes are checked (only one checkbox can be checked in the first 4),
Then the next 4 checkboxes should be disabled
Else the next 4 checkboxes should be enabled.
My solution :
Make the first 4 checkboxes radiobuttons to confirm to the only one
checkbox can be selected condition.
Disable/Enable the next 4 checkboxes based on the above action. So,
if the radiobutton is not selected, then the next 4 checkboxes should
be available for selection.
I have to actually disable the checkboxes rather than hide using jQuery, so the checkboxes should be solidgray (uncheckable) when disabled.
Sample code (stripped off some formatting mess for others looking for a similar solution) :
<div>
<input type="checkbox" name="check1" value="1" id="check1" <?php if (!empty($rows['check1'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check2" value="1" id="check2" <?php if (!empty($rows['check2'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check3" value="1" id="check3" <?php if (!empty($rows['check3'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check4" value="1" id="check4" <?php if (!empty($rows['check4'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check5" value="1" id="check5" <?php if (!empty($rows['check5'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check6" value="1" id="check6" <?php if (!empty($rows['check6'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check7" value="1" id="check7" <?php if (!empty($rows['check7'])) { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="check8" value="1" id="check8" <?php if (!empty($rows['check8'])) { echo 'checked="checked"'; } ?> />
</div>
My requests :
What is the most efficient way of doing this? (simple without complicating the problem)
Any sample code is greatly appreciated.
I think this is what you're looking for. You can achieve it using .index() to get current clicked checkbox. .slice() is used to get all elements at index 4 and after.
$('input[type=checkbox]').change(function(){
var $linputs = $('input[type=checkbox]').slice(4);
var $this = $(this);
$linputs.prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$linputs.prop('checked',false);
}
});​
FIDDDLE
Or is it something like this that you want? Where only one of the first four checkboxes can be checked. If one is checked then all the others will be disabled.
$('input[type=checkbox]').change(function(){
var $this = $(this);
$(linputs).prop('disabled',($this.index() < 4 && this.checked));
if($this.index() < 4 && this.checked){
$(linputs).prop('checked',false);
}
});​
http://jsfiddle.net/h5wDr/
EDIT:
if you have other checkboxes in the page and want to be able to separate them from this logic, you can add context in the selector so it keeps this code isolated to only those within this div like so
<div id='test'>
<input type="checkbox" name="check1" value="1" id="check1" >first
<input type="checkbox" name="check2" value="1" id="check2" >second
<input type="checkbox" name="check3" value="1" id="check3" >third
<input type="checkbox" name="check4" value="1" id="check4" >fourth
<input type="checkbox" name="check5" value="1" id="check5" >fifth
<input type="checkbox" name="check6" value="1" id="check6" >sixth
<input type="checkbox" name="check7" value="1" id="check7" >seventh
<input type="checkbox" name="check8" value="1" id="check8" >eight
</div>
Then just add the context
var $inputs = $('input[type=checkbox]', $('#test'));
// this will only select checkboxes within the element with id=test
http://jsfiddle.net/h5wDr/2/
<div>
<input type="checkbox" name="check1" value="1" id="check1" />
<input type="checkbox" name="check2" value="1" id="check2" />
<input type="checkbox" name="check3" value="1" id="check3" />
<input type="checkbox" name="check4" value="1" id="check4" />
<input type="checkbox" name="check5" value="1" id="check5" />
<input type="checkbox" name="check6" value="1" id="check6" />
<input type="checkbox" name="check7" value="1" id="check7" />
<input type="checkbox" name="check8" value="1" id="check8" />
</div>
<script>
$(document).ready(function () {
var $firstFourChecks = $("#check1,#check2,#check3,#check4");
var $lastFourChecks = $("#check5,#check6,#check7,#check8");
$firstFourChecks.on('click', function (e) {
var isCheck = $(this).is(':checked');
$firstFourChecks.not($(this)).prop('checked', false);
$(this).prop('checked', isCheck);
if (isCheck) {
$lastFourChecks.prop("disabled", true).prop('checked', false);
} else {
$lastFourChecks.prop("disabled", false);
}
});
});
</script>
This is entirely done in javascript, and is agnostic to the fact you are using php. Essentially, we make sure in the first four for you have not selected, they are set to false. Then we toggle the state of the one clicked.
If you clicked something on in your first four the last four are turned off and disabled, otherwise they are renabled. This matches the posted pseudocode.
You should be able to paste this directly in. The selectors are cached for speed reasons.
http://jsfiddle.net/L4qeN/ see it here.
Edit: wow looks like someone beat me to the punchline by only a few minutes. We did use very different methods; however.
You would have to try it yourself, but I would do something like (using javascript):
Add a class to every checkbox of the first group and another class to every checkbox of the second group;
Check for change events for the first class, turn off all of the first group but the clicked one;
Check if any of the first group is selected and activate / deactivate the second class group accordingly.
Give your first four checkboxes one class and then your second four a second class and then add onclick handlers to all the first checkboxes:
$('.firstfour_class').click(
if $('input:checkbox:checked.firstfour_class').length > 1){
//code to turn OFF the second four and make them unchecked
} else {
//code to turn ON the second four
}
})
Check out the jQuery :checked selector.

Using checkbox and Input field for inserting into DB

I have a form such as the one below:
<input type="checkbox" name="type" value="wash"><label>Wash</label><br>
<input type="checkbox" name="type" value="no wash"><label>No Wash</label><br>
<label>Other (Specify)</label><br>
<input name="type"><br>
If you notice for all three i am using "type" as the input name. The point being that the user will be given two options, if none of the two options apply to them they should enter a value in other. Now in the database i have the field type, so if they selected the first two and entered a value in the field or if they only wrote a value in the field i still want it to to be part of the type field. So how can i make it so that if they select the input field it should also insert in "type".
Do you mean something like this?
HTML:
<input type="checkbox" name="type[]" value="wash"/><label>Wash</label>
<input type="checkbox" name="type[]" value="no_wash"/><label>No wash</label>
Other type:
<input type="text" name="other_type"/>
PHP:
if (!empty($_REQUEST['other_type']))
$_REQUEST['type'][] = $_REQUEST['other_type'];
var_dump($_REQUEST['type']);
First of all you should better use radio buttons instead of checkboxes.
Then you could do the following
<input type="radio" name="type" value="wash"/><label>Wash</label>
<input type="radio" name="type" value="no_wash"/><label>No wash</label>
<input type="radio" name="type" value="other"/><label>Other</label>
<input type="text" name="other_type"/>
Your PHP would then look like this:
if ($_REQUEST["type"] == "wash"){
echo "Wash me please";
}else if ($_REQUEST["type"] == "no_wash"){
echo "no wash";
}else if ($_REQUEST["type"] == "other"){
echo "you want to ".$_REQUEST["other_type"];
}
If you use JS you could even disable the textbox unless the user selects the third option.
Edit: If I got your comment right it would be the easiest like this:
<input type="checkbox" name="wash_me"/><label>Wash your car?</label>
<input type="text" name="other"/><label>What else can we do for you?</label>
PHP
if (isset($_REQUEST["wash_me"]){
echo "wash my car please";
}
if (strlen($_REQUEST["other"]) != 0){
echo "and do the following: ".$_REQUEST["other"];
}

Radio Button Array - Need Help

I have a radio button array that I need help with. Here is the code:
<input type="radio" name="radio" id="academic" value="1"<?php
if ($row_EventInfo['academic'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['academic'] = '';}
?>>
<label for="academic">Academic</label><br />
<input type="radio" name="radio" id="personal" value="1"<?php
if ($row_EventInfo['personal'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['personal'] = '';}
?>>
<label for="personal">Personal</label><br />
<input type="radio" name="radio" id="diversity" value="1"<?php
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
?>>
<label for="diversity">Diversity</label><br />
What I'm trying to do is this. I have a column in my database table for each radio button because we have to have their inputs separately. However, I want them to only be able to select one of the buttons at a time. I changed all the names to be the same ("radio"), but since PHP MYSQL uses the names to know where to post the information in the table it doesn't know where to post.
Is there any way to create an if statement to tell it to only allow one button at a time to be selected and keep the inputs separate for the database table?
Please let me know if you need clarification. Thanks!
set the value to the column name, eg
<input type="radio" name="radio" id="diversity" value="diversity"
on the php end, simply do something like
$sql = "UPDATE table SET {$_POST['radio']} = 1";
this is in a form, right?
Then you get the data in the POST. I wouldn't set the value to 1, set the value to the value you want to select, e.g.
Then in the post, get the value and use this to set the data in the DB.
Hope this helps!
BR,
TJ
If you use:
<input type="radio" name="radio" id="diversity" value="value1">
<input type="radio" name="radio" id="diversity" value="value2">
<input type="radio" name="radio" id="diversity" value="value3">
<input type="radio" name="radio" id="diversity" value="value4">
it will be available in $_POST['radio']; with what ever radio button you selected. eg.value3 dont confuse yourself by naming input types with the same name
If you use:
<input type="radio" name="myradio" id="diversity" value="value1">
It will be available in $_POST['myradio'];
and so on
plus you may want to look into using the ternary operator
if ($row_EventInfo['diversity'] == '1') {
echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}
into
echo ($row_EventInfo['diversity'] == '1') ? ' checked="checked"':'';

Categories