jquery if checkbox within parent div is checked then include php file? - php

QUESTION UPDATED
pqq.php file:
I have a form like so:
<form>
{General form input here, i.e. name, address}
<php include 'form_general.php'; ?>
{Specific form input here, i.e. equipment type}
<php include 'infrastructure.php'; ?>
<div id="placeholder"></div>
<submit>
</form>
So in my infrastructure.php file I have the following checkboxes contained within a couple of parent divs like so:
infrastrucure.php file:
Plant Hire Category
<div class="option_select"><p class="select">Plant Hire</p></div>
<div class="option_underlay">
<div class="align"><p class="header">Access Platforms</p>
<div class="float_column3">
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
<br>
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
<input type="checkbox" name="option" id="option" class="group1" value="same">Option
</div>
</div>
</div>
Travel Category
<div class="option_select"><p class="select">Travel</p></div>
<div class="option_underlay">
<div class="align"><p class="header">Planes</p>
<div class="float_column3">
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
<br>
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
<input type="checkbox" name="option" id="option" class="group2" value="same">Option
</div>
</div>
</div>
JQUERY in my pqq.php file:
<script>
$(document).ready(function () {
$('input[name=option]').change(function () {
if($(this).hasClass('group1')) {
$('#placeholder').load("safety.php");
} else if($(this).hasClass('group2')) {
$('#placeholder').load("travel.php");
}
});
});
</script>
what I am trying to do is if a user checks any checkbox from the Plant Hire category then it includes my safety.php file in the form. Otherwise if a user selects any checkbox from my travel category then my travel.php file is included in the form?
Does anyone know a way I can do this using jquery or other methods?
Thanks,

First make div, where the loaded php file will be stored, in my code it would be <div id="placeholder"></div>, then for each group of checkboxes use the same class, in my code it's class group-1 and class group-2. Then use something like this:
$(document).ready(function () {
$('input[name=option]').change(function () {
if ($(this).hasClass('group1')) {
var length = $('.group1:checked').length;
if (length) {
$('#placeholder').load("safety.php");
} else {
$('#placeholder').html("");
}
} else if ($(this).hasClass('group2')) {
var length = $('.group2:checked').length;
if (length) {
$('#placeholder').load("travel.php");
} else {
$('#placeholder').html("");
}
}
});
});
Can more checkboxes be selected? Or only one? If only one, you should probably uncheck all other checkboxes in the code.
Also you have in your sample code multiple same IDs, which is not a good practice.

Related

reading dynamicaly added check box value

