Connecting jQuery scripts with test results - php

I've been trying to find a way to render the Submit button on a dynamic quiz useless until a user has selected an answer to every question. I could simply insert "required" before the answer's closing tags, but I have three different types of answers - including questions that require users to choose any number of checkboxes.
The solution appears to be a jQuery script that requires a selection for each question. But I can't get anything to work. I think maybe I'm just not getting my scripts properly associated with my form.
This is an example of my test code:
<div class="Answer">
<label class="Wide" for="q'.$QID.'-'.$Value.'"><div class="Radio"><input type="radio" name="q'.$QID.'[]" id="q'.$QID.'-'.$Value.'" value="'.$Value.'" style="display: none;"> '.$Value.'. '.$QA.'</div></label></div>
This is what the HTML looks like:
<li id="q7">
<div class="Question">Media that pretend to be opposed to the mainstream media are called...</div>
<div class="Answer">
<label class="Wide" for="q7-A"><div class="Radio"><input type="radio" name="q7[]" id="q7-A" value="A" style="display: none;"> A. alternative media</div></label></div>
<div class="Answer">
<label class="Wide" for="q7-B"><div class="Radio"><input type="radio" name="q7[]" id="q7-B" value="B" style="display: none;"> B. soapboxes</div></label></div>
<div class="Answer">
<label class="Wide" for="q7-C"><div class="Radio"><input type="radio" name="q7[]" id="q7-C" value="C" style="display: none;"> C. underground media</div></label></div>
<div class="Answer">
<label class="Wide" for="q7-D"><div class="Radio"><input type="radio" name="q7[]" id="q7-D" value="D" style="display: none;"> D. yellow journalism</div></label></div>
</li>
And this is the form that processes everything:
<div id="quiz2" rel="key" style="margin-top: 50px;">
<form action="grade.php" method="post" id="quiz">
<ol>
<li style="display: none;">
<?php
echo join ($Base, '');
?>
</li>
</ol>
<input type="hidden" name="PreviousURL" value="<?php echo $MyURL; ?>" id="url" />
<input type="hidden" name="user_token" value="<?php echo isset($_POST['user_token']) ? $_POST['user_token'] : '' ; ?>" />
<input type="submit" value="Submit Quiz" />
</form>
</div><!-- quiz-container -->
So what do I have to do to make the following jQuery work with the above? I changed the ID number to quiz, matching my form. I don't understand what "index" means, but I've tried replacing "value" with a number of variables. Nothing works.
<script>
var inputs = $('#quiz :input');
inputs.each(function(index, value) {
if ($(value).val() == '') { //check if the input is empty
//return the error
echo 'ERROR!';
}
});
</script>
Here's another script that looks interesting, but I don't know how to associate it with my form:
<script>
function checkInput() {
var inputs = $("input");
check = 0;
for (var i = 0; i < inputs.size(); i++) {
var iVal = $(inputs[i]).val();
if (iVal !== '' && iVal !== null) {
$(inputs[i]).removeClass('input-error');
} else {
$(inputs[i]).addClass('input-error');
$(inputs[i]).focus(function() {
$("input").removeClass('input-error');
$(inputs[i]).off('focus');
});
check++;
}
}
if (check > 0) {
return false;
} else {
return true;
}
}
checkInput()
</script>
I've been playing with both scripts, manipulating various variables, but every time I click the Submit button without answering a question, it forwards me to the next page.

Related

How to bind radio type inputs to PHP for a quiz

