Page 1 of 2
form radio alert working but not correctly
Posted: Sun May 06, 2012 1:25 pm
by jonnyfortis
I have a form with textfeilds and radio buttons
i have it working but when the form is submitted when it gets to a radio button that hasn't been checked it notify's this but also the alert after i tried adding an elseif statement but that shows errors, could someone look and show me where i am going wrong
<script type="text/javascript">
function validateForm()
{
var x=document.forms["form2"]["StartDate"].value;
if (x==null || x=="")
{
alert("Start Date needs to be filled in");
return false;
}
function checkRadio (frmName, rbGroupName) {
var radios = document[frmName].elements[rbGroupName];
for (var i=0; i <radios.length; i++) {
if (radios.checked) {
return true;
}
}
return false;
}
{
if (!checkRadio("form2","weeks"))
alert("Duration needs to be selected");
}
elseif
var x=document.forms["form2"]["EndDate"].value;
if (x==null || x=="")
{
alert("End Date needs to be filled in");
return false;
}
if (!checkRadio("form2","Title"))
alert("Title needs to be selected");
var x=document.forms["form2"]["userid"].value;
if (x==null || x=="")
{
alert("user id needs to be filled in");
return false;
}
</script>
thanks in advance
Re: form radio alert working but not correctly
Posted: Sun May 06, 2012 10:33 pm
by Robert07
If you are getting errors, please include them in your question. It is not clear what you are asking here, please describe what is happening and what you expect to be happening.
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 5:21 am
by jonnyfortis
no i am not getting errors it works but after it prompts a radio button if the button / text area isnt filled in below it prompts this one below, i want it just to prompt the radio....rather than the following text area / radio below, there needs to something after the
if (!checkRadio("form2","weeks"))
alert("Duration needs to be selected");
}
to stop it jumping to the next alert
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 7:36 am
by Gopesh
Hi,Can u Please post the html code also.
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 8:14 am
by jonnyfortis
i have included the form below
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2" onsubmit="return validateForm()">
<input type="hidden" name="MM_insert" value="form2" />
<input type="radio" value="364" name="weeks" />
52 weeks
<input type="radio" value="301" name="weeks" />
<input type="text" value="" name="EndDate" size="30" />
<td><span class="table-text">Mr
<input type="radio" name="Title" value="Mr" />
Mrs
<input type="radio" name="Title" value="Mrs" />
</span></td>
<td><div align="right"><span class="table-text">Miss
<input type="radio"name="Title" value="Miss" />
Ms
<input type="radio" name="Title" value="Ms" />
<input name="userid" type="text" id="userid" value="" size="30" />
<input type="submit" value="submit" />
</form>
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 8:33 am
by Gopesh
Hi,Try this
Code: Select all
if (!checkRadio("form2","weeks"))
{
alert("Duration needs to be selected");
return false;
}
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 10:58 am
by jonnyfortis
hello i tried adding what you suggested and it worked to a point however it doesnt give ANY errors or anything after the Start date.
<script type="text/javascript">
function validateForm()
{
var x=document.forms["form2"]["StartDate"].value;
if (x==null || x=="")
{
alert("Start Date needs to be filled in");
return false;
}
function checkRadio (frmName, rbGroupName) {
var radios = document[frmName].elements[rbGroupName];
for (var i=0; i <radios.length; i++) {
if (radios.checked) {
return true;
}
}
return false;
}
{
if (!checkRadio("form2","weeks"))
alert("Duration needs to be selected");
return false;
}
var x=document.forms["form2"]["EndDate"].value;
if (x==null || x=="")
{
alert("End Date needs to be filled in");
return false;
}
if (!checkRadio("form2","Title"))
alert("Title needs to be selected");
var x=document.forms["form2"]["userid"].value;
if (x==null || x=="")
{
alert("user id needs to be filled in");
return false;
}
the form is longer than this but if i can get the top part working i ca do the rest.
thanks
Jon
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 11:03 am
by Robert07
Have you tried adding this php code at the top of the file in order to make sure you are seeing all the errors?
#Show errors
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 11:24 am
by jonnyfortis
yes i added the code but it doesnt do anything. after i have filled the weeks in i cant go any further??
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 11:26 am
by jonnyfortis
i think we are getting the post muddled up. i will try this
#Show errors
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
on the other post
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 11:32 am
by jonnyfortis
this is a javascript problem not a php???sw
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 11:59 am
by Robert07
I understand it is a javascript problem but php is used to update the error printing configuration. You might also try using firefox with the firebug plugin and check the console for any javascript errors happening on the client side.
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 3:27 pm
by jonnyfortis
ok thanks i did use the php code but it didnt give no errors?
Re: form radio alert working but not correctly
Posted: Mon May 07, 2012 3:42 pm
by Robert07
Have you tried checking the console for javascript errors or warnings in the firebug plugin for firefox? Are you familiar with that?
Re: form radio alert working but not correctly
Posted: Tue May 08, 2012 12:09 am
by Gopesh
Hi, i didn't see any
Start Date in ur html.I have modified it with end date. check this code
Html
Code: Select all
<form action="action.php" method="post" name="form2" id="form2" onsubmit="return validateForm()" >
<input type="hidden" name="MM_insert" value="form2" />
<input type="radio" value="364" name="weeks" />
52 weeks
<input type="radio" value="301" name="weeks" />
<input type="text" value="" name="EndDate" size="30" />
<table>
<tr>
<td><span class="table-text">Mr
<input type="radio" name="Title" value="Mr" />
Mrs
<input type="radio" name="Title" value="Mrs" />
</span></td></tr>
<tr> <td><div align="right"><span class="table-text">Miss
<input type="radio"name="Title" value="Miss" />
Ms
<input type="radio" name="Title" value="Ms" /></td></tr>
<input name="userid" type="text" id="userid" value="" size="30" />
<input type="submit" value="submit" />
</form>
javascript
Code: Select all
<script type="text/javascript">
function validateForm()
{
var x=document.forms["form2"]["EndDate"].value;
if (x==null || x=="")
{
alert("Start Date needs to be filled in");
return false;
}
function checkRadio (frmName, rbGroupName) {
var radios = document[frmName].elements[rbGroupName];
for (var i=0; i <radios.length; i++) {
if (radios[i].checked) {
return true;
}
}
return false;
}
{
if (!checkRadio("form2","weeks"))
alert("Duration needs to be selected");
return false;
}
elseif
var x=document.forms["form2"]["EndDate"].value;
if (x==null || x=="")
{
alert("End Date needs to be filled in");
return false;
}
if (!checkRadio("form2","Title"))
alert("Title needs to be selected");
var x=document.forms["form2"]["userid"].value;
if (x==null || x=="")
{
alert("user id needs to be filled in");
return false;
}
}
</script>