<head>
<title>jQuery Add & Remove Dynamically Input Fields in PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<h1>jQuery Add & Remove Dynamically Input Fields in PHP</h1>
<form method="post" action="">
<div class="form-group fieldGroup">
<div class="input-group">
<input type="text" name="phno[]" class="form-control" placeholder="Enter No" />
<input type="checkbox" name="cdr[]" class="checkbox-inline cdr" />
<input type="checkbox" name="caf[]" class="checkbox-inline caf" />
<div class="input-group-addon">
<span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Add
</div>
</div>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="SUBMIT" />
</form>
<!-- copy of input fields group -->
<div class="form-group fieldGroupCopy" style="display: none;">
<div class="input-group">
<input type="text" name="phno[]" class="form-control" placeholder="Enter No" />
<input type="checkbox" name="cdr[]" class="checkbox-inline cdr" />
<input type="checkbox" name="caf[]" class="checkbox-inline caf" />
<div class="input-group-addon">
<span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Remove
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
<script>
$(document).ready(function() {
//group add limit
var maxGroup = 10;
//add more fields group
$(".addMore").click(function() {
if ($('body').find('.fieldGroup').length < maxGroup) {
var fieldHTML = '<div class="form-group fieldGroup">' + $(".fieldGroupCopy").html() + '</div>';
$('body').find('.fieldGroup:last').after(fieldHTML);
} else {
alert('Maximum ' + maxGroup + ' groups are allowed.');
}
});
//remove fields group
$("body").on("click", ".remove", function() {
$(this).parents(".fieldGroup").remove();
});
});
</script>
</body>
in the above code i get the check box values but not corresponding to the text field value
kindly help
i need out put as the data for eg
if i entered abcd then check one check box associated with it the the out i need is abcd-value of checkbox clicked
this will repetet up to the number of input group
I assume "cdrtxt" is in the same node as ".cdr"?
Use .parent() on ".cdr"
$(document).on('click', '.cdr', function() {
alert(1);
$(this).parent().find('.cdrtxt').val("1");
});
There are several issues with this one line of code:
$("this").find('.cdrtxt').val("1");
There is no such thing as $("this"). At least if you do not have a non-standard <this> tag in your HTML. When you want to refer to the node that received the event and wrap it in a jQuery object, you should use $(this) <-- no quotes.
There is no cdrtxt class in your HTML code.
The solution in your case is to search for the text input sibling of the clicked checkbox. Something like:
$(this).siblings('input[type=text]').val('something');
<input type="text" name="phno[]" class="form-control" placeholder="Enter No" />
<input type="checkbox" name="cdr[]" class="checkbox-inline cdr" />
You can not properly associate the text input field values and checkbox values, using this naming scheme.
Text fields always cause an entry in the form submission data set, even if they are empty - checkboxes don’t, they only create one if they actually where checked.
If you had the above combination of fields four times, but you only checked the first and third checkbox, then you would get $_POST['phno'] as an array of four text values, indexed from 0 to 3, and $_POST['cdr'] would contain only two checkbox values (you did not specify one here explicitly, so those checkboxes would submit the value on), indexed with 0 and 1.
Based on those indexes, it is impossible to tell now, which of those checkboxes where checked. If you had checked the third and fourth instead, you would still get those values under the indexes 0 and 1.
You need to specify the index upfront here:
<input type="text" name="phno[0]" class="form-control" placeholder="Enter No" />
<input type="checkbox" name="cdr[0]" class="checkbox-inline cdr" />
and that 0 needs to become 1 for the next set, and so on.
Then the checked checkboxes would get an index that corresponds to that of the text field.
There would still be “holes” in the indexes in $_POST['cdr'] - if you checked the first (index 0) and fourth (index 3) one, then $_POST['cdr'][0] and $_POST['cdr'][3] would be set afterwards.
But if you loop over $_POST['phno'], which contains all indexes from 0 to 3, then you can use that index value to check if the corresponding index in $_POST['cdr'] is set.
So you will have to modify your JavaScript part, that simply “clones” those existing rows, so that it sets the proper fields names, including the index.
The problem with checkbox is that if it is not checked then it is not posted with the form. If you check a checkbox and post a form you will get the value of the checkbox in the $_POST variable , if it's unchecked no value will be assigned to the $_POST variable. So as to know which checkbox is checked assign a distinct value to it.This code might solve your problem
<?php
if(isset($_POST['submit'])){
if(isset($_POST['checkbox'])){
echo 'Checkbox: ';
print_r($_POST['checkbox']);
}
if(isset($_POST['text'])){
$text=$_POST['text'];
echo 'Textbox: ';
print_r($text);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="tests.php">
<input type="checkbox" name="checkbox[]" class="check" value="1">
<br>
<input type="checkbox" name="checkbox[]" class="check" value="2">
<br>
<input type="checkbox" name="checkbox[]" class="check" value="3">
<br>
<input type="text" name="text[]" class="text">
<br>
<br>
<input type="submit" name="submit">
</form>
<button id="add">ADD</button>
</body>
<script>
$(document).ready(function()
{
$('#add').on('click',function()
{
var breaks="<br>"
var checkbox="<br>";
var textbox="<br><input type='text' name='text[]' class='text'>";
var check_length=$('.check').length;
console.log(check_length);
for(var i=check_length+1;i<check_length+4;i++)
{
checkbox=checkbox+"<input type='checkbox' name='checkbox[]' class='check' value='" + i + "'><br>";
}
$('input[type="text"]').last().after(checkbox,textbox);
});
});
</script>
</html>

How to connect Jquery draggable UI with mysql via AJAX

I would like to drag elements in my horizontal list and than save the position in mysql.
The text sort=1&sort=2.... ist just for testing purpose.
this is my code in jquery
$("#sortable").sortable({
stop: function(event, ui) {
var data = $(this).sortable('serialize', {key:"sort"});
$('#tags').text(data);
$.ajax({
data: data,
type: 'POST',
url: 'listpriority.php'
});
}
});
The sortable function is working. It serializes me the list.
Now on the server side i would like to recive the serialized data.
my php code:
<?php
print_r($_POST['sort']);
?>
But the print_r function show me only the length of the list.
the full php code should be:
<?php
include = ""; // the connection to sql
if(isset($_POST['sort'])) {
$sort = $_POST['sort'];
$sql = "UPDATE tags SET position='".$sort."'";
$result = mysqli_query($conn, $sql);
}
?>
My solution for the problem:
$( "#sortable" ).sortable({
update: function(event, ui) {
var data = $(this).sortable('serialize', { key: "sort" });
$('#tags').text(data);
$.post("listpriority.php", {data});
}
});
i changed the $.ajax to $.post and than i echo out the data in php echo($_POST['data']);
Try to get and set data from hidden field
<div class="main_div">
<div class="inner">
<span>1</span>
<input type="hidden" name="original[]" value="1">
<input type="hidden" name="new[]" class="new_ord" value="1">
</div>
<div class="inner">
<span>2</span>
<input type="hidden" name="original[]" value="2">
<input type="hidden" name="new[]" class="new_ord" value="2">
</div>
<div class="inner">
<span>3</span>
<input type="hidden" name="original[]" value="3">
<input type="hidden" name="new[]" class="new_ord" value="3">
</div>
<div class="inner">
<span>4</span>
<input type="hidden" name="original[]" value="4">
<input type="hidden" name="new[]" class="new_ord" value="4">
</div>
<div class="inner">
<span>5</span>
<input type="hidden" name="original[]" value="5">
<input type="hidden" name="new[]" class="new_ord" value="5">
</div>
</div>
And php script is
if(isset($_POST['post']) == 1) {
$original = $_POST['original'];
$new_val = $_POST['new_val'];
for ($i=0; $i < count($original); $i++) {
$sql = 'UPDATE TABLE Table_name SET order="'.$new_val[$i].'" WHERE id="'.$original[$i].'"';
echo $sql."\n\r";
}
}
See jquery code here and uncomment ajax call portion
Maybe it helps you

Select all checkbox with jQuery form POST for multiple checkboxes

For a project I want to implement a nice filtering mechanism with multiple checkboxes. I get the checkboxes to work correctly as well as a jQuery function to automatically POST the form when checking a checkbox.
Now I want to add a "select all" checkbox above the checkboxes, but I cannot seem to find the correct way. I have tried a dozen solutions for (somewhat) similar questions but I cannot get it to work correctly and consistent.
The HTML part is something like this:
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green"> Green</label><br/><br/>
<input type="checkbox" /> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand"> SomeOtherBrand</label><br/>
</form>
The jQuery part I use to post the form on each click on the checkbox (is not sufficient):
$('input:checkbox:').live('click',function() {
$(this).closest('form').submit();
});
My question now is what do I need for the jQuery part to make sure this works correctly?
I want to be able to click the label to deselect all from that group and select only that one checkbox. It also needs to POST the form for the array values. And lastly if all checkboxes are checked manually the "select all" one has to be checked as well.
Hopefully someone can help me out as I am stuck with this for a long time...
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#colorall").click(function()
{
var checked_status = this.checked;
$(".color").each(function()
{
this.checked = checked_status;
});
});
$(".color").click(function(){
if($(".color").length == $(".color:checked").length) {
document.getElementById("colorall").checked = true;
} else {
$("#colorall").removeAttr("checked");
}
});
$("#brandall").click(function()
{
var checked_status = this.checked;
$(".brand").each(function()
{
this.checked = checked_status;
});
});
$(".brand").click(function(){
if($(".brand").length == $(".brand:checked").length) {
document.getElementById("brandall").checked = true;
} else {
$("#brandall").removeAttr("checked");
}
});
});
</script>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<input type="checkbox" id="colorall" /> Select All colors<br/>
<label> <input type="checkbox" name="color[]" value="yellow" class="color"> Yellow</label><br/>
<label> <input type="checkbox" name="color[]" value="blue" class="color"> Blue</label><br/>
<label> <input type="checkbox" name="color[]" value="red" class="color"> Red</label><br/>
<label> <input type="checkbox" name="color[]" value="green" class="color"> Green</label><br/><br/>
<input type="checkbox" id="brandall"/> Select All brands<br/>
<label> <input type="checkbox" name="brand[]" value="Nike" class="brand"> Nike</label><br/>
<label> <input type="checkbox" name="brand[]" value="Adidas" class="brand"> Adidas</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeBrand" class="brand"> SomeBrand</label><br/>
<label> <input type="checkbox" name="brand[]" value="SomeOtherBrand" class="brand"> SomeOtherBrand</label><br/>
</form>

