I have created a contact form on my website using php and some of the values, more specifically the ones that have a checkbox-type input, are not send to my DB. I think there must be something wrong with the way I use the checkboxes or something. Upon submitting a form I get several notices like such:
Notice: Undefined index: ironing in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 79
Notice: Undefined variable: POST in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 94
The rest of the code seems to work fine and the data is loaded into my DB.
Here is some of my code in the demo-form.php file that I think is wrong:
<h4>i) Cleaningservice:</h4>
<p>Experience (in years): <input type="number" name="cleanExpInYears"/> </p>
<p>Skills: <br>
<input type="checkbox" name="basic" value="yes"> Basic cleaning<br>
<input type="checkbox" name="kitchen" value="yes"> Kitchen tools<br>
<input type="checkbox" name="laundry" value="yes"> Laundry<br>
<input type="checkbox" name="ironing" value="yes"> Ironing<br>
<input type="checkbox" name="window" value="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
And here is the corresponding demo.php file that connects to the DB and stores the data there:
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears'];
$clean_value2 = $_POST['basic'];
$clean_value3 = $_POST['kitchen'];
$clean_value4 = $_POST['laundry'];
$clean_value5 = $_POST['ironing'];
$clean_value6 = $_POST['window'];
$clean_value7 = $_POST['other_clean'];
/* ...and inserted into the table cleaningservice */
$sql = "INSERT INTO cleaningservice (applicant_id, cleanExpInYears, basic, kitchen, laundry, ironing, window, other)
VALUES (LAST_INSERT_ID(),'$clean_value1', '$clean_value2', '$clean_value3', '$clean_value4', '$clean_value5', '$clean_value6', '$clean_value7')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
What am I missing here ??
Thanks in advance
The browser only passes the checkbox to the PHP script if the checkbox was actually checked.
So in PHP you have to check that the $_POST variable actually exists before using its contents, and provide the UNCHECKED i.e. default option if it does not exist.
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears']);
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = $_POST['other_clean'];
To avoid error messages like `array index does not exist you should check all POST/GET values exist like this anyway.
/* Cleaningservice data is gathered... */
$clean_value1 = isset($_POST['cleanExpInYears']) ? $_POST['cleanExpInYears'] : '';
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = isset($_POST['other_clean']) ? $_POST['other_clean'] : '';
You must switch between name and values
<p>Skills: <br>
<input type="checkbox" value="basic" name="yes"> Basic cleaning<br>
<input type="checkbox" value="kitchen" name="yes"> Kitchen tools<br>
<input type="checkbox" value="laundry" name="yes"> Laundry<br>
<input type="checkbox" value="ironing" name="yes"> Ironing<br>
<input type="checkbox" value="window" name="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
and in PHP side, you will find your selected values in var_dump($_POST['yes'])
Related
I have been working on a contact form for a website and I got this problem:
I have 4 checkboxgroups, every each of them have at least 3 checkboxes that are available to check. We don't want them to be required to send the email. So the code is this:
$CheckboxGroup1 = array();
if(isset($_POST['submit'])){
$name = $_POST['name'];
$attending = $_POST['attending'];
$CheckboxGroup1 = isset($_POST['CheckboxGroup1']) ? $_POST['CheckboxGroup1'] : 'Nothing checked';
HTML:
<h4>What kind of set-up would you like?</h4>
<p>Additional fees may apply for living room/specialty set-ups.</p>
<p>
<div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Living Room">
Living Room
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Conference Room">
Conference Room
</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Other">
Other (please specify at the end of the form)
</label>
<br>
</div>
</p>
When none of the checkboxes is selected I get the message "Nothing checked" and when one of them is selected I get the value of it. The problem is when I select more than one, I get this in my email:
What kind of set-up will you like?: Array (not the name of those selected).
I do not know what I have to change to make it work the right way.
Any help would be very much appreciated.
You cannot print array with echo. Echoing array will give you string Array. Simple way in your case to get array values is to use implode:
echo implode(', ', $yourArray);
you have to change below line
Your code
$CheckboxGroup1 = isset($_POST['CheckboxGroup1']) ? $_POST['CheckboxGroup1'] : 'Nothing checked';
Change it to
if( isset($_POST['CheckboxGroup1']))
{
$CheckboxGroup1 =implode(", ",$_POST['CheckboxGroup1']);
}
else{
$CheckboxGroup1="Nothing checked";
}
I'm creating some simple search page for my little project. Its about list of Multifunctional devices and options on devices.
I have table devices and fields with device options:
option_hdd
option_fax
option_df (document feeder)
option_duplex
and so on... totally 8 option fields.
If device contain options in field is 1 if not 0.
I created small form with check boxess for all this option with GET method.
<form action="search.php" method="get" name="search">
<input name="HDD" type="checkbox" value="1" /> HDD<br />
<input name="Fax" type="checkbox" value="1" /> Fax<br />
<input name="DF" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
I try to create some query but i don't get exact results.
If user want to check for devices which contain hdd and fax option he will check that two checkboxes and hit search, query need to return results with devices that contain checked options (not essential for other options)
i try with fallowing query's i found googling :)
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' OR option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......'
and also
SELECT * FROM devices WHERE option_hdd = '$_GET[HDD]' AND (option_fax = '$_GET[Fax]' OR option_finisher = '$_GET[Df]' ......);
but i do'n get wanted results... but this last is the most close-up...
Example i have 5 devices in DB for testing
All 5 devices have Hdd and 2 devices have Fax option
In form i check hdd and fax and i get result of all 5 devices, but correct results must be 2... cause only two devices have fax option...
Can somebody help me and give me correct query :)
Thanks
I suggest using 'AND' but...
If the checkbox is checked, the value will be 1. If the checkbox is not checked the value will be empty.
So you code will be like this:
SELECT * FROM devices WHERE option_hdd = '1' AND option_fax = ' ' AND option_finisher = '1';
Which will give you a wrong resultset.
Try this instead:
$hdd = (!empty($_GET['HDD'])) ? $_GET['HDD'] : 0;
$fax = (!empty($_GET['Fax'])) ? $_GET['HDD'] : 0;
$df = (!empty($_GET['DF'])) ? $_GET['HDD'] : 0;
$sql = "SELECT * FROM devices WHERE option_hdd = '".$hdd."' AND option_fax = '".$fax."' AND option_finisher ='".$df."'";
Another option is to build your query. Change the name of the checkboxes like in the example below:
<form action="" method="get" name="search">
<input name="option[HDD]" type="checkbox" value="1" /> HDD<br />
<input name="option[Fax]" type="checkbox" value="1" /> Fax<br />
<input name="option[DF]" type="checkbox" value="1" /> DF<br />
...
<input name="Search" type="submit" />
</form>
And see the phpexample:
$sql = 'SELECT * FROM devices';
if(isset($_GET['option']) && count($_GET['option']) > 0){
$c = 0;
$sql .= ' WHERE';
foreach ($_GET['option'] AS $key => $option) {
$sql .= ($c == 0) ? ' option_'.$key.'='.$option : ' AND option_'.$key.'='.$option;
$c++;
}
}
Another solution is to use this trick:
<input name="HDD" type="hidden" value="0" />
<input name="HDD" type="checkbox" value="1" /> HDD<br />
This way if PHP will revert to default 0 if it doesn't get the value from checkbox.
And please be careful with your data, don't use $_GET directly in SQL.
This is my HTML code
<input type="checkbox" name="cbox[]" value="Jaywalking" />
Jaywalking<br>
<input type="checkbox" name="cbox[]" value="Littering" />
Littering<br>
<input type="checkbox" name="cbox[]" value="Illegal Vendor" />
Illegal Vendor
This is my PHP code
if(is_array($_POST['cbox'])) $violation=implode(',',$_POST['cbox']); else $violation=$_POST['cbox'];
mysql_query("insert into tblcitizen(violation) values ('$violation')",$conn) or die (mysql_error());
How can i update this checkbox? for example, i choose the jaywalking in my registration form, and i want to edit it, what do i need to do if i want it to be checked if i fetch it? please give a sample code. thank you much appreciated.
I would suggest to have another table related to tblcitizen as ManyToOne, so that you be able to create separate record for each submitted checkbox with the Id of the checkbox as value, then you will be able to update each record separately.
If you want to keep it in one table and save all value in one field separated by a delimiter and if the checkbox is not checked the value will not be save; for example if we have 3 checkboxes with these values jaywalking, littering, mysample and first, third ones are checked the value which is saved in Database should be jaywalking,mysample.
I assume this is your form on edit mode:
$result = mysqli_query("SELECT * FROM `tblcitizen` WHERE `id`='$id'");
$row = mysqli_fetch_assoc($result);
$selected_boxes = explode(',', $row['violation']);
<input type="checkbox" name="cbox[]" value="Jaywalking" <?php in_array('Jaywalking', $selected_boxes) ? 'checked' : '' ?> /> Jaywalking<br>
<input type="checkbox" name="cbox[]" value="Littering" <?php in_array('Littering', $selected_boxes) ? 'checked' : '' ?> /> Littering<br>
<input type="checkbox" name="cbox[]" value="Illegal Vendor" <?php in_array('Illegal Vendor', $selected_boxes) ? 'checked' : '' ?> /> Illegal Vendor
Now to edit after submit
if(is_array($_POST['cbox'])) $violation=implode(',',$_POST['cbox']); else $violation=$_POST['cbox'];
$query = "UPDATE `tblcitizen` SET `tblcitizen` = '$violation' WHERE `id`='$id'";
...
This is the simplest way to implement this logic
form :
<label for="sitecheck">
<span style="font-weight:bold;">סגור אתר זמנית:</span>
</label>
<input name="sitecheck" type="checkbox" id="sitecheck" onClick="validateSitec();" <?php if($data['sclosed'] == 'true'){echo 'checked = "checked"';}; ?> /><span style="font-weight:bold;">סגור אתר ורשום הודעה זמנית</span><br>
<input type="text" name="closedmsg" id="closedmsg" style="width:440px;height:120px;<?php if($data['sclosed'] == 'true'){echo '';}else{ echo 'display:none;'; }; ?>" value="<?php echo $data['csitemsg']; ?>" /><span id="sitemsg_error"></span>
php :
if(isset($_POST['sitecheck']))
{
$sitecheck = 'true';
}else{
$sitecheck = 'false';
}
any idea why its not working ?
i trying to determine if checked or not and update on Database.. anyidea why its alwys true even if i uncheck?
EDIT :
fixed by my own was ajax post wrong , i should val the checked box like that :
var sitecheck = $('#sitecheck').is(':checked') ? $('#sitecheck').val() : '' ;
so now working thanks.
Add value param to your checkbox. Value will be send if checkbox is checked.
<input name="sitecheck" type="checkbox" id="sitecheck" value="1" ....
Basically I can get the value that applies to when the box is ticked (= 1), but I can't get it to send the default value of 0 when not checked.
<input type="checkbox" name="post_friend" value="1">
I've searched around and someone suggested setting a hidden checkbox, but it's not working for me.
<input type="hidden" name="post_friend" value="0">
Could you not do something like this?
$checkbox = isset($_POST['post_friend']) ? $_POST['post_friend'] : 0 ;
So if the checkbox is checked, variable is 1. If not, variable assigned value of 0
The original method which was given by the OP should work, however, the value may very well be different.
For example,
<input type="checkbox" name="post_friend" value="1">
<input type="hidden" name="post_friend" value="0">
In this case ["post_friend"] = 0.
However, in this example:
<input type="checkbox" checked="checked" name="post_friend" value="1">
<input type="hidden" name="post_friend" value="0">
Most browsers will send ["post_friend"] = "1,0".
Multiple values to the same property will usually be concatenated in the http request.
For this you can use String.Contains to find the 1.
However, here, you should really assess whether your input should be a checkbox, or more suitably, radio input.
Try something like below
<div class="col-xs-5 drinkingLeft">
<input type="checkbox" name="beer" id="beer"class="require-one col-xs-1" value="0"/>
<input id='beerHidden' type='hidden' value='0' name='beer'>
<label for="beer" class="col-xs-10">Beer </label>
</div>
$('#beer').on('change', function () {
this.value = this.checked ? 1 : 0;
}).change();
$("#submit").click(function () {
if(document.getElementById("beer").checked){
document.getElementById('beerHidden').disabled = true;
}
});
HTML
<input type="checkbox" id="chkbox1">
Jquery
if( $("#chkbox1").is(':checked')){
value = "True";
}else{
value= "False";
}
the only possible solution is :
<?php
// if data is posted, set value to 1, else to 0
$check_0 = isset($_POST['check'][0]) ? 1 : 0;
$check_1 = isset($_POST['check'][1]) ? 1 : 0;
?>
If you want to check info on the same page then use JS and check NULL values...