#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Hong Kong
    Posts
    71
    Rep Power
    15
    I want to check the empty text in the form with javascript and php. What's wrong in this program? How to solve it? Thanks!

    <html>
    <script language="javascript">
    <!--

    option_desc=new makearray(<? echo $num-1; ?> );

    function checkform() {

    <?
    for ($i=0; $i<$num; $i++) {
    ?>
    if (document.poll_form.option_desc[<? echo $i; ?>].value=="") {
    alert("Text<? echo $i+1; ?> is empty.");
    document.poll_form.option_desc[<? echo $i; ?>].focus();
    return false;
    }
    <?
    }
    ?>
    return true;
    }

    //-->
    </script>

    <body>
    <form method="post" action="" name="poll_form" onsubmit="return checkform();">
    <?
    for ($i=0; $i<$num; $i++) {
    ?>
    <p>
    <input type="text" name="option_desc[<? echo $i; ?>]" size="50">
    </p>
    <?
    }
    ?>
    <input type="submit" name="submit">
    </form>
    </body>
    </html>

    [This message has been edited by RayRay (edited June 16, 2000).]
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    The javascript routine is client side - the onSubmit() executes client side before the form is posted and therfore before the values in option_desc[] exist-

    if you send the page to itself via the form ,then validation will occur but your validation routine runs before the form is actually submitted. onSubmit() return true; - will only send the form & post the variables if the validation was successful.

    If you see what I mean?

    Simon

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Hong Kong
    Posts
    71
    Rep Power
    15
    Now, is the javascript doesn't work. It can't validation the text data.
    How to solve it?
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    Hi RayRay - listen - apologies my last answer was gibberish - not sure where I was coming from - the form below works!

    <html>
    <script language="javascript">
    <!--
    function checkform() {
    <?php $num=3;?>
    <?
    for ($i=0; $i<$num; $i++) {
    ?>
    if (document.poll_form.option_desc<? echo $i; ?>.value=="") {
    alert("Text<? echo $i+1; ?> is empty.");
    document.poll_form.option_desc<? echo $i; ?>.focus();
    return false;
    }
    <?
    }
    ?>
    return true;
    }

    //-->
    </script>
    <body>
    <form method="post" action="#" name="poll_form" onsubmit="return checkform();">
    <?
    for ($i=0; $i<$num; $i++) {
    ?>
    <p>
    <input type="text" name="option_desc<? echo $i; ?>" size="50">
    </p>
    <?
    }
    ?>
    <input type="submit" name="submit">
    </form>
    </body>
    </html>

    The problem was making option_value[] a javascript array - Now it is a PHP one and it works fine!!!!


    Still not sure what I was thinking the first time?

    Simon


    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    Perth West Australia
    Posts
    757
    Rep Power
    15
    I must be on something today ! -

    "The problem was making option_value[] a javascript array - Now it is a PHP one and it works "

    option_value is no longer an array of any sort!

    ------------------
    Simon Wheeler
    FirePages -DHTML/PHP/MySQL
  10. #6
  11. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Hong Kong
    Posts
    71
    Rep Power
    15
    Thank you very much!

Similar Threads

  1. PHP scripts to run in Javascript continuously
    By linhardt in forum JavaScript Development
    Replies: 0
    Last Post: February 13th, 2004, 02:51 PM
  2. javascript inside php tags
    By andy123 in forum JavaScript Development
    Replies: 5
    Last Post: February 5th, 2004, 04:34 PM
  3. Javascript in PHP
    By tintintin in forum JavaScript Development
    Replies: 5
    Last Post: January 28th, 2004, 05:40 AM
  4. php, mysql, and a tiny bit of javascript programmer for hire
    By benthekiller in forum JavaScript Development
    Replies: 0
    Last Post: January 21st, 2004, 01:15 PM
  5. Passing string from PHP to Javascript
    By ka8oad in forum JavaScript Development
    Replies: 4
    Last Post: January 17th, 2004, 03:12 PM

IMN logo majestic logo threadwatch logo seochat tools logo