How can I determine the current ID of the input field type "submit" for use in a PHP else if conditional expression?

I am still learning PHP, so bear with me. I am trying to create a sign up form that includes 2 DIVs. Once you click submit on the first div (personal info), it slides away and the second div (billing info) slides up using jQuery.
My problem is... I need some help figuring out how to determine if the submit function came from the first div or the second. If it's the first div, the slide happens. If it's the second div, the form is submitted.
HTML with Form
<div id="container">
<!-- #first_step -->
<div id="first_step">
<h1>SIGN UP FOR 17 FIT CLUB</h1>
<form class="signup" action="post/signup" method="post">
<input type="hidden" name="step" value="user" />
<div class="form">
<input type="text" name="email" id="email_signup" value="" />
<label for="email">Your email address. We send important administration notices to this address. </label>
<input type="text" name="confirmemail" id="cemail_signup" value="" />
<label for="username">Please re-type your email to verify it is correct.</label>
<input type="text" name="firstname" id="firstname_signup" value="" />
<label for="firstname">Your First Name. </label>
<input type="text" name="lastname" id="lastname_signup" value="" />
<label for="lastname">Your Last Name. </label>
<input type="text" name="username" id="username_signup" value="" />
<label for="username">At least 6 characters. Uppercase letters, lowercase letters and numbers only.</label>
<input type="password" name="password" id="password_signup" value="" />
<label for="password">At least 6 characters. Use a mix of upper and lowercase for a strong password.</label>
<input type="password" name="cpassword" id="cpassword_signup" value="" />
<label for="cpassword">If your passwords aren’t equal, you won’t be able to continue with signup.</label>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="submit" type="submit" name="submit_first" id="submit_first" value="submit"/>
</form>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<!-- #second_step -->
<div id="second_step">
<h1>SIGN UP FOR 17 FIT CLUB</h1>
<form class="signup" action="post/signup" method="post">
<input type="hidden" name="step" value="user" />
<div class="form">
<input type="text" name="nameoncard" id="nameoncard_signup" value="" />
<label for="email">Enter the name on the credit or debit card used for billing. </label>
<select name="cardtype" id="cardtype_signup" >
<option name="visa">Visa</option>
<option name="mastercard">Mastercard</option>
<option name="amex">American Express</option>
<option name="discover">Discover</option>
</select>
<label for="cardtype">What type of card are you using?</label>
<input type="text" name="cardnumber" id="cardnumber_signup" value="" />
<label for="cardnumber">Enter the card number.</label>
<div id="exp_date_signup">
<select name="exp_month" id="exp_month_signup" >
<option name="01">01</option>
<option name="02">02</option>
<option name="03">03</option>
<option name="04">04</option>
</select>
<select name="exp_year" id="exp_year_signup" >
<option name="12">12</option>
<option name="13">13</option>
<option name="14">14</option>
<option name="15">15</option>
</select>
</div>
<label for="exp_year">Enter the expiration date on the card.</label>
<input type="text" name="CVV2" id="cvv2_signup" value="" />
<label for="CVV2">Enter the 3 or 4 digit CVV2 number.</label>
<select name="country" id="country_signup">
<option value=" " selected>(please select a country)</option>
<option value="AF">Afghanistan</option>
<option value="ZM">...More options...</option>
<option value="ZW">Zimbabwe</option>
</select>
<label for="country">Enter the country for your billing address.</label>
<input type="text" name="billingaddress" id="billingaddress_signup" value="" />
<label for="bilingaddress">Enter the street name and number for the credit or debit card billing address.</label>
<input type="text" name="billingcity" id="billingcity_signup" value="" />
<label for="billingcity">Enter the city for you billing address.</label>
<select name="billingstate" id="billingstate_signup">
<option value="AL">Alabama</option>
<option value="AK">...More options...</option>
<option value="WY">Wyoming</option>
</select>
<label for="billingstate">Choose the state for your billing address.</label>
<input type="text" name="billingpostalcode" id="billingpostalcode_signup" value="" />
<label for="cpassword">Enter the postal code for your billing address.</label>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
<input class="send submit" type="submit" name="submit_second" id="submit_second" value="submit" />
</form>
</div> <!-- clearfix --><div class="clear"></div><!-- /clearfix -->
</div>
Javascript (I put "???" in the area I assume I need help)
<script type="text/javascript">
$(function(){
$('form.signup').submit(function(event){
event.preventDefault();
uri = $(this).attr('action');
data = $(this).queryString();
$.get(uri, data, function(response){
if(response.status == 0){
alert(response.message);
}
else if(???){
//show next step
$('#first_step').slideUp();
$('#second_step').slideDown();
}
else {
// redirect to internal home page
window.location = '<?=PROTOCOL?>//<?=DOMAIN?>/home';
}
}, 'json');
});
$('form.signup input').focus(function(event){
if(!$(this).hasClass('clicked')){
$(this)
.val('')
.addClass('clicked');
}
});
});
</script>
Any help would be greatly appreciated! I am sure this has a simple solution, but I haven't been able to crack it. n00b!
UPDATE:
ANSWER LISTED BELOW
What I would recommend is to combine both of the forms into only one form; split up the two "forms" with div tags with two separate buttons and then have jquery like this
//First Button Click Handler
$.("form div#first input.submit").click(function(){
$("div.first").slideUp('fast',function(){
$("div.second").slideDown('fast');
});
});
//Second Button Click Handler
$.("form div#second input.submit").click(function(){
var data = $('form').serialize();
var url = "whatever";
$.get(url,data,function(response){
//Handle Response
});
});
The trick is to disable the form's normal submit triggers and then handle them directly using specific click handlers
Something like this in the html will stop your form submitting by default
<form onsubmit="return false;">
<input type="password"/>
<input type="submit"/>
</form>
Have a next button that calls the jQuery function and the submit button at the bottom of the billing info. Ex:
function(slide){
$('#first_step').slideUp();
$('#second_step').slideDown();
}
Here is the answer to my question. I figured it out with the assistance of a person in RL. Both submit inputs have their own value, "user" and "billing" respectively.
<script type="text/javascript">
$(function(){
$('form.signup').submit(function(event){
event.preventDefault();
uri = $(this).attr('action');
data = $(this).queryString();
step = $(this).find('input[name=step]').val(); // <--update
if(response.status == 0){
alert(response.message);
}
else{ // <--update
if(step == 'user'){ // <--update
//show next slide // <--update
$('#first_step').slideUp(); // <--update
$('#second_step').slideDown(); // <--update
} // <--update
else if(step == 'billing'){ // <--update
//redirect to internal home page // <--update
window.location = '<?=PROTOCOL?>//<?=DOMAIN?>/home'; // <--update
} // <--update
}
}, 'json');
});
$('form.signup input').focus(function(event){
if(!$(this).hasClass('clicked')){
$(this)
.val('')
.addClass('clicked');
}
});
});
</script>

