I am creating an HTML form and I have a textbox in that. My requirement is that a user can check multiple check boxes, and then I have to get all checked values and then send the values to the database (I want to use PHP).
Here is my code for within the textbox
$Intensive=$_POST['Intensive'];
$Intensive_count=count($Intensive);
$i=0;
//$count=0;
//$index;
$max=0;
//$index;
While($i < $Intensive_count)
{
if($Intensive[$i]=="High frequency ventilation" )
{
$Intensive_score=4;
}
elseif($Intensive[$i]=="Mechanical ventilation with muscle relaxation")
{
$Intensive_score=4;
}
elseif($Intensive[$i]=="Mechanical ventilation")
{
$Intensive_score=3;
}
elseif($Intensive[$i]=="CPAP")
{
$Intensive_score=2;
}
elseif($Intensive[$i]=="supplemental oxygen")
{
$Intensive_score=1;
}
if($Intensive_score>$max)
{
$max=$Intensive_score;
$index=$i;
}
$i++;
}
Now with the above code I am able to echo the value ,but the record is not going to the database.
$sql1="insert into Form2 values('$Medical_Record_Id','$sub1','$Question1','4','$Intensive[$index]','$max')";
mysql_query($sql1);
Could anybody tell me how to go about it.
Thanks ..:)
Assuming you are using POST as the method for sending the form those checkboxes are in, you can get the values array with $_POST['Intensive'].
I would recommend you use integers for the value instead of long strings, also an ID is supposed to be unique, so modify your ids.
HTML:
<input type="checkbox" name="Intensive[]" id="r1" value="1">supplemental oxygen<br>
<input type="checkbox" name="Intensive[]" id ="r2" value="2">supplemental oxygen<br>
<input type="checkbox" name="Intensive[]" id="r3" value="3">Mechanical ventilation<br>
<input type="checkbox" name="Intensive[]" id="r4" value="4">Mechanical ventilation with muscle relaxation<br>
<input type="checkbox" name="Intensive[]" id="r5" value="5">High-frequency ventilation
PHP:
foreach($_POST['Intensive'] as $data) {// Or $_GET
if ($data == 1){
/// do so and so
}
if ($data == 2){
/// do so and so
}
... and so on.
}
Related
I have a form that gets validated when submit. I make sure some fields are not empty, date fields have a valid format, etc. I have a block of check boxes with a text input next to it.
Option 1
<input type="checkbox" name="jobBonusId[]" value="1" />
Option 2
<input type="checkbox" name="jobBonusId[]" value="2" />
Option 3
<input type="checkbox" name="jobBonusId[]" value="3" />
Amount 1
<input type="number" name="jobBonusAmount[]" step="0.01" value="0.00" />
Amount 2
<input type="number" name="jobBonusAmount[]" step="0.01" value="0.00" />
Amount 3
<input type="number" name="jobBonusAmount[]" step="0.01" value="0.00" />
So, if any of the jobBonusId[] checkbox gets checked it creates an array. Now, I want to validate the jobBonusAmount[] if its -parent- checkbox was checked and make sure is not empty or equals 0.00.
So far, I have the following code:
// Run the script
if (isset($_POST['addJobRecord']) && $_POST['addJobRecord']=='oTzm50xfm') {
// Validate date format
if (!validateDate($jobDateStart) || !empty($jobDateEnd) && !validateDate($jobDateEnd)) {
// Show the form
$displayContent = $displayForm;
// Validate data dates
} else if ($jobTimeIn>$jobTimeOut) {
// Show the form
$displayContent = $displayForm;
// Validate bonus values
} else if (!empty($jobBonusId) && is_array($jobBonusId)) {
// At least one jobBonusId checkbox was checked
// Make sure its child input is not empty
... HERE'S WHERE I'M STUCK ...
} else {
// Everything looks good
// Add record to database
}
}
Any ideas how to accomplish it?
Thank you much!
You can use array key, and check child by jobBonusId key:
foreach ($jobBonusId as $key => $bonusId) {
if (!empty($bonusId)) {
if (!empty($jobBonusAmount[$key])) { // check a child
// if child is filled
} else {
// not filled
}
}
}
It means, that $jobBonusId array is the same to $jobBonusAmount array
I have a form of checkboxes as shown below:
<form method="POST" action="display.php">
<input type="checkbox" value="1" name="options[]">
<span class="checkboxText"> Fruits</span>
<input type="checkbox" value="2" name="options[]">
<span class="checkboxText">Vegetables </span><br><br>
<button class="button" type="submit" value="display">DISPLAY</button>
</form>
I get the options[] using $_POST['options'] and save the array of data in a variable. I want to display the array of fruits if the fruits checkbox is checked, the vegetables array if vegetables checkbox is checked and display both of them if both are checked and display a message saying "Fruits and Vegetables are healthy". This is the php code I have so far but it does not seem to work as I would like it to.
<?php
$values = $_POST['options'];
$n = count($values);
for($i=0; $i < $n; $i++ )
{
if($values[$i] === "1" && $values[$i] == "2")
{
//iteration to display both tables
echo 'Fruits and Vegetables are healthy';
}
else if($values[$i] === "1")
{
//display fruits
}
else if( $values[$i] == "2")
{
//display vegetables
}
}
?>
The problem with my php code is that is does not go into the first if at all. It just displays both tables from the other two ifs (since the echo is not displayed either). Is there any way I could solve this?
You shouldn't need a loop for this. You just need to check in $_POST['options'] for each of the values in question. I would suggest using the text you want to display as the values for your checkboxes so you don't have to convert from numbers to words.
<input type="checkbox" value="Fruits" name="options[]">
<span class="checkboxText"> Fruits</span>
<input type="checkbox" value="Vegetables" name="options[]">
<span class="checkboxText">Vegetables </span><br><br>
Then for the display, just output the fruits/vegetables arrays depending on whether or not those values are present in $_POST['options'].
if (!empty($_POST['options'])) {
echo implode(' and ', $_POST['options']) . " are healthy";
if (in_array('Fruits', $_POST['options'])) {
// show the fruits
}
if (in_array('Vegetables', $_POST['options'])) {
// show the veg
}
}
I want to store multiple checkbox value in database ,and my checkbox name is differnt,how can save ??.I have three column like Called them,Called you,toured, how i can store each value in data base.
HTML Code
<input type="checkbox" name="Called_them[]" value="1">1<br>
<input type="checkbox" name="Called_them[]" value="2">2<br>
<input type="checkbox" name="Called_you[]" value="1">1<br>
<input type="checkbox" name="Called_you[]" value="2">2<br>
PHP Code:-
if(isset($_POST['submits']) )
{
$checked = $_POST['alled_them'];
$checked1 = isset($_POST['alled_them']);
for($i=0; $i < count($checked); $i++){
$tellfriend=new MemberEmails;
$tellfriend->called_them = isset($checked[$i])==0?'':$checked[i];
$tellfriend->save();
}
if(isset($_POST["submit"])) {
foreach($_POST['called_them'] as $called_them) {
//do things
}
foreach($_POST['called_you'] as $called_you) {
// do things
}
}
$array_called_them = $_POST['called_them'];
$array_called_them = $_POST['called_you];
You now have both checkbox groups as arrays. if you want the value from the array just use foreach on each of the arrays.
How can I use the conditional OR in a form with isset?
I have this but it does not work.
FORM HTML:
...
<input type="checkbox" name="carga1">
<input type="checkbox" name="carga2">
...
and the PHP
$cargas=array($_POST['carga1'],$_POST['carga2'],$_POST['carga3'],
$_POST['carga4'],$_POST['carga5'],$_POST['carga6'],
$_POST['carga7'],$_POST['carga8'],$_POST['carga9'],
$_POST['carga10'],$_POST['carga11'],$_POST['carga12'],
$_POST['carga13'],$_POST['carga14'],$_POST['carga15'],
$_POST['carga16'],$_POST['carga17'],$_POST['carga18']);
if(isset($cargas[0]) ││ isset ($cargas[1])){
$cargas[0]=5.62;
$cargas[1]=4.5;
echo "$cargas[0]<br>";
echo "$cargas[1]<br>";
}
i expect that this works but is not.
Only checked checkbox is posted to the server.You have to change your condition using pregmatch and work accordingly.
$postData = $_POST;
foreach ($postData as $key => $value) {
$match = preg_match('|cargas(\d+)|', $key, $matches);
if ($match) {
$index = $matches[1];
if($index == 0 || $index == 1){
// do your stuff which you would have done in case of $cargas[0] ,$cargas[1]
}
}
}
I think array is not Suitable way to do this try following
try this
<input type="checkbox" name="carga1">
<input type="checkbox" name="carga2">
.....................................
<input type="submit" name="submit">
<?php
if(isset($_POST['submit'])){
//
$category1=$_POST['carga1'];
$category2=$_POST['carga2'];
$category3=$_POST['carga3'];
if(isset($category1) ││ isset ($category2)){
$category1=5.62;
$category2=4.5;
echo "$category1<br>";
echo "$category2<br>";
}
}
?>
only the checked checkboxes get posted. so it needs slightly different appraoch.
You can acheive it like this-
put a hidden input with the same name as the checkbox that might not be checked. I think it works so that if the checkbox isn't checked, the hidden input is still successful and sent to the server but if the checkbox is checked it will override the hidden input before it. This way you don't have to keep track of which values in the posted data were expected to come from checkboxes.
<form>
<input type='hidden' id='testName' value='0' name='carga1'>
<input type='checkbox' id='testNameHidden' value='1' name='carga1'>
</form>
Before submitting the form , disabled the hidden field based on the checked condition.
<script>
if(document.getElementById("testName").checked){
document.getElementById('testNameHidden').disabled = true;
}
</script>
I personally think its the easiest approach for this.
ok, check boxes in html works as follows,
<input type="checkbox" name="carga1" value="1">
<input type="checkbox" name="carga2" value="123">
in php,
if the check box is in checked state during the submission, you will get
isset($_POST['carga1']) as true, else the form element would not be available in post data, hence false.
and in cheked state you will get value for
$_POST['carga1'] as 1 and
$_POST['carga2'] as 123
and if you want to group the check boxes in form you can use a single name for multiple check boxes and different values,
<input type="checkbox" name="carga[]" value="1">
<input type="checkbox" name="carga[]" value="2">
<input type="checkbox" name="carga[]" value="3">
<input type="checkbox" name="carga[]" value="4">
and in php you will get an array of selected values of the check boxes
$arr=$_POST['carga'];
and you can use foreach to iterate through the values,,,
I have a text field and two checkboxes, I need to list users based on the selection. Can anyone show me an example.
See:
Enumerate all Check Box in PHP
<input name="rows[]" value="someVal" type="checkbox" />
<input name="rows[]" value="anotherVal" type="checkbox" />
<?php
// $_POST['rows'] contains the values of all checked checkboxes
//if something has been checked
if(isset($_POST['rows'])) {
//loop over each checked value
foreach ($_POST['rows'] as $row) {
echo $row . '<br />';
}
}
?>
if (isset($_POST['mycheckbox']))
{
draw_selectionCheckboxChecked();
}
else
{
draw_NoCheckbox();
}