Process form with both Javascript and PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mouac01@yahoo.com

    Process form with both Javascript and PHP

    I'm new to PHP/Javascript. I have a simple form I want to validate the
    fields with javascript and then run the PHP script. All the scripts
    are in one page. I want PHP to control where the next page is. It
    works fine without the javascript but when I add the javascript the
    page doesn't do anything besides the validation. Thanks for your
    help... Chong

    ----login.html--------------------------------
    <?php
    ob_start();
    session_start() ;
    require('dataso urce.html');
    switch ($_POST['process'])
    {
    case 'Login':
    $_SESSION['login'] = $_POST['login'];
    $sql = "SELECT first_name, last_name, security_access , expire,
    passwd_date FROM sy_user WHERE login = '".$_SESSION['login']."' AND
    password = '".substr(sha1( $_POST['password']), 0, 10)."'";
    $result = mysql_query($sq l);
    if (mysql_num_rows ($result) < 1)
    {
    $msg = "Invalid login and password!";
    }
    else
    {
    $row = mysql_fetch_ass oc($result);
    $_SESSION['first_name'] = $row['first_name'];
    $_SESSION['last_name'] = $row['last_name'];
    $_SESSION['security_acces s'] = $row['security_acces s'];
    $passwd_date = $row['passwd_date'];
    $expire = $row['expire'];

    if ($passwd_date + $expire < date('Y-m-j'))
    {
    header('locatio n: expired.html');
    }
    else
    {
    header('locatio n: main.html');
    }
    }
    }
    ?>
    <SCRIPT LANGUAGE="JavaS cript">
    function checkrequired(f orm)
    {
    for(f=0;f<form. length;f++)
    {
    if(form[f].name.substring (4,length)=="re qd")
    {
    if(form[f].value){continu e}
    else
    {
    alert("Please fill "+form[f].name.substring (5));
    form[f].focus();
    return false;
    }
    }
    }
    return true;
    }
    </script>

    <form method="post">
    Login:<input name="reqd.logi n" type="text"><br >
    Password:<input name="reqd.pass word" type="password" ><br>
    <input type="submit" name="process" value="Login"
    onClick="checkr equired(this)">
    </form>
    <?php echo($msg); ?>
    -------------------------------------------------------

  • d

    #2
    Re: Process form with both Javascript and PHP

    <mouac01@yahoo. com> wrote in message
    news:1139418022 .628334.121000@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > I'm new to PHP/Javascript. I have a simple form I want to validate the
    > fields with javascript and then run the PHP script. All the scripts
    > are in one page. I want PHP to control where the next page is. It
    > works fine without the javascript but when I add the javascript the
    > page doesn't do anything besides the validation. Thanks for your
    > help... Chong
    >
    > ----login.html--------------------------------
    > <?php
    > ob_start();
    > session_start() ;
    > require('dataso urce.html');
    > switch ($_POST['process'])
    > {
    > case 'Login':
    > $_SESSION['login'] = $_POST['login'];
    > $sql = "SELECT first_name, last_name, security_access , expire,
    > passwd_date FROM sy_user WHERE login = '".$_SESSION['login']."' AND
    > password = '".substr(sha1( $_POST['password']), 0, 10)."'";
    > $result = mysql_query($sq l);
    > if (mysql_num_rows ($result) < 1)
    > {
    > $msg = "Invalid login and password!";
    > }
    > else
    > {
    > $row = mysql_fetch_ass oc($result);
    > $_SESSION['first_name'] = $row['first_name'];
    > $_SESSION['last_name'] = $row['last_name'];
    > $_SESSION['security_acces s'] = $row['security_acces s'];
    > $passwd_date = $row['passwd_date'];
    > $expire = $row['expire'];
    >
    > if ($passwd_date + $expire < date('Y-m-j'))
    > {
    > header('locatio n: expired.html');
    > }
    > else
    > {
    > header('locatio n: main.html');
    > }
    > }
    > }
    > ?>
    > <SCRIPT LANGUAGE="JavaS cript">
    > function checkrequired(f orm)
    > {
    > for(f=0;f<form. length;f++)
    > {
    > if(form[f].name.substring (4,length)=="re qd")
    > {
    > if(form[f].value){continu e}
    > else
    > {
    > alert("Please fill "+form[f].name.substring (5));
    > form[f].focus();
    > return false;
    > }
    > }
    > }
    > return true;
    > }
    > </script>
    >
    > <form method="post">
    > Login:<input name="reqd.logi n" type="text"><br >
    > Password:<input name="reqd.pass word" type="password" ><br>
    > <input type="submit" name="process" value="Login"
    > onClick="checkr equired(this)">
    > </form>
    > <?php echo($msg); ?>
    > -------------------------------------------------------[/color]

    If you moved the javascript call from the onclick() event of the submit
    button to the form's onsubmit() event:

    <form method="post" onsubmit="retur n checkrequired(t his);">

    It'll work as you expect :) The problem at the moment is that your
    javascript isn't submitting the form when it's completed. It's returning
    "true" or "false", as a function being called by onsubmit() would be
    expected to do, but it's being used in a different fashion. Try dropping it
    in the <form> tag as I showed above and see if that makes a difference.

    dave


    Comment

    • mouac01@yahoo.com

      #3
      Re: Process form with both Javascript and PHP

      Thanks for the reply, Dave. I think I tried that before and it didn't
      work. I'll try it again. However, what if I have multiple submit
      buttons. The javascript above validates when the Login button is
      clicked. What if I have a Quit button as well that calls a different
      javascript function. I don't see how the onsubmit event can
      distinquish between two different functions when each button is
      clicked. That's why I used the onclick event so I can assign a
      javascript function to each button. I assume PHP doesn't care which
      event I use to call the javascript functions since PHP is server side?
      But just so long as the form is submitted. Thanks.... Chong

      Comment

      • d

        #4
        Re: Process form with both Javascript and PHP

        <mouac01@yahoo. com> wrote in message
        news:1139420865 .358411.160610@ g43g2000cwa.goo glegroups.com.. .[color=blue]
        > Thanks for the reply, Dave. I think I tried that before and it didn't
        > work. I'll try it again. However, what if I have multiple submit
        > buttons. The javascript above validates when the Login button is
        > clicked. What if I have a Quit button as well that calls a different
        > javascript function. I don't see how the onsubmit event can
        > distinquish between two different functions when each button is
        > clicked. That's why I used the onclick event so I can assign a
        > javascript function to each button. I assume PHP doesn't care which
        > event I use to call the javascript functions since PHP is server side?
        > But just so long as the form is submitted. Thanks.... Chong[/color]

        What does the quit button do?

        You can have each button assign a value to a hidden input in your form -
        called, say, "action", then call the submit() method of the form
        (document.getEl ementById("form _id").submit()) . That will definitely be
        picked up by any functions looking at the form after said value has been
        assigned.

        dave


        Comment

        • Jim Michaels

          #5
          Re: Process form with both Javascript and PHP


          <mouac01@yahoo. com> wrote in message
          news:1139418022 .628334.121000@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          > I'm new to PHP/Javascript. I have a simple form I want to validate the
          > fields with javascript and then run the PHP script. All the scripts
          > are in one page. I want PHP to control where the next page is. It
          > works fine without the javascript but when I add the javascript the
          > page doesn't do anything besides the validation. Thanks for your
          > help... Chong
          >
          > ----login.html--------------------------------
          > <?php
          > ob_start();
          > session_start() ;
          > require('dataso urce.html');
          > switch ($_POST['process'])
          > {
          > case 'Login':
          > $_SESSION['login'] = $_POST['login'];
          > $sql = "SELECT first_name, last_name, security_access , expire,
          > passwd_date FROM sy_user WHERE login = '".$_SESSION['login']."' AND
          > password = '".substr(sha1( $_POST['password']), 0, 10)."'";
          > $result = mysql_query($sq l);
          > if (mysql_num_rows ($result) < 1)
          > {
          > $msg = "Invalid login and password!";
          > }
          > else
          > {
          > $row = mysql_fetch_ass oc($result);
          > $_SESSION['first_name'] = $row['first_name'];
          > $_SESSION['last_name'] = $row['last_name'];
          > $_SESSION['security_acces s'] = $row['security_acces s'];
          > $passwd_date = $row['passwd_date'];
          > $expire = $row['expire'];
          >
          > if ($passwd_date + $expire < date('Y-m-j'))
          > {
          > header('locatio n: expired.html');
          > }
          > else
          > {
          > header('locatio n: main.html');
          > }
          > }
          > }[/color]

          move your javascript into the HEAD section where it belongs. weird things
          happen elsewhere.

          [color=blue]
          > ?>
          > <SCRIPT LANGUAGE="JavaS cript">
          > function checkrequired(f orm)
          > {
          > for(f=0;f<form. length;f++)
          > {
          > if(form[f].name.substring (4,length)=="re qd")
          > {
          > if(form[f].value){continu e}
          > else
          > {
          > alert("Please fill "+form[f].name.substring (5));
          > form[f].focus();
          > return false;
          > }
          > }
          > }
          > return true;
          > }
          > </script>
          >
          > <form method="post">
          > Login:<input name="reqd.logi n" type="text"><br >
          > Password:<input name="reqd.pass word" type="password" ><br>
          > <input type="submit" name="process" value="Login"
          > onClick="checkr equired(this)">
          > </form>
          > <?php echo($msg); ?>
          > -------------------------------------------------------
          >[/color]


          Comment

          • Jim Michaels

            #6
            Re: Process form with both Javascript and PHP


            "Rich" <rich@newsguy.c om> wrote in message
            news:dsda3o02ea 6@drn.newsguy.c om...[color=blue]
            > In article <1139418022.628 334.121000@z14g 2000cwz.googleg roups.com>,
            > mouac01@yahoo.c om says...[color=green]
            >>
            >>I'm new to PHP/Javascript. I have a simple form I want to validate the
            >>fields with javascript and then run the PHP script. All the scripts
            >>are in one page. I want PHP to control where the next page is. It
            >>works fine without the javascript but when I add the javascript the
            >>page doesn't do anything besides the validation. Thanks for your
            >>help... Chong
            >>
            >>----login.html--------------------------------
            >><?php
            >>ob_start();
            >>session_start ();
            >>require('data source.html');
            >>switch ($_POST['process'])
            >>{
            >> case 'Login':
            >> $_SESSION['login'] = $_POST['login'];
            >> $sql = "SELECT first_name, last_name, security_access , expire,
            >>passwd_date FROM sy_user WHERE login = '".$_SESSION['login']."' AND
            >>password = '".substr(sha1( $_POST['password']), 0, 10)."'";
            >> $result = mysql_query($sq l);
            >> if (mysql_num_rows ($result) < 1)
            >> {
            >> $msg = "Invalid login and password!";
            >> }
            >> else
            >> {
            >> $row = mysql_fetch_ass oc($result);
            >> $_SESSION['first_name'] = $row['first_name'];
            >> $_SESSION['last_name'] = $row['last_name'];
            >> $_SESSION['security_acces s'] = $row['security_acces s'];
            >> $passwd_date = $row['passwd_date'];
            >> $expire = $row['expire'];
            >>
            >> if ($passwd_date + $expire < date('Y-m-j'))
            >> {
            >> header('locatio n: expired.html');
            >> }
            >> else
            >> {
            >> header('locatio n: main.html');
            >> }
            >> }
            >>}
            >>?>
            >><SCRIPT LANGUAGE="JavaS cript">
            >>function checkrequired(f orm)
            >>{
            >>for(f=0;f<for m.length;f++)
            >>{
            >> if(form[f].name.substring (4,length)=="re qd")
            >> {
            >> if(form[f].value){continu e}
            >> else
            >> {
            >> alert("Please fill "+form[f].name.substring (5));
            >> form[f].focus();
            >> return false;
            >> }
            >> }
            >>}
            >>return true;
            >>}
            >></script>
            >>
            >><form method="post">
            >>Login:<inpu t name="reqd.logi n" type="text"><br >
            >>Password:<inp ut name="reqd.pass word" type="password" ><br>
            >><input type="submit" name="process" value="Login"
            >>onClick="chec krequired(this) ">
            >></form>
            >><?php echo($msg); ?>
            >>-------------------------------------------------------
            >>[/color]
            >
            > May need to add to your <form> tag. Once the Javascript does the form
            > validation, it needs to know where to send the form data. You have the
            > method
            > set to "post" but were missing the action setting. If you are sending the
            > form
            > back to itself you can usually specify that in PHP using...
            >
            > <form action="<?php $PHP_SELF; ?>" method="post">[/color]

            that works if you have register_global s on probably. and it's probably not
            on by default.
            try using <?php echo $_SERVER['PHP_SELF']; ?> instead. it's more likely to
            work.
            [color=blue]
            >
            > You might need to pull the "onClick" event from your submit button and
            > include
            > that in the form tag as well.
            >
            > Rich
            > --
            > Newsguy BonusBytes! - Free monthly rewards
            > http://newsguy.com/bonusbytes.htm
            >[/color]


            Comment

            Working...