count html radio buttons value with php - php

I have a html form with grouped radio button. This form contains questions with yes or no options
<form action="results.php" method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
I know i can count the number of radio buttons with name q
$count_cbox = count($_POST['q'])
But is it possible that when the user makes a choice i count the radio button value that ="yes" or "no".

Please change your radio button names slightly. Otherwise the grouping doesn't work:
<form method="post" enctype="multipart/form-data"><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q[0]" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q[0]" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q[1]" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q[1]" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<p>Can your concept be easily described and understood?</p>
<div id="q_1">
<input type="radio" name="q[2]" id="q_3_yes" value="yes">
<label for="q_3_yes">Yes</label>
<input type="radio" name="q[2]" id="q_3_no" value="no">
<label for="q_3_no">No</label>
</div><br>
<input type="submit" name="sub_eit" id="sub_eit" value="Submit">
</div>
</form>
The PHP code:
for($i = 0; $i < count($_POST['q']); ++$i) {
if($_POST['q'][$i] == 'yes') {
++$yes;
}
}
Now $yes contains the number of radio buttons with the value yes. In this case 0-3. Please pay attention to the fact that if no radio button is selected, it will return NULL, not 0.

Related

Inserting multiple rows from multiple question radio form in PHP

I have a form to determine the user's personality. So each question (there are 25) has 4 possible answers to choose from:
<form method="post">
<div>
<label for="name"></label>
<input name="name" id="name" placeholder="Name">
</div>
<div>
<label for="surname"></label>
<input name="surname" id="surname" placeholder="Surname">
</div>
<br>
<div>
<label for="q1">Question 1</label><br>
<input type="radio" name="q1" id="q1" value="S"> q1c1 <br>
<input type="radio" name="q1" id="q1" value="D"> q1c2 <br>
<input type="radio" name="q1" id="q1" value="K"> q1c3 <br>
<input type="radio" name="q1" id="q1" value="I"> q1c4 <br>
</div>
<br>
<div>
<label for="q2">Question 2</label><br>
<input type="radio" name="q2" id="q2" value="D"> q2c1 <br>
<input type="radio" name="q2" id="q2" value="K"> q2c2 <br>
<input type="radio" name="q2" id="q2" value="I"> q2c3 <br>
<input type="radio" name="q2" id="q2" value="S"> q2c4 <br>
</div>
<br>
<div>
<label for="q3">Question 3</label><br>
<input type="radio" name="q3" id="q3" value="S"> q3c1 <br>
<input type="radio" name="q3" id="q3" value="I"> q3c2 <br>
<input type="radio" name="q3" id="q3" value="D"> q3c3 <br>
<input type="radio" name="q3" id="q3" value="K"> q3c4 <br>
</div>
<br><br>
<button>Submit</button>
</form>
I need to insert the name, surname, and the selection of the question in one line. The next row in the database should also contain the name, surname, and then the next question's selection for all 25 questions.
My table users in the database only has 3 columns namely: name, surname, q
I am very new to PHP and tried some previous questions but none worked for my particular scenario.
I assume you are working fully with PHP, so no JavaScript or Ajax involved.
first of all you need to specify where your request should send:
<form method="post" action="https://example.org/target.php">
in target.php, you need to read data out of $_POST. In your case, it must be something like $_POST['name'].
From there on, you can do whatever you want with the data.
Please consider this answer as a minimal question as you need to do further checks, such as security checks, prevent SQL injections and overall data validation.

How to select another one of radio button?

