PHP - Getting Multiple Radio Buttons Values from From Submission - php

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

Related

Bootstrap radio button already checked not worked

This is my code of radio button i want image radio buttton is alredy check but it's not working
<div class="col-md-4" style="padding-top: 24px;" >
<label>Visual</label>
<div>
<label class="radio-inline">
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off"><?php echo __('Image') ?>
</label>
<label>or</label>
<label class="radio-inline">
<input value="video" type="radio" name="media_option[]" class="media_option" autocomplete="off" ><?php echo __('Video') ?>
</label>
</div>
Try this.
Don't set radio name as array and not need to set autocomplete attribute.
<div class="col-md-4" style="padding-top: 24px;" >
<label>Visual</label>
<div>
<label class="radio-inline">
<input type="radio" value="image" name="media_option" class="media_option" checked="checked" ><?php echo __('Image') ?>
</label>
<label>or</label>
<label class="radio-inline">
<input value="video" type="radio" name="media_option" class="media_option" ><?php echo __('Video') ?>
</label>
</div>
Its working fine, check here:
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off">One
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off">Two
when you are using radio, only one radio can be selected within one group (having same name). The same is working here.
How about this?
This will always work
<input type="radio" value="image" checked="checked" name="media_option[]" class="media_option" autocomplete="off">
If not try this
<input checked type="radio" value="image" name="media_option[]" class="media_option" autocomplete="off">
Just change the checked="checked" to checked only :
Here is the code ,please try:
<div class="col-md-4" style="padding-top: 24px;" >
<label>Visual</label>
<div>
<label class="radio-inline">
<input type="radio" value="image" checked name="media_option[]" class="media_option" autocomplete="off"><?php echo __('Image') ?>
</label>
<label>or</label>
<label class="radio-inline">
<input value="video" type="radio" name="media_option[]" class="media_option" autocomplete="off" ><?php echo __('Video') ?>
</label>
/div>
I came here because I had the same issue and without seeing the rest of the code in the question, won't make any assumptions.
However, if the code above wasn't within a form element, that could have caused the issue.
That was the issue in my case, after placing the radio elements within the form, the checked attribute worked again as expected. Hopefully this helps someone else out as well.

count html radio buttons value with 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.

radio button "Checked" option not working

Could you please help me why my checked option in the following code is not working?
<div class="span1">
<h7> Sex</h7>
<label class="radio">
<input id="pccf_sexMF" name="pccf_sexMF" value="M" type="radio" checked class="span1" > M
</label>
<label class="radio">
<input id="sexMF" name="pccf_sexMF" value="F" type="radio" class="span1"> F
</label>
</div>
Thanks
It works on this fiddle
<div class="span1">
Sex
<label class="radio">M</label>
<input id="pccf_sexMF" name="pccf_sexMF" value="M" type="radio" checked="checked" class="span1"/>
<label class="radio">F</label>
<input id="sexMF" name="pccf_sexMF" value="F" type="radio" class="span1"/>
</div>
First - no such thing as h7
Only text should be in a label, not the input
As I said in my comment, you could have another element with position:absolute that will cover your div because its height is bigger than you need it to be. Something like this:
http://jsfiddle.net/hescano/8T7Nc/
To avoid this, you can set your div a relative position:
<div class="span1" style="position:relative">
http://jsfiddle.net/hescano/8T7Nc/1/

Text for checkboxes not appearing in Drupal 6

This is a Drupal 6 build, I'm trying to figure out why the checkboxes are not showing text. Here's an image of what's going on - you can see that the checkboxes are empty.
The actual generated code is as follows:
<div id="attach-wrapper"><div id="edit-files-392-description-wrapper" class="form-item">
<input type="text" class="form-text" value="2011 SchoolAgePQA_Sample_Items.pdf" size="60" id="edit-files-392-description" name="files[392][description]" maxlength="256">
<div class="description"><small>http://domain.org/sites/domain.org/files/2011 SchoolAgePQA_Sample_Items.pdf</small></div>
</div>
492.94 KB<div id="edit-files-392-remove-wrapper" class="form-item">
<input type="checkbox" class="form-checkbox" value="1" id="edit-files-392-remove" name="files[392][remove]">
</div>
<div id="edit-files-392-list-wrapper" class="form-item">
<input type="checkbox" class="form-checkbox" value="1" id="edit-files-392-list" name="cats">
</div>
<div id="edit-files-392-weight-wrapper" class="form-item">
<select id="edit-files-392-weight" class="form-select" name="files[392][weight]"><option value="-8">-8</option><option value="-7">-7</option><option value="-6">-6</option><option value="-5">-5</option><option value="-4">-4</option><option value="-3">-3</option><option value="-2">-2</option><option value="-1">-1</option><option selected="selected" value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option></select>
</div>
<div id="edit-files-392-private-wrapper" class="form-item">
<input type="checkbox" class="form-checkbox" value="1" id="edit-files-392-private" name="files[392][private]">
</div>
The only thing that sticks out to me is that the value of the checkboxes is exactly the same as the name - and those are all various positive and negative integers.
I've never seen this before, and I'm not sure what might cause it - incorrect configuration setting, poorly coded theme, something else?
There's no label there, so no text is going to be displayed. In drupal it should allow you to change the label and set a value.
The output code should look similar to
<label><input type="checkbox" class="form-checkbox" value="1" id="edit-files-392-list" name="cats">Cats</label>
Try to add label or simply text after your checkbox:
<div id="edit-files-392-list-wrapper" class="form-item">
<label> Text here
<input type="checkbox" class="form-checkbox" value="1" id="edit-files-392-list" name="cats">
</label>
</div>
Or simply :
<div id="edit-files-392-list-wrapper" class="form-item">
<input type="checkbox" class="form-checkbox" value="1" id="edit-files-392-list" name="cats">
Text here
</div>
But i recommande the first solution.

