validate button radio in form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • elia

    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.for ml.Genre.value == "")
    {
    alert("Choose the gender!");
    document.forml. Reference.focus ();
    return false;
    }
    </script>
    </head>
    <body>
    <form name="forml" method="post" onSubmit="retur n verif_champs()"
    action="test22. php" >
    <table>
    <tr><td>
    <?
    $checked[$_POST['Genre']]='checked="chec ked"';
    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="'.htmlent ities($key).'"
    value="'.htmlen tities($val).'" />';
    }
    ?>
    <? echo $Genre ?>
    <input type="submit" name="Submit2" value="Back" />
    </form>
    </td></tr></table>
    </body>

    Thanks a lot. Pascal

  • Tim Streater

    #2
    Re: validate button radio in form

    In article <1174999392.257 336.277940@b75g 2000hsg.googleg roups.com>,
    "elia" <joseph@pcl.chw rote:
    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.for ml.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="retur n verif_champs()"
    action="test22. php" >
    <table>
    <tr><td>
    <?
    $checked[$_POST['Genre']]='checked="chec ked"';
    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="'.htmlent ities($key).'"
    value="'.htmlen tities($val).'" />';
    }
    ?>
    <? echo $Genre ?>
    <input type="submit" name="Submit2" value="Back" />
    </form>
    </td></tr></table>
    </body>
    >
    Thanks a lot. Pascal
    -- tim

    Comment

    • elia

      #3
      Re: validate button radio in form

      ok, sorry, thanks, but even with } it's doesn't work!! Pascal

      Comment

      • Cah Sableng

        #4
        Re: validate button radio in form

        On Mar 27, 7:43 pm, "elia" <jos...@pcl.chw rote:
        function verif_champs()
        {
        >
        if(document.for ml.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="retur n verif_champs()"
        action="test22. php" >
        The form declaration should give parameter to the new function

        <form name="forml" method="post" onSubmit="retur n verif_champs(th is)"
        action="test22. php" >

        HTH

        Comment

        • elia

          #5
          Re: validate button radio in form

          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

          Comment

          • Evertjan.

            #6
            Re: validate button radio in form

            elia wrote on 27 mrt 2007 in comp.lang.javas cript:
            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)

            Comment

            Working...