I made radio buttons and all radio buttons are selected, where I try my code.
How to choose just one radio button in sintaks laravel?
This is My View Page :
<div class="form-group">
<b>Paket</b>
<br/>
<fieldset>
<input type="checkbox" name="delux" id="delux" value="d"> <label for="">Paket Delux </label>
<input type="checkbox" name="paket1" id="p1" value="p1"> <label for="">Paket 1</label>
<input type="checkbox" name="paket2" id="p2" value="p2"> <label for="">Paket 2</label>
</fieldset>
</div>
<div class="form-group">
<b>Jenis Pembayaran</b>
<br/>
<fieldset>
<form id="form_radio" name="form_radio">
<input type="radio" value="tunai" name="tunai" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="nontunai" id="rd2"> <label for="">non tunai</label>
</fieldset>
</div>
<input type="submit" value="Upload" class="btn btn-primary">
</form>
And this is My controller :
public function input()
{
$jenis = JenisMkn::select('id_jenis','jenis_makanan')->get();
return view('upload_gambar',['jenis'=>$jenis]);
}
public function proses(Request $request)
{
$cek = Gambar::get('checkbox');
echo $cek;
$radio = Gambar::get('radio');
echo $radio;
What the fault in my code?
Any help? Thank you.
Simply give them the same name, cek this code:
<input type="radio" value="tunai" name="transaksi" id="rd1"> <label for="">tunai</label>
<br>
<input type="radio" value="non" name="transaksi" id="rd2"> <label for="">non tunai</label>
You can rename the name input with your name.
If you want only one radio button to get selected then you must specify same value for name property. for example:
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
Hope it helped.

how to use checkbox and input type number together in php

I am creating a meeting room booking application using php. In that, when a person book a room there have an option to book tea or snacks and specify the number of items they need. For that, I use checkboxes to select items and input type number to specify the number of items.i.e, if tea selected you have to specify the no. of tea in the input field. My problem is that I can't store or display the checkbox value and associated number together.my code is
<form class="form-horizontal" method="post" id="bookroom" action="bookedreview.php">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[]">
</div>
<input type="submit" name="submit" value="value">
</form>
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
//$food=implode(',',$_POST['food']);
foreach($foodnum as $index =>$value){
$fud=$value;
$num=$foodnum['index'];
}
}
but when I display the variable I couldn't get the result. Can anyone help me how could store the checked items and the associated quantity together.
You can get with passing food name in textbox name array
<form class="form-horizontal" method="post" id="bookroom" action="">
<h4>Food & Beverages</h4>
<div class="checkbox">
<input type="checkbox" value="tea" name="food[]"><label>Tea</label>
<input type="number" name="foodnum[tea]">
</div>
<div class="checkbox">
<input type="checkbox" value="coffee" name="food[]"><label>Coffee</label>
<input type="number" name="foodnum[coffee]">
</div>
<div class="checkbox">
<input type="checkbox" value="snacks" name="food[]"><label>Snacks</label>
<input type="number" name="foodnum[snacks]">
</div>
<div class="checkbox">
<input type="checkbox" value="water" name="food[]"><label>Water</label>
<input type="number" name="foodnum[water]">
</div>
<div class="checkbox">
<input type="checkbox" value="nuts" name="food[]"><label>Nuts</label>
<input type="number" name="foodnum[nuts]">
</div>
<div class="checkbox">
<input type="checkbox" value="breakfast" name="food[]"><label>Breakfast</label>
<input type="number" name="foodnum[breakfast]">
</div>
<div class="checkbox">
<input type="checkbox" value="lunch" name="food[]"><label>Lunch</label>
<input type="number" name="foodnum[lunch]">
</div>
<div class="checkbox">
<input type="checkbox" value="dinner" name="food[]"><label>Dinner</label>
<input type="number" name="foodnum[dinner]">
</div>
<input type="submit" name="submit" value="value">
</form>
<?php
if (isset($_POST['submit']))
{
$foodnum=$_POST['foodnum'];
$food=$_POST['food'];
echo "<table border='1' style='width:100%'><tr><th>Food Name</th><th>Count</th></tr>";
// output data of each row
foreach($food as $foo)
{
echo "<tr><td>".$foo."</td><td>".$foodnum[$foo]."</td></tr>";
}
echo "</table>";
}
?>
For Insert
foreach($food as $foo)
{
$fieldVal1=$foo;
$fieldVal1=$foodnum[$foo];
$query ="INSERT INTO foodcounts( foodsname, cnt) VALUES ('".$fieldVal1."','".$fieldVal2."' )";
mysqli_query($conn, $query);
}

