get value of radio button using jQuery and php - php

I want to get the value clicked in radio button.
This is my code:
<ul class="collapsible popout" data-collapsible="accordion" id="clicks">
<?php
foreach ($preguntas['preguntas'] as $row)
{
$opciones = $pregunta->opciones($row[0]);
?>
<li>
<div class="collapsible-header"><i class="material-icons">question_answer</i><?php echo utf8_encode($row[2]); ?></div>
<div class="collapsible-body">
<?php foreach ($opciones as $opcion){ ?>
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
<?php } ?>
</div>
<?php } ?>
</li>
</ul>
I need to get the value of this input id="opcion_..."
<p class="left-align" id="options">
<input class="with-gap" name="pregunta_<?php echo utf8_encode($row[0]); ?>" type="radio" id="opcion_<?php echo utf8_encode($opcion[0]); ?><?php echo $row[0] ?>" value="<?php echo $opcion[0]; ?>" />
<label for="opcion_<?php echo $opcion[0]; ?><?php echo utf8_encode($row[0]); ?>"><?php echo utf8_encode($opcion[2]); ?></label>
</p>
The problem is name and id is changing, it's not the same
Any idea?
Thank you.

JQuery selector for part of attribute $("[attribute^='value']") like this:
$("[id^='opcion_']").click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input name="pregunta_xxx" type="radio" id="opcion_123" value="123">radio for 123</label>
<label><input name="pregunta_xxx" type="radio" id="opcion_456" value="456">radio for 456</label>

Related

Selection in Radio Button gets replaced in two divs

I have two questions with radio button (each question having 4 radio) in PHP
But there is single check in all 8 radio buttons, but it should be, with 4-4 radio buttons.
Look: https://imgur.com/Sytl72a
My Code:
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<?php
$lstmt = $user->runQuery("SELECT * FROM mcq WHERE LRN=:lrn ORDER BY Sr ASC ");
$lstmt->bindparam(":lrn",$id);
$lstmt->execute();
if($lstmt->rowCount() > 0)
{
$i=0;
while($lrow=$lstmt->fetch(PDO::FETCH_ASSOC))
{
extract($lrow);
$i++;
?>
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
<hr></br></br>
<?php
}
}
?>
<button class="btn btn-large btn-primary" type="submit" name="btn-submit">Submit</button>
</form>
<?php
}
if(isset($_POST['btn-submit']))
{
echo $ufname = trim($_POST['radio']);
}
?>
Update your radio input name attribute just like this,
<div>
<h1><?php echo $i; ?>) <?php echo $Question; ?></h1></br>
<h2> <input type="radio" name="radio<?php echo $i; ?>?>" value="<?php echo $Oa; ?>"> A) <?php echo $Oa; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Ob; ?>"> B) <?php echo $Ob; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Oc; ?>"> C) <?php echo $Oc; ?></h2>
<h2> <input type="radio" name="radio<?php echo $i; ?>" value="<?php echo $Od; ?>"> D) <?php echo $Od; ?></h2>
</div>
And for submitting:
<?php
}
$count = $i;
for($j = 1; $j <= $count; $j++){ if(isset($_POST['btn-submit'])) {
echo $ufname = trim($_POST['radio'.$j]);
}
}
?>
It will definitely solve your problem,

PHP form with radio buttons - make only one answer required, second optional

I have this PHP form with radio buttons:
{
$agreements = jm_get_application_setting( 'application_agreements', '' );
if( empty( $agreements ) ) return;
$questions = explode( "\n", $agreements );
if (!empty($questions)):
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="1" required><i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio" name="_noo_application_answer_<?php echo $index; ?>" value="0" required><i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
endforeach;
endif;
}
How can I make only one answer required to validate the form?
I mean.. Just the first MUST be selected, but I also need to show the second one for "legal reasons".
As is, this form accepts both choices as acceptable.
You can set a variable for the condition, and change it's value at the end of first iteration.
if (!empty($questions)):
$first = 1; // <----- HERE
foreach ($questions as $index => $question) :
?>
<div class="form-group">
<p><strong for="question-<?php echo sanitize_title($question); ?>" ><?php echo $question; ?></strong></p>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="1"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Do il consenso', 'noo'); ?>
</label>
</div>
<div class="form-control-flat">
<label class="radio">
<input type="radio"
name="_noo_application_answer_<?php echo $index; ?>"
value="0"
<?php echo $first ? "required" : ""; ?>> // <----- HERE
<i></i><?php echo esc_html__('Nego il consenso', 'noo'); ?>
</label>
</div>
<input type="hidden" name="_noo_questions[]" value="<?php echo esc_attr($question); ?>"/>
</div>
<?php
$first = 0; // <----- HERE
endforeach;
endif;

