Hide to Show some information jQuery using Radio Button - php

I want to show different information based on radio buttons using jQuery.
When I select any payment processor then I want to show related payment method information on the right side.
So, when I select any button then show the information related to that payment processor, and at the same time hide information related to the other payment processors.
This is my HTML Form:
<form class="form-horizontal" action="<?php base_url(); ?>confirm_order" method="post">
<fieldset>
<div class="span4">
<!-- Left Side -->
<input type="radio" checked="checked" name="paypal">Paypal<br/>
<input type="radio" name="cart">Visa, Master Card, etc<br/>
<input type="radio" name="cash_on_delevary">Cash on Delivery
<!-- End Left Side -->
</div>
<div class="span4">
<!-- Right Side -->
<div class="control-group" id="paypal">
<label class="control-label" >Paypal Payment</label>
</div>
<div class="control-group" id="Cart">
<label class="control-label" >Cart</label>
</div>
<div class="control-group" id="cash_on_delevary">
<label class="control-label" >Cash on Delevary</label>
</div>
<!-- end Right Side -->
</div>
<input type="submit" name="Continue" class="btn btn-orange pull-right">
</fieldset>
</form>
What can I do?

This will work:
$("input[type='radio']").change(function() {
$(".control-group").hide();
$("#" + this.name).show();
});

If you want radiobutton to work, you need to assign them the same name, else when you select 1, the others wont deselect.
What you should do is to assign a data on element to know wich section they show :
<input type="radio" data-for='paypal' checked="checked" name="payment">Paypal<br/>
<input type="radio" data-for='Cart' name="payment">Visa, Master etc<br/>
<input type="radio" data-for='cash_on_delevary' name="payment">Cash on Delevary
And then You bind the change event :
$("input[name=payment]").change(function() {
$(".control-group").hide();
$("#" + $(this).data("for")).show();
});
But to hide every section at start, you need to add this :
$(".control-group").hide();
$("input[name=payment]:checked").trigger('change')
Here's a fiddle : http://jsfiddle.net/a9Fvn/

Related

hide/show php checkbox by id like www.anysite.php?id=10