I want to make a quiz test in PHP so that it will instantly show in every page if the answer chosen by the user is correct or not. So my quiz test will be shown as a question per page, please explain and show me how to modify the following code so that I will know what to do in the other pages:
<main>
<div class="big-div">
<p>1. Which is the biggest planet in our Solar System?</p>
<form method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
<label class="container"> D) Neptune
<input type="radio" name="biggestplanet" value="neptune">
<span class="checkmark"></span>
</label>
<input type="submit" value="Send" class="submit">
</form>
<?php
$answer1 = $_POST["getAttribute('biggestplanet')"];
if ($answer1 == "C) Jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
?>
So, briefly, this is my PHP code that I can't figure out how to write it correctly:
<?php
$answer1 = $_POST["getAttribute('biggestplanet')"];
if ($answer1 == "C) Jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
?>
I'm not for the moment interested in how to send the answers to a database, I just want to know how to show to the user if his answers are correct or not.
This is just a quick way to write this, I honestly would use a switch statement when making this into a quiz, but I corrected your errors.
If you wanted them to show the correct or incorrect answer without having to click the submit button than you would need to use Ajax to you would not need to refresh the page.
<main>
<div class="big-div">
<p>1. Which is the biggest planet in our Solar System?</p>
<form action="sotest.php" method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" id="uranus" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
<label class="container"> D) Neptune
<input type="radio" name="biggestplanet" value="neptune">
<span class="checkmark"></span>
</label>
<input type="submit" value="Send" class="submit">
</form>
<?php
if(isset($_POST['submit'])){
$answer1 = $_POST['biggestplanet'];
if ($answer1 == "jupiter") {
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
} else {
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
}
}
?>
//////////IN A SWITCH////////
<?php
if(isset($_POST['submit'])){ //stops page from submiting on page refresh
$answer1 = $_POST['biggestplanet'];
switch ($answer1) {
case "uranus":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
case "saturn":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
case "jupiter":
echo "<p style='color:red;font-size:20px;'>Correct answer!</p>";
break;
case "Neptune":
echo "<p style='color:red;font-size:20px;'>Wrong answer! The biggest planet in out Solar System is Jupiter!</p>";
break;
}
}
?>
I simplyfied it a bit. (easier to understand)
HTML:
<form method="POST">
<label class="container"> A) Uranus
<input type="radio" name="biggestplanet" value="uranus">
<span class="checkmark"></span>
</label>
<label class="container"> B) Saturn
<input type="radio" name="biggestplanet" value="saturn">
<span class="checkmark"></span>
</label>
<label class="container"> C) Jupiter
<input type="radio" name="biggestplanet" value="jupiter">
<span class="checkmark"></span>
</label>
</form
PHP:
if (isset($_POST['biggestplanet'])) {
$submittedAnswer = $_POST['biggestplanet']; // VALUE of the checked input - identified by NAME of your input
$correctAnswer = 'jupiter';
if ($submittedAnswer === $correctAnswer) {
echo 'Correct!';
} else {
echo 'Sorry, the correct answer is: ' . $correctAnswer;
}
}
If some day you dont know what exactly is submitted. Try a var_dump($_POST); to show submitted data.
The <label> tag is typically bound to an element using the 'for' attribute, which would match the id of the element to which it should be bound. Example:
<label for="uranus">Uranus</label>
<input type="radio" name="biggestplanet" id="uranus" value="Uranus">
<label for="saturn">Saturn</label>
<input type="radio" name="biggestplanet" id="saturn" value="Saturn">
<input type="submit" value="Submit">
And then to grab the value from the input, you'll need to use the 'name' for the input tag:
<form method="POST">
<label for="uranus">Uranus</label>
<input type="radio" name="biggestplanet" id="uranus" value="Uranus">
<label for="saturn">Saturn</label>
<input type="radio" name="biggestplanet" id="saturn" value="Saturn">
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['biggestplanet'])){
if($_POST['biggestplanet'] === 'Saturn'){
$answer1 = $_POST['biggestplanet'];
}elseif($_POST['biggestplanet'] === 'Uranus'){
$answer1 = $_POST['biggestplanet'];
}
echo $answer1;
}
?>

Make user check all answers on form

Is there a way to modify my database-powered quiz so that, if a user doesn't answer one or more questions, clicking the Submit button will trigger an alert? In other words, I want to force users to answer all the questions, even if they just guess. The scoring mechanism doesn't work correctly unless all the questions have a response.
Note that some questions (the last one in this example) may have multiple correct answers. I think all that matters is that at least one checkbox is selected.
<form action="" method="post" id="quiz">
<li id="q9">
<div class="Question">Scientists believe the universe is:</div>
<div class="Answer">
<label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9[]" id="q9-A" value="A" style="display: none;"> A. disappearing</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9[]" id="q9-B" value="B" style="display: none;"> B. expanding</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9[]" id="q9-C" value="C" style="display: none;"> C. contracting</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9[]" id="q9-D" value="D" style="display: none;"> D. becoming bipolar</div></label></div>
</li>
<li id="q10">
<div class="Question">Check each item that can be found in our solar system.</div>
<div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;">
<label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">planet</label>
<label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">asteroid</label>
<label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">comet</label>
<label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">black hole</label>
<label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">neutrino star</label>
<label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">quasar</label>
</div>
</li>
</ol>
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="54f3f5438292e" />
<input type="submit" value="Submit Quiz" />
</form>
Please check the complete solution to your question as below:
<form action="" method="post" id="quiz" onsubmit="return formValidate();">
<li id="q9">
<div class="Question">Scientists believe the universe is:</div>
<div class="Answer">
<label class="Wide" for="q9-A"><div class="Radio"><input type="radio" name="q9[]" id="q9-A" value="A"> A. disappearing</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-B"><div class="Radio"><input type="radio" name="q9[]" id="q9-B" value="B"> B. expanding</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-C"><div class="Radio"><input type="radio" name="q9[]" id="q9-C" value="C"> C. contracting</div></label></div>
<div class="Answer">
<label class="Wide" for="q9-D"><div class="Radio"><input type="radio" name="q9[]" id="q9-D" value="D"> D. becoming bipolar</div></label></div>
</li>
<li id="q10">
<div class="Question">Check each item that can be found in our solar system.</div>
<div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;">
<label for="q10-A"><input type="checkbox" name="q10[]" id="q10-A" value="A">planet</label>
<label for="q10-B"><input type="checkbox" name="q10[]" id="q10-B" value="B">asteroid</label>
<label for="q10-C"><input type="checkbox" name="q10[]" id="q10-C" value="C">comet</label>
<label for="q10-D"><input type="checkbox" name="q10[]" id="q10-D" value="D">black hole</label>
<label for="q10-E"><input type="checkbox" name="q10[]" id="q10-E" value="E">neutrino star</label>
<label for="q10-F"><input type="checkbox" name="q10[]" id="q10-F" value="F">quasar</label>
</div>
</li>
</ol>
<input type="hidden" name="PreviousURL" id="url" />
<input type="hidden" name="user_token" value="54f3f5438292e" />
<input type="submit" value="Submit Quiz" />
</form>
<script type="text/javascript">
function formValidate(){
var qno = 2; // Total Questions, say. in your example it's 2 (9 and 10)
var i = 9; // Question No to start with, say. in your example it's 9
var qcount = (i+qno)-1;
for(i; i <= qcount; i++){
qid = 'q'+i+'[]';
var qidElement = document.getElementsByName(qid);
var checkcount = 0;
for(var j=0; j < qidElement.length; ++j){
if(qidElement[j].checked){
++checkcount;
}
}
if(checkcount == 0){
alert("Please select at least one Answer for Question no. "+i);
return false;
}
}
document.getElementById('quiz').submit();
}
</script>
Input values for qno & i in javascript function as per your requirement, as guided by me in the relevant comments beside them.
Let me know if there is any issue. Thanks! :)
If you need to force users to fill all fields you have to use one off those validation methods :
1- Client side: via java script , but user can bypass it by disable java script "not recommended".
2- Server side: user can't disable it , and the code related to your programming language .
3- Mix type: Via Ajax and that is the best choice
Example
A-Check request in server side and return results in JSON type .
B-Process returned data via JavaScript and display results in the form
Check this links will help you so much:
http://www.formget.com/form-validation-using-ajax/
http://www.w3schools.com/php/php_form_validation.asp

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

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.