Hiding divs based on a selected radio button

I'm having an issue with radio buttons showing one div and hiding the rest. I have my code which I'll post below (tested and working in jsfiddle but not when I put it onto my page).
To give you an idea of what I'm after, I am making a registration page. The radio buttons will control a div that has the form placed inside it, so radio 1 checked, div 1 is selected, and the rest hidden and so on.
Here's my jquery:
<script type="text/javascript">
$('#accountchoice').change(function() {
if ($('#personalfan').attr('checked')) {
$('#personalfan1').show();
$('#soloartist1').hide();
$('#band1').hide();
$('#venue1').hide();
$('#business1').hide();
$('#service1').hide();
} else if ($('#soloartist').attr('checked')) {
$('#soloartist1').show();
$('#personalfan1').hide();
$('#band1').hide();
$('#venue1').hide();
$('#business1').hide();
$('#service1').hide();
} else if ($('#band').attr('checked')) {
$('#band1').show();
$('#personalfan1').hide();
$('#soloartist1').hide();
$('#venue1').hide();
$('#business1').hide();
$('#service1').hide();
} else if ($('#venue').attr('checked')) {
$('#venue1').show();
$('#personalfan1').hide();
$('#soloartist1').hide();
$('#band1').hide();
$('#business1').hide();
$('#service1').hide();
} else if ($('#business').attr('checked')) {
$('#business1').show();
$('#personalfan1').hide();
$('#soloartist1').hide();
$('#band1').hide();
$('#venue1').hide();
$('#service1').hide();
} else if ($('#service').attr('checked')) {
$('#service1').show();
$('#personalfan1').hide();
$('#soloartist1').hide();
$('#band1').hide();
$('#venue1').hide();
$('#business1').hide();
}
});
</script>
and here's the HTML
<body>
<?php include_once "header_template.php"; ?>
<div id="signupwrapper">
<div id="signupinner">
<h3 align="left"> GETSCENE REGISTRATION ! </h3>
<hr />
<div id="signup" style="border:thin; border-color:#666">
<h4 align="left">Please Choose One of The Following Account Types</h4>
<div id="accountswrapper">
<form id="accountchoice" name="accountchoice" method="post" action="">
<label for="radio">personal/fan</label>
<input type="radio" name="radio" id="personalfan" value="radio1" checked="checked" />
<label for="radio2">Solo artist</label>
<input type="radio" name="radio" id="soloartist" value="radio2" />
<label for="radio3">band</label>
<input type="radio" name="radio" id="band" value="radio3" />
<label for="radio4">venue</label>
<input type="radio" name="radio" id="venue" value="radio4" />
<label for="radio5">business</label>
<input type="radio" name="radio" id="business" value="radio5" />
<label for="radio6">service</label>
<input type="radio" name="radio" id="service" value="radio6" />
</form>
<hr />
<div id="personalfan1">1</div>
<div id="soloartist1">2</div>
<div id="band1">3</div>
<div id="venue1">4</div>
<div id="business1">5</div>
<div id="service1">6</div>
</div>
</div>
</div>
</div>
<?php include_once "footer_template.php"; ?>
</body>
Any help here will be really appreciated as I've been banging my head against this for hours.
This script
<script type="text/javascript">
$('#accountchoice').change(function() {
Will only work if it's at the end of your body tag. If it's in the head, it won't do anything, since the dom won't be ready when the script runs.
The easiest way to fix this is to put this code into a document.ready handler:
$(function() {
//this will run when the dom is ready
$('#accountchoice').change(function() {
});
You should be able to trim this code down and do it all in one shot by looking at the radio that's checked
var allDivs = ['venue1', 'personalfan1', 'soloartist1', 'band1', 'business1', 'service1'];
var radio = $("input[type='radio']:checked");
if (radio.length === 0)
return;
$('#' + allDivs.join(',#')).hide();
$("div#" + radio[0].id + "1").show();
You could restructure your HTML to make your life MUCH easier.
Here is an example:
<style type="text/css">
#account_types > div { display: none; }
</style>
<div id="signupwrapper">
<div id="signupinner">
<h3 align="left"> GETSCENE REGISTRATION ! </h3>
<hr />
<div id="signup" style="border:thin; border-color:#666">
<h4 align="left">Please Choose One of The Following Account Types</h4>
<div id="accountswrapper">
<form id="accountchoice" name="accountchoice" method="post" action="">
<label for="personalfan">personal/fan</label>
<input type="radio" name="radio" id="personalfan" value="radio1" checked="checked" />
<label for="soloartist">Solo artist</label>
<input type="radio" name="radio" id="soloartist" value="radio2" />
<label for="band">band</label>
<input type="radio" name="radio" id="band" value="radio3" />
<label for="venue">venue</label>
<input type="radio" name="radio" id="venue" value="radio4" />
<label for="business">business</label>
<input type="radio" name="radio" id="business" value="radio5" />
<label for="service">service</label>
<input type="radio" name="radio" id="service" value="radio6" />
</form>
<hr />
<div id="account_types">
<div class="personalfan">1</div>
<div class="soloartist">2</div>
<div class="band">3</div>
<div class="venue">4</div>
<div class="business">5</div>
<div class="service">6</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#accountchoice').change(function() {
var divToShow = $(this).find('input:checked').attr('id');
$('#account_types > div').each(function() {
if($(this).hasClass(divToShow)) { $(this).show(); }
else { $(this).hide();}
});
});
$('#accountchoice').trigger('change');
});
</script>
Now, if you'll take a note at the restructuring, you'll see a few things. First, the Divs that contained the different account types are inside a holding Div. You don't need to do that, but it makes it easier to isolate them. You could just as easily have given them all a similar class to accomplish the same task.
Secondly, they now have class names that are the same as their associated input ID. This makes it very easy to reference the exact one you are looking for, while still making it easy to touch the rest of them too. So now you can loop through the array of these elements, show() the Div that goes along with the selected radio, and hide() the ones that aren't. All in a few small steps.
This also makes it much easier to add new parts to your code. Otherwise, if you added a new section, you'd have to edit each if() statement you made, adding the new section to make sure it shows and hides properly.
This is where you can see some of the power of the DOM. Much simpler code, easier to maintain, and you can reuse it later on.
I also fixed the labels for you too.
Here is another example, though it does require a little change to HTML to make it neat/easy to manage.
HTML
// Added class "choice" to the radio inputs
<input type="radio" name="radio" id="venue" class="choice" value="radio4" />
<input type="radio" name="radio" id="business" class="choice" value="radio5" />
<input type="radio" name="radio" id="service" class="choice" value="radio6" />
<div class="account_types">
<div class="dynamic" id="venue1">
4</div>
<div class="dynamic" id="business1">
5</div>
<div class="dynamic" id="service1">
6</div>
</div>
SCRIPT
$('input.choice').click(function () {
var eleToShow = this.id + "1";
$('div', '.account_types').hide();
$('#' + eleToShow).show();
});

Categories