I have a form and a checkbox. By click on the checkbox, a div within some other divs will show up or hide. This works! But, if i visit my page with an id like xxxxx.php?id=10, the checkbox is checked but the divs are hide. if i click on the checkbox, the divs will show up, but the checkbox is unchecked (bad for e.g. mysql updates).. hope anyone can help me by this issue..
Here my code:
$(document).ready(function() {
$('#Show').hide();
$("input[name=mycheckbox]").click(function () {
$('#Show').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
<div class="form-group">
<div class="form-group">
<div class="checkbox-inline">
<label>
<input type="checkbox" name="mycheckbox" id="mycheckbox" value="yes"> my option
</label>
</div>
</div>
</div>
<div id="Show">
<div class="form-group">
<label for="textinput" class="control-label">Company </label>
<div class="">
<input type="text" class="form-control" name="name" id="name" placeholder="" value="">
</div>
</div>
</div>
</form>
My input field have this code (php doesn't work in the snippet):
<input type="checkbox" name="mycheckbox" id="mycheckbox" value="yes" <?php if (isset($_GET['id'])) {echo $row->mycheckbox == "yes" ? 'checked="checked"' : "";}?>
thanks for your help!
I would recommend to use change event, trigger using .trigger('change') on page load so that the div in sync with the state of checkbox element.
Also you should .toggle(display)
$("input[name=mycheckbox]").change(function () {
$('#Show').toggle(this.checked);
}).trigger('change');

Display Content Based on Radio Button Selection (but only once form is submitted)

First, I'm a complete novice to php...but I think it's the best method to accomplish my goal. I've researched many related topics here, but couldn't find quite what I was looking for.
This is what I'm trying to accomplish. I’d like to have unique content display directly on the page based on which radio button a user chooses in the form (see the form code below), but only after the form is submitted.
<div id="mc_embed_signup_bond_analysis">
<form action="//example.us8.list-manage.com/subscribe/post?u=12345678&id=12345" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<div id="mc_embed_signup_scroll">
<div class="mc-field-group input-group">
<strong>Why Do You Need a Bond?</strong>
<ul>
<li><input type="radio" value="For your license or permit." name="MMERGE2" id="mce-MMERGE2-0"><label for="mce-MMERGE2-0"> For your license or permit.</label></li>
<li><input type="radio" value="For a specific construction job." name="MMERGE2" id="mce-MMERGE2-1"><label for="mce-MMERGE2-1"> For a specific construction job.</label></li>
<li><input type="radio" value="A court is requiring one." name="MMERGE2" id="mce-MMERGE2-2"><label for="mce-MMERGE2-2"> A court is requiring one.</label></li>
<li><input type="radio" value="To protect your business or clients." name="MMERGE2" id="mce-MMERGE2-3"><label for="mce-MMERGE2-3"> To protect your business or clients.</label></li>
<li><input type="radio" value="To protect your business or clients." name="MMERGE2" id="mce-MMERGE2-4"><label for="mce-MMERGE2-4"> None of these options apply to me.</label></li>
</ul>
</div>
<div class="mc-field-group">
<label for="mce-EMAIL"><strong>Email Address:</strong> <span class="asterisk">*</span></label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div class="mc-field-group">
<label for="mce-FNAME"><strong>First Name:</strong> <span class="asterisk">*</span></label>
<input type="text" value="" name="FNAME" class="required" id="mce-FNAME">
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;"><input type="text" name="b_123456789_123456" tabindex="-1" value=""></div>
<div class="clear text-center"><input type="submit" value="Get Free Bond Analysis!" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</div>
</form>
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>
I've seen a few jQuery solutions here, but they didn't seem to accomplish both displaying unique content based on the radio button selection AND only displaying it once the form is submitted. Any help or a nudge in the right direction is greatly appreciated, as I'm completely lost.
Thanks in advance!
Here is a loose (and I stress loose) example of how such an Ajax/Mailchimp implementation without page refresh and with custom info box below the form based on the selected radio at time of submit.
First you will need to adjust the action url that the form is posting to in order to send the form JSON encoded:
<div id="form-wrap">
<div id="mc_embed_signup_bond_analysis">
<form action="//example.us8.list-manage.com/subscribe/post-json?u=12345678&id=12345" method="get" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate main-form" target="_blank" novalidate>
<div id="mc_embed_signup_scroll">
<div class="mc-field-group input-group">
<strong>Why Do You Need a Bond?</strong>
<ul>
<li><input type="radio" value="For your license or permit." name="MMERGE2" id="mce-MMERGE2-0"><label for="mce-MMERGE2-0"> For your license or permit.</label></li>
<li><input type="radio" value="For a specific construction job." name="MMERGE2" id="mce-MMERGE2-1"><label for="mce-MMERGE2-1"> For a specific construction job.</label></li>
<li><input type="radio" value="A court is requiring one." name="MMERGE2" id="mce-MMERGE2-2"><label for="mce-MMERGE2-2"> A court is requiring one.</label></li>
<li><input type="radio" value="To protect your business or clients." name="MMERGE2" id="mce-MMERGE2-3"><label for="mce-MMERGE2-3"> To protect your business or clients.</label></li>
<li><input type="radio" value="To protect your business or clients." name="MMERGE2" id="mce-MMERGE2-4"><label for="mce-MMERGE2-4"> None of these options apply to me.</label></li>
</ul>
</div>
<div class="mc-field-group">
<label for="mce-EMAIL"><strong>Email Address:</strong> <span class="asterisk">*</span></label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div class="mc-field-group">
<label for="mce-FNAME"><strong>First Name:</strong> <span class="asterisk">*</span></label>
<input type="text" value="" name="FNAME" class="required" id="mce-FNAME">
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;"><input type="text" name="b_123456789_123456" tabindex="-1" value=""></div>
<div class="clear text-center"><input type="submit" value="Get Free Bond Analysis!" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</div>
</form>
</div>
<div id="info-section"></div>
</div>
You will then need to set some variable that will store the value and id of the radio button, listen for the submit and then pass those values to a switch function (to get the custom text you want based on the selection) and the AJAX post function (that will post your data).
//If you have more than one form you can set $form to the id of the target form
var $form = $('.main-form');
var $radiotext;
var $radioVal;
var $radioId;
$('form input[type="submit"]').on('click', function (e) {
e.preventDefault();
//set $radioVal as value of checked radio value
$radioVal = $('input[name="MMERGE2"]:checked', '#mc-embedded-subscribe-form').val();
//set $radioID as value of checked radio ID
$radioID = $('input[name="MMERGE2"]:checked', '#mc-embedded-subscribe-form').attr('id');
//pass the $radioID to the switch statement and set value of $radioText to returned result.
$radioText = switchInfo($radioID);
//pass the form to the register function
register($form);
});
We then need to create those switch and Ajax functions:
//As noted above, the id is passed as param v. This is passed as the base comparison value for the switch statement. You then have the ID's of each radio as the case to compare to the switch parameter (the ID passed above)
function switchInfo(v){
switch (v) {
case 'mce-MMERGE2-0':
return 'Whatever your permit text is'
break;
case 'mce-MMERGE2-1':
return 'Whatever your construction text is'
break;
case 'mce-MMERGE2-2':
return 'Whatever your court order text is'
break;
case 'mce-MMERGE2-3':
return 'Whatever your protect clients text is'
break;
case 'mce-MMERGE2-4':
return 'None of the options provided applied to you'
break;
default:
return 'Not Specified'
}
}
//
function register($form) {
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
cache : false,
dataType : 'json',
contentType: "application/json; charset=utf-8",
error : function(err) { alert("Could not connect to the registration server. Please try again later."); },
success : function(data) {
if (data.result != "success") {
alert('An Error occured and your form could not be processed');
}else {
//Insert a heading of the radio value and paragraph from the switch value to the #info-section
$('#info-section').html('<h1>'+$radioVal+'<h1><p>'+$radioText+'<p>');
}
}
});
}
As I said, this is a completely untried and untested method. I don't recall ever sending a post to Mailchimp via ajax. This is just a base on how you would achieve what you would like to achieve. There are a ton of resources online regarding AJAX and there really isn't that steep of a learning curve. A couple of tuts and what you are trying to achieve would make sense to you.

Jquery getting the value of a parent element input when checking a child checkbox

I have this set of forms, when the user checks the input, I need to get the value that is outside this div, basically a parent div, try it to do it using .parent() and then find but could not make it work, not sure what do I am doing wrong, I have a js http://jsfiddle.net/creativestudio/bCgeD/2/
This is my js:
function set_index_id_checkbox() {
$("input:checkbox[name='like']").each(function(index){
var currElem = $(this);
var prevLabel = currElem.next();
currElem.attr("id", this.id + index);
prevLabel.attr("for", prevLabel.attr("for") + index);
});
}
set_index_id_checkbox();
function getValues(){
$("input:checkbox[name='like']").click(function(){
if (this.checked) {
$(this).closest('.reply-form').find('.feedback_nid').text(this.value);
} else {
// ...
}
});
} getValues();
and this is my html:
<input type="hidden" class="feedback_nid" value="form a 111">
<input type="hidden" class="branch_nid" value="form a 222">
<div class="block">
<!-- value -->
<span class="info-item float-left value">
<span class="likes">value</span>
</span><!-- /.value -->
<!-- checkbox -->
<div class="btn like-checkbox float-right">
<input id="like" class="like" type="checkbox" name="like" value="33">
<label class="like" for="like">like 1</label>
</div><!-- /checkbox -->
</div>
</div>
<div class="reply-form">
<input type="hidden" class="feedback_nid" value="form a 111">
<input type="hidden" class="branch_nid" value="form a 222">
<div class="block">
<!-- value -->
<span class="info-item float-left value">
<span class="likes">value</span>
</span><!-- /.value -->
<!-- checkbox -->
<div class="btn like-checkbox float-right">
<input id="like" class="like" type="checkbox" name="like" value="45">
<label class="like" for="like">like 2</label>
</div><!-- /checkbox -->
</div>
</div>
<div class="reply-form">
<input type="hidden" class="feedback_nid" value="form a 111">
<input type="hidden" class="branch_nid" value="form a 222">
<div class="block">
<!-- value -->
<span class="info-item float-left value">
<span class="likes">value</span>
</span><!-- /.value -->
<!-- checkbox -->
<div class="btn like-checkbox float-right">
<input id="like" class="like" type="checkbox" name="like" value="66">
<label class="like" for="like">like 3</label>
</div><!-- /checkbox -->
</div>
</div>​
In the code you are trying to set the html of hidden input field..
.find('.feedback_nid').html(this.value);
Input element's have no inner HTML..
Maybe you are looking for .val(this.value)
Also you can access the checkboxes using the class like instead of attribute selector..
I am instead populating the span with class="likes" and seems to be working fine..
Check Fiddle
Try parents, parent will only search one div higher, but parents will search the file until it find the div if exist.
See the jquery documention:
Jquery parent
The span with text Value in it is a different class likes not the class feedback_nid you have set for a hidden input.
If you want the hidden input to have the same value use val() for the input and set the span using text()
http://jsfiddle.net/gunderson/bCgeD/4/
I updated your example to work in jsfiddle.
The main change I made was traversing up the tree using .parent() until I got to the div that containes the value you're looking for. Additionally I updated your syntax to pull the value out of the hidden field using jquery.val();
function getValues(){
$("input:checkbox[name='like']").click(function(){
if (this.checked) {
var value = $(this).parent().parent().parent()
.find('.feedback_nid').val();
$(this).parent().parent().find(".likes").text(value);
} else {
// ...
}
});
} getValues();

Active radio button with jquery

I used all of the helpful answers on this site to try to build a jquery script to select radio buttons, but for some reason they don't work. Am I doing something wrong? For the sake of simplicity I've started back from square one.
I've attached the format that the php-generated form produces below. I tried multiple browsers, but was unable to get any button to select anything. Is there some complexity I'm not taking into account? The page is a .php page, and the form is generated in php.
Sorry for the basic question -- any help would be greatly appreciated. Not sure if it's relevant, but I want these buttons to work on mobile as well as various browsers.
Form Code:
<form class="form-horizontal" method="get" action="submit.php" name="form">
<div class="control-group">
<div class="row-fluid">
<div class="span2"></div>
<div class="span8">
<label class = "control-label" for="input1"> foreign policy </label>
<div class="controls">
<label class="checkbox inline"><input type="radio" name="group1" id="1_l" value="1_l"> Liberal</label>
<label class="checkbox inline"><input type="radio" name="group1" id="1_m" value="1_m"> Moderate</label>
<label class="checkbox inline"><input type="radio" name="group1" id="1_c" value="1_c"> Conservative</label>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span2"></div>
<div class="span8">
<br>
<center><button type="submit" class="btn btn-danger btn-large">Generate</button></center>
</div>
<div class="span2"></div>
</div>
</form>
Jquery Code:
$('button').click(
function() {
$('#1_l').attr('checked', true);
}
);
Make sure to only bind the handler after the DOM has been loaded:
$(function() {
$('button').click(function() {
$('#1_l').attr('checked', true);
});
});
Use the ":checked" selector in jQuery to select the checked input, otherwise you only get the first one.
$(function () {
$('button.btn').click(function () {
var $box = $('.checkbox input:checked');
if ($box.length > 0)
alert($box.val());
});
});​
Here is what i see you have a div that was not closed i closed it
I changes the button type to button rather than submit so i could see it work and all was good
See my working DEMO
<form class="form-horizontal" method="get" action="submit.php" name="form">
<div class="control-group">
<div class="row-fluid">
<div class="span2"></div>
<div class="span8">
<label class = "control-label" for="input1"> foreign policy </label>
<div class="controls">
<label class="checkbox inline"><input type="radio" name="group1" id="1_l" value="1_l"> Liberal</label>
<label class="checkbox inline"><input type="radio" name="group1" id="1_m" value="1_m"> Moderate</label>
<label class="checkbox inline"><input type="radio" name="group1" id="1_c" value="1_c"> Conservative</label>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span2"></div>
<div class="span8">
<br>
<center><button type="button" class="btn btn-danger btn-large">Generate</button></center>
</div>
<div class="span2"></div>
</div>
</div>
</form>​
and here is the jQuery
$(function () {
$('button').click(function() {
$('#1_l').attr('checked', true);
});​
}

Form over several pages - URL should remain

Don´t know if my english is sufficient to explain this right:
I have made a Form that is needs 4 or 5 pages until final submit comes.
On every change the Post is added to the new page as a hidden field and a new script is called.
<form method="post" action="form4.php">
My problem is i dont want the browser to show the url to the new script and ideally keep it at the origin url.
I dont know the right terms to search on google or here for a solution. Maybe you can give me a hint how to accomplish this?
Another option is to handle the form by the same script in stages.
<form action="formhandler.php" method="post">
<input type="hidden" name="form_stage" value="1" />
....
</form>
and within the formhandler.php:
switch($_POST['form_stage']) {
case 5:
// handle the stage 5 stuff here
break;
case 4:
// handle stage 4 stuff here, output form for stage 5
break;
case 3:
// handle stage 3 stuff here, output form for stage 4
break;
case 2:
// ditto
break;
case 1:
default:
// display initial form here
}
Of course, you don't have to use a switch for this. A long sequence of if/else if works just as well, it comes down to personal preference/code complexity. But this basic workflow allows you to keep the same url and still display multiple different forms without having to resort to AJAX/javascript.
If the action of the form is form4.php, then you can't change which URL the form posts to. However, you can do a redirect immediately after processing the data of the post to the URL you want the user to be at.
Alternately, you can change your form actions so they all post to the same page and just allow for multiple types of posting in your PHP.
You can use Ajax and pass data from form to php script and than load new form. Or you can pass argument(hidden or not) to file:
<form action="form.php?p=1" method="post">
...
</form>
Hidden parameter:
<form action="form.php" method="post">
<input type="hidden" value="4" name="p">
...
</form>
Depending on p value you load proper form and do things with rest of data.
Load your php site inside an iframe within some page.. The url of this page will always remain.
think i found another solution and want to share it:
the solution with the switch and the solution with ajax made me think about how to solve this easier. why not make the form on one page and hide the different parts. what waas before a button to submit the part is now a button to hide previous part and show next.
<script type="text/javascript">
window.addEvent('domready', function()
{
$$('.blau').set('text', 'Erkennen Sie die Heilpflanze?');
$('page_2').slide('hide');
$('page_3').slide('hide');
$('page_4').slide('hide');
$('page_5').slide('hide');
var togglePrefix = 'toggle_', boxPrefix = 'page_', emptyPrefix = '';
$$('.submit_box a').addEvent('click', function(e)
{
e.stop();
var id = $(this.get('id').replace(togglePrefix,emptyPrefix));
var id_new = parseInt($(this).get('id').replace(togglePrefix, emptyPrefix)) + 1;
var next = ('page_'+id_new);
var id_old = $(this.get('id').replace(togglePrefix,boxPrefix));
$(id_old).set('slide', {duration: 'long', transition: 'linear'});
$(id_old).slide('out');
$(next).slide('in');
if (next == 'page_1')
{
}
if (next == 'page_2')
{
}
if (next == 'page_3')
{
}
if (next == 'page_4')
{
$$('.blau').set('text', '....und die letzte ist?');
}
if (next == 'page_5')
{
$$('.blau').set('text', 'Nur noch ein paar Daten:');
}
});
});
</script>
the html:
<form id="gewinnspiel" name="gewinnspiel" method="post" action="<?=$_SERVER[PHP_SELF]; ?>">
<div id="page_1">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage1" value="Kamille" /><span>Kamille</span></div>
<div><input type="radio" name="frage1" value="Kaktus" /><span>Kaktus</span></div>
<div><input type="radio" name="frage1" value="Krokus" /><span>Krokus</span></div>
</div>
<div class="submit_box"><a id="toggle_1" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_2">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage2" value="Ringelblume" /><span>Ringelblume</span></div>
<div><input type="radio" name="frage2" value="Rotklee" /><span>Rotklee</span></div>
<div><input type="radio" name="frage2" value="Ringelkraut" /><span>Ringelkraut</span></div>
</div>
<div class="submit_box"><a id="toggle_2" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_3">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage3" value="Enzian" /><span>Enzian</span></div>
<div><input type="radio" name="frage3" value="Eisenkraut" /><span>Eisenkraut</span></div>
<div><input type="radio" name="frage3" value="Eiche" /><span>Eiche</span></div>
</div>
<div class="submit_box"><a id="toggle_3" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_4">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage4" value="Wollblume" /><span>Wollblume</span></div>
<div><input type="radio" name="frage4" value="Tulpe" /><span>Tulpe</span></div>
<div><input type="radio" name="frage4" value="Rose" /><span>Rose</span></div>
</div>
<div class="submit_box"><a id="toggle_4" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_5">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box_ende">
<?php echo '<input name="date" type="hidden" value="', time(), '" />'; ?>
<div class="nosee">eMail:<input name="email" type="text" id="email" value="<?=$_POST['email']; ?>" size="30" /></div>
<div><span>Vorname:</span><input type="text" name="vorname" value="<?=$_POST['vorname']; ?>"/></div>
<div><span>Nachname:</span><input type="text" name="nachname" value="<?=$_POST['nachname']; ?>"/></div>
<div><span>Strasse:</span><input type="text" name="strasse" value="<?=$_POST['strasse']; ?>"/></div>
<div><span>Ort:</span><input type="text" name="ort" value="<?=$_POST['ort']; ?>"/></div>
<div><span>PLZ:</span><input type="text" name="plz" value="<?=$_POST['plz']; ?>"/></div>
<div><span>eMail:</span><input type="text" name="imehl" value="<?=$_POST['imehl']; ?>"/></div>
</div>
<div class="submit_box"><input id="submit" name="senden" type="submit" value="Senden" /></div>
</form>
</div>
</div>
</div>
all working ! thx for the inspiration!

Categories