form processing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

form processing

Post 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>
Last edited by pritam79 on Sat Jul 12, 2008 2:38 am, edited 1 time in total.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: form processing

Post 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?)
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: form processing

Post by pritam79 »

Thanks apollo, i am getting the desired result now. Will get back for further queries.
pritam79
Forum Commoner
Posts: 65
Joined: Wed Mar 26, 2008 9:28 am

Re: form processing

Post 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";
?>
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: form processing

Post by Stryks »

Is this thread any help?
Post Reply