I am trying to place a series of links on one page that, when clicked, will select a default value of a radio group on another page. The perfect example of this is here. By clicking one of the blue links on the far right, they define the default value of the radio group on the next page. That is exactly what I am trying to do.
Can anyone help me understand how they did that?
This isn't 'checking' the box on the other end. The link that you're clicking contains a variable in the URL (GET variable). When the new page loads, it's likely using a server-side scripting language to test for each radio button.
In this case, ?default_amt=5 is what is attached for the $5 contribution. Then, on the page:
<input type="radio" checked="checked" value="5" name="amount" id="amt_preset_1" onclick="BSD.contribution.clearother();" class="radiobutton">
Is the value for the radio button. Simply put something like this php code to 'check' it
<?php if ($_GET['default_amt'] == "5") {echo "checked=\"checked\"; } ?>
The links on the previous page could be this:
Set amount to 1 on new page
Set amount to 2 on new page
Set amount to 3 on new page
Set amount to 4 on new page
'newpage.html' would have the following:
<input type="radio" value="1" name="toPay" <?php if ($_GET['amount'] == "1") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="2" name="toPay" <?php if ($_GET['amount'] == "2") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="3" name="toPay" <?php if ($_GET['amount'] == "3") {echo "checked=\"checked\"; } ?>>
<input type="radio" value="4" name="toPay" <?php if ($_GET['amount'] == "4") {echo "checked=\"checked\"; } ?>>
Related
I have this problem: In a form, I want to display the info from a box (idCreditorCnpCui) only if the second or the third option is selected from the form (if "P" or "O" option is selected). While I select any of these options, the box appears as it should. The problem I encountered is that when I press the "sent" button (with O or P option), the idCreditorCnpCui box disappears from the view.
This is the HTML with the options of the form:
<div class="form-field">
<label for="payment.creditorOrganizationOrPerson"><localization:localize key="creditorOrganizationOrPerson.label"/></label>
<div class="form-field-element">
<input type="radio" property="payment.creditorOrganizationOrPerson" value="N" onclick="display();" /> N <br>
</div>
<div class="form-field-element">
<input type="radio" property="payment.creditorOrganizationOrPerson" value="P" onclick="display();" /> P <br>
</div>
<div class="form-field-element">
<input type="radio" property="payment.creditorOrganizationOrPerson" value="O" onclick="display();" /> O
</div>
</div>
This is the box I want to be display or no according the option selected:
<div class="form-field" id="idCreditorCnpCui" style="display:none">
<label for="payment.creditorCnpCui"><localization:localize key="creditorCnpCui.label"/></label>
<div class="form-field-element">
<input type="text" property="payment.creditorCnpCui" size="36" maxlength="35" styleClass="input-field" styleId="payment.creditorCnpCui"/>
</div>
And this is the js
function display(toDisplay,idDiv){
if(toDisplay == false){
document.getElementById(idDiv).style.display = "none";
}else{
document.getElementById(idDiv).style.display = "block";
}
}
display(false,"idCreditorCnpCui");
If I turn the final line to "display(true, "idCreditorCnpCui"), the box appear even when the option "N" is selected by default. That should not happen since the N option means the idCreditorCnpCui box is hidden. Any idea please.
I want a field to be display just if an option is selected. But when I send the form, the field disappears even when the option that display that field is selected
I'm new here and I'm writing my first php code. I want to prepare a multiple choice quiz in which, after click on a button at the end of the page, the radios button (the choices of the test) will remain checked and unchangeable. I simplify the code in this way:
<?php
$choice1 = "a";
$choice2 = "b";
?>
<form method="post" name="testForm" id="testForm">
<ul>
<li><input type="radio" name="q1" value="a"
<?php if(isset($_POST['test-result']) && $radioVal==$choice1){ echo "disabled='disabled' checked";}?>
<?php if(isset($_POST['test-result']) && $radioVal<>$choice1){ echo "disabled='disabled'";}?>>
<?php echo "<span id='question'>". $choice1 ."</span>";?> </li>
<li><input type="radio" name="q1" value="b"
<?php if(isset($_POST['test-result']) && $radioVal==$choice2){ echo "disabled='disabled' checked";}?>
<?php if(isset($_POST['test-result']) && $radioVal<>$choice2){ echo "disabled='disabled'";}?>>
<?php echo "<span id='question'>". $choice2 ."</span>";?> </li>
</ul><br>
<button class='button' name='test-result'>Finaliza el test</button>
</form>
<?php
if (isset($_POST['test-result']))
{
$radioVal = $_POST["q1"];
}
?>
However, after click on the button, all radios remain uncheged (and this is ok) but the choices made are not checked. Can someone help me? Thanks in advance!
Checkbox is not showing checked because variable $radioVal is set after input box. To resolve your issue could you please set $radioVal before from.
In Prestashop I am doing a module. In that module I have view file(smarty template). In smarty I am doing form so that user can submit the form from frontend. Now when user makes fill the form and clicks on submit then the form should save all the vales to thae database. So for that in smarty I made a form like this
<div id="content" class="form-wrapper" >
<div class="form-content">
<input type="text" id="name" name="name" placeholder="Name" /> <br />
<input type="text" id="email" name="email" placeholder="email"/> <br />
<input type="text" id="phone" name="phone" placeholder="Phone Number"/> <br />
<input type="submit" name="submit-query" id="submit-enquiry" value="submit" />
</div>
</div>
and in the file where the values will be submitted I have made my code like this
<?php
include '../../../config/settings.inc.php';
include '../../../config/defines.inc.php';
include '../../../config/config.inc.php';
include(dirname(__FILE__).'/../../../../init.php');
if(Tools::getValue('submit-query')) {
$this->_html .=$this->displayConfirmation($this->l('Settings updated successfully'));
}
else {
$this->_html .= $this->displayError($this->l('You Have Some Errors'));
}
?>
Now when doing click on my submit button It is showing error like this
Fatal error: Using $this when not in object context in file.php on line 11
Now can someone kindly tell me what is the issue here and how can I make a form in smarty for frontend users so that they can submit the form and the value will be stored to the database. Any help and suggestions will be really appreciable. Thanks
First I would recommend to do the processing inside of your module, e.g. in init() function, you can get your module link using $this->context->link->getModuleLink('myModuleName'); and add it to the form action attribute, all variables can be assigned using $this->context->smarty->assign().
Storing values in database is quite a wide question. You have to provide some more details about it.
Check some tutorials from Prestashop e.g. Creating a Prestashop Module. You will find most of your answers there.
In my form, I have x groups of radio buttons generated:
for ( $j = 0; $j < $x; $j++ ) {
<input type="radio" name="gender<?php echo $x; ?>" value="male" />Male
<input type="radio" name="gender<?php echo $x; ?>" value="female" />Female
...
}
When, after submit, I try to retrieve these values:
for ( $j = 0; $j < $x; $j++ ) {
echo $_POST['gender'.$j];
...
}
I get these error messages:
Notice: Undefined index: gender0 in ...
Notice: Undefined index: gender1 in ...
Notice: Undefined index: gender2 in ...
Yet I see these radio groups in my markup correctly defined:
<input type="radio" value="male" name="gender0">Male
<input type="radio" value="female" name="gender0">Female
...
<input type="radio" value="male" name="gender1">Male
<input type="radio" value="female" name="gender1">Female
So why these error messages?
Just make a var_dump($_POST); on the beginning of your receiving script, then you’ll see exactly what you get.
And you are aware that you get values only for radio buttons that are actually checked, right?
So if you can not be certain if the user will have a radio button checked per group, then check whether that POST entry is available before accessing it, using isset or empty.
Btw., an even more convenient way to deal with data like this, is to name the form fields in a way that gets you an array in PHP in the first place:
<input type="radio" name="gender[0]" value="male" />Male
<input type="radio" name="gender[0]" value="female" />Female
<input type="radio" name="gender[1]" value="male" />Male
<input type="radio" name="gender[1]" value="female" />Female
– that will get you an array in $_POST['gender'], that you can loop through with a foreach loop like this:
if(isset($_POST['gender']) && is_array($_POST['gender'])) {
foreach($_POST['gender'] as $index => $value) {
// …
}
}
I have two radio buttons and when user clicks on one radio button and clicks button it should stay selected. But what is happening is only last one remains selected even if I select and search with first radio button.
My code:
<input type="radio" <?php if(isset($_POST['gender']) && isset($_POST['gender']) == 'male') echo "checked='checked'"; ?> name="gender" id="male" value="male" onclick="getGender(this.value);" /><strong>Groom</strong>
<input type="radio" <?php if(isset($_POST['gender']) && isset($_POST['gender']) == 'female') echo "checked='checked'"; ?> name="gender" id="female" value="female" onclick="getGender(this.value);" /><strong>Bride</strong>
Thanks in advance.
Just change
&& isset($_POST['gender']) == 'male')
to
&& ($_POST['gender'] == 'male')
and do the same for female