You are here: Re: Process form with both Javascript and PHP « PHP Programming Language « IT news, forums, messages
Re: Process form with both Javascript and PHP

Posted by d on 09/30/22 11:39

<mouac01@yahoo.com> wrote in message
news:1139418022.628334.121000@z14g2000cwz.googlegroups.com...
> 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('datasource.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($sql);
> if (mysql_num_rows($result) < 1)
> {
> $msg = "Invalid login and password!";
> }
> else
> {
> $row = mysql_fetch_assoc($result);
> $_SESSION['first_name'] = $row['first_name'];
> $_SESSION['last_name'] = $row['last_name'];
> $_SESSION['security_access'] = $row['security_access'];
> $passwd_date = $row['passwd_date'];
> $expire = $row['expire'];
>
> if ($passwd_date + $expire < date('Y-m-j'))
> {
> header('location: expired.html');
> }
> else
> {
> header('location: main.html');
> }
> }
> }
> ?>
> <SCRIPT LANGUAGE="JavaScript">
> function checkrequired(form)
> {
> for(f=0;f<form.length;f++)
> {
> if(form[f].name.substring(4,length)=="reqd")
> {
> if(form[f].value){continue}
> else
> {
> alert("Please fill "+form[f].name.substring(5));
> form[f].focus();
> return false;
> }
> }
> }
> return true;
> }
> </script>
>
> <form method="post">
> Login:<input name="reqd.login" type="text"><br>
> Password:<input name="reqd.password" type="password"><br>
> <input type="submit" name="process" value="Login"
> onClick="checkrequired(this)">
> </form>
> <?php echo($msg); ?>
> -------------------------------------------------------

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

<form method="post" onsubmit="return checkrequired(this);">

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация