473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

validate button radio in form

Hello,

I use php and javascript on a form.
My validate script doesn't work with radio button. What's wrong? I
want to be sure that one of the button is press. M or F

I get on my first page:

<?php
$Genre=$_POST['genre'];
?>
<head>
<script type="text/javascript">
function verif_champs()
{

if(document.forml.Genre.value == "")
{
alert("Choose the gender!");
document.forml.Reference.focus();
return false;
}
</script>
</head>
<body>
<form name="forml" method="post" onSubmit="return verif_champs()"
action="test22.php" >
<table>
<tr><td>
<?
$checked[$_POST['Genre']]='checked="checked"';
echo '<input type = "radio" name="Genre" value="F"
'.$checked['F'].'/>';
echo '<input type = "radio" name="Genre" value="M"
'.$checked['M'].'/>';
? <input type="submit"></td></tr>
</table>
</form>

On my page 2:

<?php
$Genre=$_POST['Genre'];
?>
<body>
<table>
<tr><td>
<form name="form2" ACTION="test11.php" method="post">
<? foreach ($_POST as $key=>$val)
{
echo '<input type = "hidden" name="'.htmlentities($key).'"
value="'.htmlentities($val).'" />';
}
?>
<? echo $Genre ?>
<input type="submit" name="Submit2" value="Back" />
</form>
</td></tr></table>
</body>

Thanks a lot. Pascal

Mar 27 '07 #1
5 1635
In article <11**********************@b75g2000hsg.googlegroups .com>,
"elia" <jo****@pcl.chwrote:
Hello,

I use php and javascript on a form.
My validate script doesn't work with radio button. What's wrong? I
want to be sure that one of the button is press. M or F

I get on my first page:

<?php
$Genre=$_POST['genre'];
?>
<head>
<script type="text/javascript">
function verif_champs()
{

if(document.forml.Genre.value == "")
{
alert("Choose the gender!");
document.forml.Reference.focus();
return false;
}
Where is the closing } for this function?

</script>
</head>
<body>
<form name="forml" method="post" onSubmit="return verif_champs()"
action="test22.php" >
<table>
<tr><td>
<?
$checked[$_POST['Genre']]='checked="checked"';
echo '<input type = "radio" name="Genre" value="F"
'.$checked['F'].'/>';
echo '<input type = "radio" name="Genre" value="M"
'.$checked['M'].'/>';
? <input type="submit"></td></tr>
</table>
</form>

On my page 2:

<?php
$Genre=$_POST['Genre'];
?>
<body>
<table>
<tr><td>
<form name="form2" ACTION="test11.php" method="post">
<? foreach ($_POST as $key=>$val)
{
echo '<input type = "hidden" name="'.htmlentities($key).'"
value="'.htmlentities($val).'" />';
}
?>
<? echo $Genre ?>
<input type="submit" name="Submit2" value="Back" />
</form>
</td></tr></table>
</body>

Thanks a lot. Pascal
-- tim
Mar 27 '07 #2
ok, sorry, thanks, but even with } it's doesn't work!! Pascal

Mar 27 '07 #3
On Mar 27, 7:43 pm, "elia" <jos...@pcl.chwrote:
function verif_champs()
{

if(document.forml.Genre.value == "")
{
alert("Choose the gender!");
document.forml.Reference.focus();
return false;
}
For a multiple elements with same name, the element becomes
collection. It can be accessed like an array. Use some code like:

function verif_champs(f)
{
var z=f.elements['Genre'];
var y=z.length;
while (y--)
{
if (z[y].checked)
{
return true;
}
}
alert('choose genre');
return false;
}
<form name="forml" method="post" onSubmit="return verif_champs()"
action="test22.php" >
The form declaration should give parameter to the new function

<form name="forml" method="post" onSubmit="return verif_champs(this)"
action="test22.php" >

HTH

Mar 27 '07 #4
Ok, thanks, I will try with this, but my problem is that I have many
differents buttons ask like do you like this or that, + other field or
checkbox on my form ... and I want to use the same script, that's mean
one script for all my form...

Pascal

Mar 27 '07 #5
elia wrote on 27 mrt 2007 in comp.lang.javascript:
Ok, thanks, I will try with this, but my problem is that I have many
differents buttons ask like do you like this or that, + other field or
checkbox on my form ... and I want to use the same script, that's mean
one script for all my form...
Please, this is no email, so please quote.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Pete | last post by:
I have form/select which executes a function using onchange. No problem. However, when I validate the page with a strict HTML 4.01 doctype at http://validator.w3.org, it demands either an action or...
3
by: David Jubinville | last post by:
Hi All, I'm developing a 'testing' page for the current courseware solution I'm working on. Currently I have a single page with each question residing in it's own layer <DIV>. I want to make...
2
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
7
by: nathaniel.k.lee | last post by:
Is it not possible, in IE, to dynamically click a radio button? I'm grabbing some values from a database and using them to populate radio buttons on a page. I have alternate code for Firefox...
2
by: K Forester | last post by:
Hi, I am a complete beginner and need help with a form. There are 2 group boxes, each containing 3 radio buttons on my form. I'd like to write a subroutine in a module that checks to be sure that...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
11
by: TokyoJ | last post by:
I run a small camp in Alaska for kids and my director is asking for a web form. Could someone please have a look and offer some advice on where I'm making mistake(s)? I'm using the RegExp function...
5
by: elia | last post by:
Hello, I use php and javascript on a form. My validate script doesn't work with radio button. What's wrong? I want to be sure that one of the button is press. M or F I get on my first page: ...
1
by: huhuhuhu | last post by:
i am trying to validate radiobutton n checkbox but to no avail. this is the code that i m using to validate radiobutton>> <script language="JavaScript"> function checkForm() { var cgender...
3
by: 1051109210 | last post by:
<table class="special3" align="left" width="75%" height="65%" > <form> <table width="50%" align="center"> <tr align="right"> <td></td> <td><a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.