How to get the values of the checked radio button in iframe

I have here a page with iframe. Inside the iframe, it will display all the questions in multiple choice. The problem is that I don't know how to get all the values of the checked radio buttons inside the iframe. The button is in the parent page (I haven't included it yet).
This is the screen capture of my page (questions.php):
This is my iframe code from questions.php:
<div class="question-display test">
<iframe src="questions/frame.php" height="500px" width="100%">
</iframe>
</div>
This is my frame.php (displaying all the questions)
<?php
include('config.php');
$view_questions=mysql_query("SELECT * FROM questions ORDER BY RAND()");
$count=mysql_num_rows($view_questions);
$i=1;
while($row=mysql_fetch_array($view_questions))
{
?>
<div class="questions-contents text-size test" style="margin-left: 10px;">
<p align="justify"><strong><?php echo $i;?>.)</strong> <?php echo $row['QUESTION'];?></p>
</div>
<div class="answer-indent" style="margin-left: 40px; margin-bottom: 20px;">
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="1" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_1'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="2" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_2'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="3" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_3'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="4" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_4'];?></class="color">
</label>
<label class="radio" style="display: none;" >
<input type="radio" name="<?php echo $row['QUESTION_NO'];?>" value="5" data-toggle="radio" checked>
</label>
</div>
<?php
$i++; }
?>
Before I make my frame.php inserted to iframe from the questions.php page, I have a php codes for checking the answers of the users (the checked radio buttons).
$questions = mysql_query("SELECT * FROM questions");
while($row=mysql_fetch_assoc($questions)){
if($_POST[$row['QUESTION_NO']]==$row["ANSWER"])
{
$score++;
}
elseif($_POST[$row['QUESTION_NO']]==5)
{
$unanswered++;
}
else
{
$wrong_score++;
}
}
Please help me, I'm doing this since yesterday but I didn't get it. I don't know how to do it.
You can give id to your iframe and access its elements.
document.getElementById('frameid').contentWindow.document.getElementById('abc')
Where abc is your radio button element.
Then you can do whatever you want to do with element.
But seriously, you can do the same thing in Div also with proper css.
I think you should give same name to all the checkboxes. And you can allot the question no. for their id
So the form will be
<div class="answer-indent" style="margin-left: 40px; margin-bottom: 20px;">
<label class="radio">
<input type="radio" name="question" id="<?php echo $row['QUESTION_NO'];?>" value="1" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_1'];?></class="color">
</label>
<label class="radio">
<input type="radio" name="question" id="<?php echo $row['QUESTION_NO'];?>" value="2" data-toggle="radio">
<class="color" style="color: gray;"><?php echo $row['ANSWER_2'];?></class="color">
</label>
.
.
.
.
</div>
So we can get the checked box values using simple snippet.
showing below
<?php
$values = $_POST['question'];
// optional
foreach ($values as $selected){
echo $selected."<br />";
}
?>
Please try this
the iframe form action should be the iframe parent url
<form action="parenturl.php" method="POST">
When you dont add the action attribute to your form element the default action will
be submitting the form to the current page by GET method, so when you trying to
view the post data in the parent there is nothing to show because the data was posted to the
iframe itself and not to the parent.
so make sure you define the method and and action.

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