html5 form not reacting to submit

I've got a simple one-page form, the kind I've written dozens of times, but this behavior is totally new to me. At first, it was submitting on page-load, thus redirecting automatically to the next page in the series. I'm still not sure how I got that to stop, but now it's not doing anything at all when you hit "submit". The page simply sits there.
I've tried stripping out the error-checking scripts, the show/hide script, even jquery itself, then taking the form down to just one input. I tried getting rid of the redirect and just having it output a simple line, then vardump, and still nothing. I hit submit, and no matter what I do, the page just sits there. I've never run into behavior like this before, and firebug et al give me no errors or leads.
If anyone has any ideas at all, no matter how crazy, I'm willing to try. I'm at a loss. Thanks in advance!
<?php
session_start();
include('functions.php');
if ($_POST['submit']) {
$company = $_POST['company'];
$taxid = $_POST['taxid'];
header("location:step2.php");
}
include('head.php'); ?>
<form method="post" name="basic" action="step1.php">
<div class="col70">
<label for="company">Organization/Business name <img src="img/req.jpg" alt="required"></label>
<input type="text" name="company" id="company" value="" />
</div>
<div class="col30">
<label for="taxid">Taxpayer ID# (IEN or SS#) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="taxid" id="taxid" value="" />
</div>
<div class="newcol">
<label for="address">Mailing Address <img src="img/req.jpg" alt="required"></label>
<input type="text" name="address" id="address" value="" />
</div>
<div class="col30 newcol">
<label for="city">City <img src="img/req.jpg" alt="required"></label>
<input type="text" name="city" id="city" value="" />
</div>
<div class="col30">
<label for="state">State <img src="img/req.jpg" alt="required"></label>
<select name="state" id="state" tabindex="<?php echo $tabin; $tabin++; ?>">
<option value="0"></option>
<option value="1">Alabama</option>
</select>
</div>
<div class="col25">
<label for="zipcode">Zip Code <img src="img/req.jpg" alt="required"></label>
<input type="text" name="zipcode" id="zipcode" value="" />
</div>
<fieldset><legend>1. What kind of group/company do you have? <img src="img/req.jpg" alt="required"></legend>
<span id="nfpfp" class="InputGroup">
<label><input name="nfpfp" id="nfpfp_1" type="radio" value="1" />For-profit business.</label>
<label><input name="nfpfp" id="nfpfp_2" type="radio" value="2" />Non-profit 501(c)3 organization.</label>
</span>
</fieldset>
<fieldset><legend>2. How will you use the booth space? Select all that apply. <img src="img/req.jpg" alt="required"></legend>
<span id="type" class="InputGroup">
<label><input name="food" id="type_1" type="checkbox" value="1" />Food sales</label>
<label><input name="retail" id="type_2" type="checkbox" value="2" />Retail sales</label>
<label><input name="activity" id="type_3" type="checkbox" value="3" />Activity</label>
<label><input name="display" id="type_4" type="checkbox" value="4" />Display</label>
<label><input name="other" id="type_5" type="checkbox" value="5" />Other</label>
</span>
</fieldset>
<label for="otherdetails" class="newcol offsides">Enter a short description of your use. (Ex: "BBQ sandwiches", "kite kits", "face painting".) <img src="img/req.jpg" alt="required"></label>
<input type="text" name="otherdetails" id="otherdetails" value="" />
<fieldset><legend>3. Select any/all that apply. Additional questions may appear, if further information is required.</legend>
<span id="additional" class="InputGroup">
<label><input name="raffle" id="raffle_1" type="checkbox" class="switchcheck1" value="1" />I'll be selling raffle tickets and/or holding a raffle at the festival.</label>
<div class="newcol offstate1">
<label for="raffledetails">You'll need written permission from the Exchange Club. Please enter details about the raffle. <img src="img/req.jpg" alt="required"></label>
<textarea name="raffledetails" id="raffledetails" tabindex="<?php echo $tabin; $tabin++; ?>"></textarea>
</div>
<label><input name="trailer" type="checkbox" id="trailer_1" value="1">I'll be bringing a trailer.</label>
<label><input name="outlets" type="checkbox" id="outlets_1" class="switchcheck2" value="1" />I'll require electrical outlets.</label>
<div class="newcol offstate2">
<label for="outletsdetails">How many outlets will you require? <img src="img/req.jpg" alt="required"></label>
<input type="text" name="outletsdetails" id="outletsdetails" />
</div>
</span>
</fieldset>
<input type="button" name="submit" class="a_button" value="submit" />
</form>
The element with name="submit" is of type button and not submit, so it renders a plain button that does nothing (with the intention that you bind some JavaScript to it).
Use type="submit" instead.
Try
<input type="submit" name="submit" class="a_button" value="submit" />
for your submit button.

Categories