Getting values of radio buttons generated in for loop

I'm currently running on codeigniter framework and running into some trouble. I am retrieving a list of questions and a list of their possible answers (something like a questionnaire) from the database and loading it out to participants to attempt.
Due to the the for loop I'm using, I'm unable to change the name or id value of individual questions. I sort of overcome this by appending the questionID to the name/id.
The trouble comes here when i post this over to my controller. How am I able to differentiate it when I need to insert it the results back into the database?
<?php foreach($dbresult->result() as $row) {?>
<p><?php echo $row->Question ;?></p>
<?php if ($row->QuestionType == "MCQ"){ ?>
<ul>
<li><b>A: </b><?php echo $row->SelectionA; ?></li>
<li><b>B: </b><?php echo $row->SelectionB; ?></li>
<li><b>C: </b><?php echo $row->SelectionC; ?></li>
<li><b>D: </b><?php echo $row->SelectionD; ?></li>
</ul>
Select one of the following options:<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" required value="A">A<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="B">B<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="C">C<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="D">D<br>
<?php } elseif($row->QuestionType== "truefalse") {?>
Select one of the following options:<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="true">True<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="false">False<br>
<?php } else {?>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="ok">OK<br>
<input type="radio" name="<?php echo $row->QuestionID;?>.Ans" value="agree">Agree<br>
<?php }?>
<?php } ?>
The above is the way I have printed the questionnaire out, any possible solutions for this?
Thats hard to manage having a name attribute like that.
Use a grouping name attribute:
name="answers[<?php echo $row->QuestionID;?>]"
Simple example:
<?php foreach($dbresult->result() as $row) {?>
<p><?php echo $row->Question ;?></p>
<?php if ($row->QuestionType == "MCQ"){ ?>
<ul>
<li><b>A: </b><?php echo $row->SelectionA; ?></li>
<li><b>B: </b><?php echo $row->SelectionB; ?></li>
<li><b>C: </b><?php echo $row->SelectionC; ?></li>
<li><b>D: </b><?php echo $row->SelectionD; ?></li>
</ul>
Select one of the following options:<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" required value="A">A<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="B">B<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="C">C<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="D">D<br>
<?php } elseif($row->QuestionType== "truefalse") {?>
Select one of the following options:<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="true">True<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="false">False<br>
<?php } else {?>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="ok">OK<br>
<input type="radio" name="answers[<?php echo $row->QuestionID;?>]" value="agree">Agree<br>
<?php }?>
<?php } ?>
So that in PHP, you could just call it by its name:
public function controller_name()
{
$answers = $this->input->post('answers');
foreach($answers as $question_id => $answer) {
}
}

Selected radio button make field true or false

I have the following code:
<?php $radioSwitch = of_get_option('radio_switch');
if ($radioSwitch == "true") { ?>
<!-- radio switch -->
<div class="row switches clearfix">
<input type="radio" name="switch" id="switch1" value="<?php echo of_get_option('switch1'); ?>" /><label for="switch1" class="field"><?php echo of_get_option('switch1'); ?></label>
<input type="radio" name="switch" id="switch2" value="<?php echo of_get_option('switch2'); ?>" /><label for="switch2" class="field"><?php echo of_get_option('switch2'); ?></label>
</div>
<?php } ?>
................................................................................
<?php
$datePicker1 = of_get_option('datepicker1');
$datePicker2 = of_get_option('datepicker2');
if (($datePicker1 == "true") || ($datePicker2 == "true")) { ?>
<div class="row clearfix">
<?php if ($datePicker1 == "true") { ?>
<!-- datepicker 1 -->
<input type="text" id="dateFrom" name="<?php echo of_get_option('datepicker1_text'); ?>" value="<?php echo of_get_option('datepicker1_text'); ?>" tabindex="6" />
<label class="error" for="dateFrom" id="dateFrom_error">Selectați data.</label>
<?php } ?>
<?php if ($datePicker2 == "true") { ?>
<!-- datepicker 2 -->
<input type="textarea" id="dateTo" name="<?php echo of_get_option('datepicker2_text'); ?>" value="<?php echo of_get_option('datepicker2_text'); ?>" tabindex="7" />
<label class="error" for="dateTo" id="dateTo_error">Selectați data.</label>
<?php } ?>
</div>
<?php } ?>
If radio button "switch1" is selected, only "$datepicker1" must be true and "$datepicker2" must be false, and if radio button "switch2" is selected, both "$datepicker1" and "$datepicker2" must be true. Anyone could help me?
PHP is a server-side language which is parsed on the server. You cannot modify variables of PHP on the browser.
Try submitting the radio button options as a form into a PHP page.

Show Custom Customer Fields in Checkout

