validate button radio in form


E

elia

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
 
Ad

Advertisements

C

Cah Sableng

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
 
E

elia

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
 
T

Tim Streater

elia said:
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
 
Ad

Advertisements

E

Evertjan.

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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top