Page 1 of 1

form processing

Posted: Sat Jul 12, 2008 2:25 am
by pritam79
Hi everyone, I have a registration script process_form.php which
processes the user inputs of the form in main.htm. But i run
this script and click on the submit button, i get an error which
says- "Invalid argument supplied for foreach() in
C:\wamp\www\test\process_form.php

Code: Select all

************** process_form.php ********************
 
<?php
if(!filled_out($HTTP_POST_VARS))
  {
   echo "You have not filled the form out correctly - pleas";
  exit;
  }
function filled_out($form_vars)
  {
   // test that each variable has a value
   foreach ($form_vars as $key => $value)
    {
     if(!isset($key) || ($value == NULL))
      return false;
    }
   // return true;
  }
?>
 
************************************************************
 
************************ main.htm **************************
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; 
 
charset=utf-8" />
<title>Email</title>
</head>
<body>
<table style="width: 100%">
    <tr>
        <td style="width: 322px" valign="top">Email</td>
        <td rowspan="4" valign="top">
        <form method="post" action="process_form.php">
        <input type="text" name="email"/><br/><br />
        <input type="text" name="user"/><br /><br />
        <input type="password" name="pass"/><br /><br />
        <input type="password" name="pass2"/><br /><br 
 
/>
        <input type="submit" value="submit"/></form>
        </td>
    </tr>
    <tr>
        <td style="width: 322px; height: 20px;" 
 
valign="top">username</td>
    </tr>
    <tr>
        <td style="width: 322px; height: 13px;" 
 
valign="top">password</td>
    </tr>
    <tr>
        <td style="width: 322px" 
 
valign="top">confirm</td>
    </tr>
</table>
</body>
</html>

Re: form processing

Posted: Sat Jul 12, 2008 2:36 am
by Apollo
Try using $_POST instead of $HTTP_POST_VARS, the latter is deprecated and may not work.

If that doesn't help, try var_dump($form_vars) in your filled_out function.

By the way, in that foreach loop, how could !isset($key) ever be true?
(Perhaps you intend to check for missing fields, i.e. !isset($form_vars['email']) etc?)

Re: form processing

Posted: Sat Jul 12, 2008 6:52 am
by pritam79
Thanks apollo, i am getting the desired result now. Will get back for further queries.

Re: form processing

Posted: Sun Jul 13, 2008 1:26 am
by pritam79
Hi apollo, I have an input form "register.php" which the user uses to register. Its action field is " do_register.php". I have included javascript alert functions in " do_register.php" to check the user inputs. When I provide wrong inputs and click on 'submit', I get the correct error message in alert boxes but i am directed to a different page whereas i want to stay on the "register.php" so that the user can fill the form again before being directed to the "members.php" page. Please tell me how to do this.

This is a part from "do_register.php"

Code: Select all

if (!valid_email($email))
  {
    print("<script language = 'javascript'>alert('Not a valid email address');</script>");
    exit();
  }
  // passwords not same
   if($passwd != $passwd2)
   {
    print("<script language = 'javascript'>alert('Passwords donot match');</script>");
    exit();
   }
   
// check password length is ok
// ok if username truncates, but passwords will get munged if they are too long.
 
if (strlen($passwd)<6 || strlen($passwd) >16)
   {
    print("<script language = 'javascript'>alert('Password must be between 6 to 16 characters');</script>");
    exit();
   }
And this is the "register.php" page.

Code: Select all

 
<?php
include "header1.php";
?>
<div id="content">
   <div id="left" style="left: 0px; top: 0px">
    <ul>
      <li>Email address :</li>
      <li>Preferred username :</li>
      <li>Password :</li>
      <li>Confirm password :</li>
    </ul>
   </div>
   <div id="right">
    <p style="left: -1px; top: 0px"><font size="4.5px">REGISTER</font></p>
    <form action="do_register.php">
    <input type="text" size="20" name="email"><br><br>
    <input type="text" size="20" name="username"> (max 16 chars)<br><br>
    <input type="password" size="20" name="passwd"> (6 to 16 chars)<br><br>
    <input type="password" size="20" name="passwd2"><br><br>
    <input type="submit" name="Submit" value="submit">
    <input type="reset" name="Reset" value="reset">
    </form>
    </div>
</div>
<?php
include "footer.php";
?>

Re: form processing

Posted: Sun Jul 13, 2008 2:30 am
by Stryks
Is this thread any help?