form radio alert working but not correctly
Moderator: General Moderators
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
form radio alert working but not correctly
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
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
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.
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: form radio alert working but not correctly
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
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
Hi,Can u Please post the html code also.
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: form radio alert working but not correctly
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>
<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
Hi,Try this
Code: Select all
if (!checkRadio("form2","weeks"))
{
alert("Duration needs to be selected");
return false;
}
Last edited by Gopesh on Mon May 07, 2012 11:58 pm, edited 1 time in total.
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: form radio alert working but not correctly
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
<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
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);
#Show errors
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: form radio alert working but not correctly
yes i added the code but it doesnt do anything. after i have filled the weeks in i cant go any further??
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: form radio alert working but not correctly
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
#Show errors
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);
on the other post
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: form radio alert working but not correctly
this is a javascript problem not a php???sw
Re: form radio alert working but not correctly
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.
-
jonnyfortis
- Forum Contributor
- Posts: 462
- Joined: Tue Jan 10, 2012 6:05 am
Re: form radio alert working but not correctly
ok thanks i did use the php code but it didnt give no errors?
Re: form radio alert working but not correctly
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
Hi, i didn't see any Start Date in ur html.I have modified it with end date. check this code
Html
javascript
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>
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>