PHP - Getting Multiple Radio Buttons Values from From Submission

I am a newbie in PHP. I have a form like below:
<div class="col-xs-2 col-xs-offset-1">
<input id="radio-ge-1" class="radio-custom" name="radio-ge" type="radio" value="1" checked>
<label for="radio-ge-1" class="radio-custom-label tooltips">1 <span>Impossible to get marks</span></label>
</div>
<div class="col-xs-2">
<input id="radio-ge-2" class="radio-custom" name="radio-ge" value="2" type="radio">
<label for="radio-ge-2" class="radio-custom-label tooltips">2 <span>You have to work hard to get marks</span></label>
</div>
<div class="col-xs-2">
<input id="radio-ge-3" class="radio-custom" name="radio-ge" value="3" type="radio">
<label for="radio-ge-3" class="radio-custom-label tooltips">3 <span>The usual, just like any other class</span></label>
</div>
<div class="col-xs-2 col-xs-offset-1">
<input id="radio-sl-1" class="radio-custom" name="radio-sl" value="1" type="radio" checked>
<label for="radio-sl-1" class="radio-custom-label tooltips">1 <span>Unbearable pressure</span></label>
</div>
<div class="col-xs-2">
<input id="radio-sl-2" class="radio-custom" name="radio-sl" value="2" type="radio">
<label for="radio-sl-2" class="radio-custom-label tooltips">2 <span>High pressure, But doable</span></label>
</div>
<div class="col-xs-2">
<input id="radio-sl-3" class="radio-custom" name="radio-sl" value="3" type="radio">
<label for="radio-sl-3" class="radio-custom-label tooltips">3 <span>Fair, just like any other class</span></label>
</div>
I'm trying to save both radio buttons value in PHP by using the code:
$g_Easiness = $_POST['radio-ge'];
$s_Load = $_POST['radio-sl'];
But in this case, I'me only getting the first button's value. Second button's value = on.
I have spend more than two hours to figure it out. But cannot resolve it. Can anyone please help me to solve the problem?
Please use below code for radio buttons
if input type="radio" then it should give value="" otherwise in value it will show "on"
e.g.
<input id="radio-must" class="radio-custom" name="radio-nns" value="absolutely" type="radio" checked>
output = [radio-nns] => on
instead put ( you can see value="1")
<input id="radio-must" class="radio-custom" name="radio-nns" value="absolutely" type="radio" checked value="1">
output = [radio-nns] =>1

Twitter Bootstrap HTML form radio button not working with PHP POST

For some reason the radio button value from my form is not being passed to the form PHP script on some web browsers when using the Twitter Bootstrap framework.
Form:
<form action="" method="post">
<li class="stream-item stream-header search-input-item">
<span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="input-xxlarge" id="appendedInputButton" autocomplete="off" name="q">
<button class="btn btn-primary" type="submit">Search</button>
</li>
</div>
<p></p>
<label class="radio">
<input type="radio" name="opt" value="1" checked>
Option 1
</label>
<label class="radio">
<input type="radio" name="opt" value="2">
Option 2
</label>
</form>
This the PHP code for collecting the variable is one simple line:
$option = $_POST['opt'];
This method works absolutely fine with Chrome but doesn't work in IE, Firefox or Opera.
Can anyone shed any light on this?
How are you submitting the data? I cannot see a submit button.
The code works fine for me in FF using the following (notice I added a submit button):
<form action="" method="post">
<li class="stream-item stream-header search-input-item">
<span class="add-on"><i class="icon-search"></i></span>
<input type="text" class="input-xxlarge" id="appendedInputButton" autocomplete="off" name="q">
<button class="btn btn-primary" type="submit">Search</button>
</li>
</div>
<p></p>
<label class="radio">
<input type="radio" name="opt" value="1" checked>
Option 1
</label>
<label class="radio">
<input type="radio" name="opt" value="2">
Option 2
</label>
<input type="submit" name="submit" value="submit" >
</form>
Result from print_r($_POST);
Array ( [q] => [opt] => 1 [submit] => submit )

Categories