I've succesfully managed to add custom fields to the customer. However I need these fields to show up in onepage checkout.
I've overridden Mage_Customer_Block_Widget_Name and created my own customer/widget/name.phtml, added the attributes in the sql/xxx_setup/installer-x.y.z.php (added them to adminhtml_customer, customer_account_edit, checkout_register and customer_account_create) and they work fine in the admin site, however they just wont work on the checkout form. The field shows up, but it has the wrong value and no label.
I'm clueless why does it work in the customer registration form but doesn't in the checkout.
The installer code to add the attribute is:
$attributes = array(
'lastname2' => array(
'frontend_label'=>'Apellido Materno',
'label' => 'Apellido Materno',
'input' => 'text',
'type' => 'varchar',
//System = False and visible true = Show in 'customer_account_create', 'customer_account_edit', 'checkout_register'
'system'=>true,
'visible'=>true, //Watch out!! Only visible fields get processed by the form controllers!!!
'user_defined'=>false,
'used_in_forms' => array('adminhtml_customer', 'customer_account_edit', 'checkout_register','customer_account_create'),
'required' => 0,
'position' =>69
));
foreach($attributes as $attribute_code=>$definition)
{
$installer->addAttribute('customer', $attribute_code, $definition);
/**
* #var Mage_Eav_Model_Config
*/
Mage::getSingleton('eav/config')
->getAttribute('customer', $attribute_code)
->setData('used_in_forms',$definition['used_in_forms'])
->save();
}
The code in name.phtml is
<div class="<?php echo $this->getContainerClassName()?>">
<?php if ($this->showPrefix()): ?>
<div class="field name-prefix">
<label for="<?php echo $this->getFieldId('prefix')?>"<?php if ($this->isPrefixRequired()) echo ' class="required"' ?>><?php if ($this->isPrefixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('prefix') ?></label>
<div class="input-box">
<?php if ($this->getPrefixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getPrefix()) ?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getPrefixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getPrefix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<div class="field name-firstname">
<label for="<?php echo $this->getFieldId('firstname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('firstname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo $this->getStoreLabel('firstname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showMiddlename()): ?>
<?php $isMiddlenameRequired = $this->isMiddlenameRequired(); ?>
<div class="field name-middlename">
<label for="<?php echo $this->getFieldId('middlename')?>"<?php echo $isMiddlenameRequired ? ' class="required"' : '' ?>><?php echo $isMiddlenameRequired ? '<em>*</em>' : '' ?><?php echo $this->getStoreLabel('middlename') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('middlename')?>" name="<?php echo $this->getFieldName('middlename')?>" value="<?php echo $this->escapeHtml($this->getObject()->getMiddlename()) ?>" title="<?php echo $this->getStoreLabel('middlename') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('middlename') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php endif; ?>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('lastname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname')?>" name="<?php echo $this->getFieldName('lastname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname()) ?>" title="<?php echo $this->getStoreLabel('lastname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname2')?>"><?php echo $this->getStoreLabel('lastname2') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname2')?>" name="<?php echo $this->getFieldName('lastname2')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname2()) ?>" title="<?php echo $this->getStoreLabel('lastname2') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname2') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showSuffix()): ?>
<div class="field name-suffix">
<label for="<?php echo $this->getFieldId('suffix')?>"<?php if ($this->isSuffixRequired()) echo ' class="required"' ?>><?php if ($this->isSuffixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('suffix') ?></label>
<div class="input-box">
<?php if ($this->getSuffixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getSuffix()) ?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getSuffixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getSuffix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
This fields in the checkout you are talking about are address attributes, not customer attributes. So you need to load them differently. For registered users you could use Mage::getSingleton('customer/session')->getCustomer()->getLastname2() but it won't be saved to your address, as there is no attribute yet.
Depending on where you want lastname2 to be accessible you can create corresponding attributes for the entities customer_address,quote_address and order_address. They are created the same way you did for customer with
$installer->addAttribute($entityName, $attribute_code, $definition);
But that's not all. For the attributes to be converted correctly you need to set up conversion rules in your module config.xml. See for example the configuration of Mage_Sales.
In the global node there is a node fieldsets with the corresponding rules. There is a node customer_address to convert address attributes to quote address attributes. In sales_convert_quote there are rules to convert this attributes to order attributes.
So for your attributes to be accessible in all you config should look like this:
<global>
<fieldsets>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
<sales_copy_order_billing_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
<sales_copy_order_billing_address>
<sales_copy_order_shipping_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
</sales_copy_order_shipping_address>
<sales_convert_quote_address>
<lastname2>
<to_order_address>*</to_order_address>
<to_customer_address>*</to_customer_address>
</lastname2>
</sales_convert_quote_address>
<sales_convert_order_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastnam2e>
<sales_convert_order_address>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
</fieldsets>
</global>

Categories