Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

My Form does not work. Here's why.

General discussions related to php

Moderators: macek, egami, gesf

My Form does not work. Here's why.

Postby Redcircle » Thu Feb 20, 2003 2:03 pm

I would like to address a problem that I have seen time and time again by newcomers that are posting for answers to why thier form does not post.

register_globals=on is your enemy. Starting with PHP version 4.2 the php.ini is set to off. What this means is that any book or tutorial that has been published before 4.2 was released is probably telling you a big lie on how to handle POST, GET, SYSTEM, SESSION, COOKIES. I know this is the case if you are using the book published by sams titles "PHP and MySQL Web Development" I know. I own the book. It does give you a good overview of the php/mysql language, but some of it is incorrect. There is a blurb in there stating that the way they are teaching you is wrong. They even reference to you that by turning register_globals=off in the php.ini is the correct way. Why they did that I don't know.

The point of my post is to be a possible reference to those wondering why they do not see thier variables after they hit the submit button. I will try and give some examples so that you will overcome your teachings of miss-information.

1. Sessions
Sessions are your friend. They allow you to preserve certain data across subsequent accesses. Which means you can transfer information from page to page. How this works is by using the session_start() function that is built into PHP. What this does is create a unique id that is sent to the clients computer in either the form of a cookie or through the URL. Please note that there has been talk of removing the method to set it from the URL.

If you are somewhat firmilar with sessions and are using session_register() this is not the best way to do it. Not only is there more code involved it also may confuse the living s@!$% out of you. There is a much easier way to do this.

Introduced into PHP 4.1 are Predefined Variables. These are a set of reserved variables that have made working with sessions and other functions as easy as simple arrays.

Using this method to set a session variable all you have to do is use the $_SESSION super global.
Code: Select all
<?php
session_start();
$_SESSION['new_variable'] = 1;
?>


This will create a session variable that will remain untill you use session_destroy(), the user closes the browser, or the time you have specified times out. The information that is set within the $_SESSION[] can be accessed from page to page. This is good when using a shopping cart, restricting access to different pages, or even making sure that your webpage counter doesn't keep increasing when they hit the refresh button.

2. $_POST, $_GET
One of the most common problem that I see is people do not realize that they need to use these when posting a form. Just like $_SESSION this is a super global that is reserved in the PHP language. In a form tag it will have a method that you would like to use to post information if you use a post method your information will be contained in the $_POST variable as an array. If you use a GET method it will be in the $_GET variable once again as an array. Below is an example on how you would use this with a POST method form and a $_SESSION['error'].
Code: Select all
<?php
   session_start();
   if(isset($_POST['submit']))
   {
      if(empty($_POST['post_firstname']) || empty($_POST['post_lastname']) ||
         empty($_POST['post_email']))
      {
         
         $_SESSION['error'] = '<strong>You must enter all information</strong>';
      }
      else
      {
         echo 'First Name: '.$_POST['post_firstname'].'<br>';
         echo 'Last Name: '.$_POST['post_lastname'].'<br>';
         echo 'Email Address: '.$_POST['post_email'].'<br>';
         exit;
      }

   }
   
   if(!empty($_SESSION['error']))
   {
      echo $_SESSION['error'];
      $_SESSION['error'] ='';
   }
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
   First Name: <input type="text" name="post_firstname"
      <?php if(!empty($_POST['post_firstname'])) echo 'value="'.$_POST['post_firstname'].'"';?>><br>
   Last Name: <input type="text" name="post_lastname"
      <?php if(!empty($_POST['post_lastname'])) echo 'value="'.$_POST['post_lastname'].'"';?>><br>
   Email: <input type="text" name="post_email"
      <?php if(!empty($_POST['post_email'])) echo 'value="'.$_POST['post_email'].'"';?>><br>
   <input type="submit" name="submit" value="submit">
</form>


</body>
</html>


That is a simple post form that when submitted it will check to see if there is actually information inputted into the text boxes and error out if any one of them is blank.

Conclusion
I wanted this post to be a little longer. I did not cover $_SERVER, or $_ENV super global but you can follow some of the links I put in there and find some good information. I hope this has helped to shed some light a little. If there are errors in my code please let me know. This post was kinda on the fly so I have not actually tested the code. I may have missed a ; here or there so if the code don't work right out the box look for that. Peace out.

NOTE: if you want to put this as a sticky post please do so more people will see it and might save some repeat posts.
Last edited by Redcircle on Sun Jun 13, 2004 12:12 am, edited 1 time in total.
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby Oleg Butuzov » Fri Feb 21, 2003 12:36 am

GOOD VERY GOOD!
Oleg Butuzov
Last Samuray
Last Samuray
 
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby buzzz » Sun Feb 23, 2003 7:49 pm

Thanks.Nice post.
buzzz
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Feb 21, 2003 11:05 pm

Postby Romantik » Mon Feb 24, 2003 5:52 am

Romantik
New php-forum User
New php-forum User
 
Posts: 67
Joined: Mon Feb 24, 2003 2:55 am
Location: ../Ukraine/Kherson

Postby Osaou » Tue Apr 15, 2003 5:29 pm

Wow, Redcircle, you are my new fan... :D
I just wish those bloody authors of big-title reference books were as deeply involved into their writing material as you are about php... ;)
Great review!
Osaou
New php-forum User
New php-forum User
 
Posts: 7
Joined: Tue Apr 15, 2003 4:52 pm
Location: Stockholm, Sweden

Correction of code above...

Postby Mazerman » Thu May 01, 2003 4:57 am

Thank you for a good explanation of this problem. Tried the code, and corrected some syntax errors, this code would now work:
Code: Select all
<?php
   session_start();
   if(isset($_POST['submit']))
   {
      if(empty($_POST['post_firstname']) || empty($_POST['post_lastname']) ||
         empty($_POST['post_email']))
      {
         
         $_SESSION['error'] = '<strong>You must enter all information</strong>';
      }
      else
      {
         echo 'First Name: '.$_POST['post_firstname'].'<br>';
         echo 'Last Name: '.$_POST['post_lastname'].'<br>';
         echo 'Email Address: '.$_POST['post_email'].'<br>';
         exit;
      }

   }
   
   if(!empty($_SESSION['error']))
   {
      echo $_SESSION['error'];
      $_SESSION['error'] ='';
   }
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
   First Name: <input type="text" name="post_firstname"
      <?php if(!empty($_POST['post_firstname'])) echo 'value="'.$_POST['post_firstname'].'"';?>><br>
   Last Name: <input type="text" name="post_lastname"
      <?php if(!empty($_POST['post_lastname'])) echo 'value="'.$_POST['post_lastname'].'"';?>><br>
   Email: <input type="text" name="post_email"
      <?php if(!empty($_POST['post_email'])) echo 'value="'.$_POST['post_email'].'"';?>><br>
   <input type="submit" name="submit" value="submit">
</form>


</body>
</html>
Mazerman
New php-forum User
New php-forum User
 
Posts: 12
Joined: Sun Jan 26, 2003 2:57 pm
Location: Sweden

can you give a example for php and wml

Postby asvur » Tue Jul 22, 2003 9:54 am

Hi

that is a very good example.
But can you give me a example how to create a login.wml
and validate the login with php and postgresql.
I have tried and searched a lot but i do not know how to send two variables that i get from my login.wml to the php file.
you can have a look at the topic:
PHP WML POSTGRESQL.

thanks
asvur
New php-forum User
New php-forum User
 
Posts: 7
Joined: Mon Jul 21, 2003 9:36 pm

Postby hmiz » Sat Aug 02, 2003 2:04 am

thanks for this :D it's great 8)
hmiz
New php-forum User
New php-forum User
 
Posts: 4
Joined: Fri Aug 01, 2003 5:56 am
Location: london

Postby cesarcesar » Thu Oct 07, 2004 12:35 pm

Great post for sure.

I changed my scripts to work with registered globals off. Things work well on my PHP5 server, but donot on my PHP4.3 server. Any ideas? Both php.ini files are as similiar as possible.

is there any difference between $_SESSION['foo'] and $_SESSION[foo] besides lazy coding?
cesarcesar
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Oct 07, 2004 12:29 pm

Postby swirlee » Thu Oct 07, 2004 1:58 pm

cesarcesar wrote:is there any difference between $_SESSION['foo'] and $_SESSION[foo] besides lazy coding?


$_SESSION[foo] isn't just lazy, it's syntactically incorrect. If you have error_reporting set to report notices, you'll see the Notice.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby cesarcesar » Fri Oct 08, 2004 7:37 am

let me beter ask my question. this is my code, it works fine just am wondering what errors may occur if any in the future. I learned how to code like this in school, but i see that others do it defferently.

if($_SERVER['REQUEST_METHOD']=="GET"){
$_vars = $HTTP_GET_VARS;
}else if($_SERVER['REQUEST_METHOD']=="POST"){
$_vars = $HTTP_POST_VARS;
}

myFunction($_SESSION[SessionID], $_vars[ProductID]);

In the if stament im using quotes when calling the array. In the functin and when calling the arrays other places i usually donot use quotes similiar to $_vars[ProductID]. Like i said everything works fine, but as the last reply it is incorrect. WHY
cesarcesar
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Oct 07, 2004 12:29 pm

Postby swirlee » Fri Oct 08, 2004 9:31 am

You should always use quotes when referring to items in an associative array. If you set error_reporting to E_ALL, you'll see that you are causing errors (notices, actually), you're just not seeing them.

I don't really understand why you'd ever have code like this:

Code: Select all
if($_SERVER['REQUEST_METHOD']=="GET"){
$_vars = $HTTP_GET_VARS;
}else if($_SERVER['REQUEST_METHOD']=="POST"){
$_vars = $HTTP_POST_VARS;
}


If you're coding your application correctly, you'll always know whether data is coming via GET or POST, so you'll know whether to use the $_GET or $_POST superglobal.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby cesarcesar » Fri Oct 08, 2004 9:41 am

i do this for ease of coding for myself. by calling that if() stament on my pages i can easily use the same array name $_vars throught my site. yes i know that i can use POST an GET superglobals, but what i use seems to works just the same.

U are a super PHP guru, I am just a newbie. Am i doing something super wrong by calling my POST or GET vars that way?

thansk for your wisdom.
cesarcesar
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Oct 07, 2004 12:29 pm

Postby swirlee » Fri Oct 08, 2004 9:46 am

You're not doing something "super wrong", but it could be done better.

The reason I advocate using $_POST and $_GET is so that you always know where your variables are coming from. It's just a good habit and gives you a little peace of mind. Another thing to consider is that with your code, if both POST and GET data are submitted in the same request (e.g. if your form has a POST method and and your action looks like "script.php?a=1&b=2"), only the GET data will wind up in your $_vars array.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby cesarcesar » Fri Oct 08, 2004 9:54 am

i see, and will remember your wisdom. currently i have bno get variable in the action of a from. i use hidden input tags within the form to pass my vars. but i will keep in mind. thank you soo much for the wisdom
cesarcesar
New php-forum User
New php-forum User
 
Posts: 8
Joined: Thu Oct 07, 2004 12:29 pm

Postby __OK__ » Mon Apr 11, 2005 1:41 am

swirlee wrote:
cesarcesar wrote:is there any difference between $_SESSION['foo'] and $_SESSION[foo] besides lazy coding?


$_SESSION[foo] isn't just lazy, it's syntactically incorrect. If you have error_reporting set to report notices, you'll see the Notice.

It is incorrect but let me explain why, because for someone new to the language it is not so obvious.

foo (wihtout quotes) is a reference to the constant with the name foo. If you have not defined this constant, it is assumed that its value equals its name, so in this case foo would be converted to "foo." So it will work, but it's ambigous and should be avoided.

I would strongly recommend setting error_reporting to report everything.
__OK__
New php-forum User
New php-forum User
 
Posts: 4
Joined: Wed Jan 12, 2005 10:04 am
Location: Sofia, Bulgaria

Postby Alexej Kubarev » Mon Apr 11, 2005 10:20 am

True... this will let you make clean codes.. or atleast avoid such incorrect usages..

Using code analyzier and debugger will lt you create even better codes... and after you are done with codding: check everything (every single line) and optimize it as much as you can..
User avatar
Alexej Kubarev
Site Admin
Site Admin
 
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län

Re: My Form does not work. Here's why.

Postby HBNUTS » Tue Dec 25, 2007 1:20 am

Real interesting conversation. Thanks for the post.
HBNUTS
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Dec 16, 2007 7:53 pm

Re: My Form does not work. Here's why.

Postby SundayForever » Sat Mar 05, 2011 12:20 am

HBNUTS wrote:Real interesting conversation. Thanks for the post.


Yes, you are right!


Buy WoW Gold
Buy World of Warcraft Gold
Buy Cheap WoW Gold
SundayForever
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sat Mar 05, 2011 12:11 am

Re: My Form does not work. Here's why.

Postby jannypan » Mon Aug 01, 2011 5:45 pm

I just wish those bloody authors of big-title reference books were as deeply involved into their writing material as you are about php... ;) Tera Power Leveling
Tera Items
Tera Items
jannypan
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Aug 01, 2011 5:02 pm

Re: My Form does not work. Here's why.

Postby VARUN006 » Fri Sep 09, 2011 3:02 am

<html>
<head>
<title>
hii
</title>
</head>
<body>
<form action="post2.php" method="post">
<p>First Name:<input type="text"

name="first_name" size="10"></p>
<p>Last Name:<input type="text"

name="name2" size="10"></p>
<p>Email:<input type="text" name="email"

size="15">
<p>Posting:<textarea name="posting"

rows="15" cols="20"></textarea></p>
<input type="submit" name="submit"

value="Send my feedback" />
</body>
</html>



-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?php
$first_name=$_POST['first_name'];
$last_name=$_POST['name2'];
$email=$_POST['email'];
$posting=$_POST['posting'];
$name=$first_name .". $last_name;
print '$posting';
?>


pls help...
VARUN006
New php-forum User
New php-forum User
 
Posts: 21
Joined: Tue Jul 12, 2011 3:51 am

Re: My Form does not work. Here's why.

Postby TheProdigyGuy » Sat Dec 10, 2011 7:56 pm

Great Article & great Thx to Author!
@Varun006
Code: Select all
<?php
error_reporting('OFF');
$html=<<<HTMl
<html>
<head>
<title>
hii
</title>
</head>
<body>
<form action="" method="post">
<p>First Name*:<input type="text"
name="first_name" size="20"></p>
<p>Last Name*:<input type="text"

name="name2" size="20"></p>
<p>Email addr*:<input type="text" name="email"
size="20">
<p>Posting*:<textarea name="posting"
rows="15" cols="40"></textarea></p>
<input type="submit" name="submit"
value="Send my feedback" />
</body>
</html>
HTMl;
echo $html;
if (count($_POST)==0) die;
if (count($_POST)==5 && !empty($_POST['first_name']) && !empty($_POST['name2']) && !empty($_POST['email']) && !empty($_POST['posting']))
{
   
$mailaddr='test@192.168.0.15'; // Your Mail address goes here.
$first_name=$_POST['first_name'];
$last_name=$_POST['name2'];
$email=$_POST['email'];
$posting=$_POST['posting'];
$name=$first_name . ' ' .$last_name;
      @mail($mailaddr,htmlspecialchars($name),wordwrap("~~~~~ Feedback from your site: ~~~~~" . PHP_EOL . htmlspecialchars($name) . " wrote: " . PHP_EOL .
      htmlspecialchars($posting) .
      PHP_EOL .
      htmlspecialchars($name) . "'s email: " . htmlspecialchars($email) . PHP_EOL . "~~~~~~~~~~~~~~~~~~~~~~~~",70));
      die("<script>alert(\"TY for your Feedback.I will contact with you ASAP.)\");</script>");

}
else
{
    die("<script>alert(\"Can't send your Feedback!Try again...\");</script>");
}

?>
TheProdigyGuy
New php-forum User
New php-forum User
 
Posts: 215
Joined: Wed Dec 07, 2011 5:25 pm

Re: My Form does not work. Here's why.

Postby Acesystech » Tue Aug 21, 2012 12:33 am

hi varun :)

plz try this one..

<?php
if(isset($_REQUEST['submit']))
{
$first_name=$_POST['first_name'];
$last_name=$_POST['name2'];
$email=$_POST['email'];
$posting=$_POST['posting'];
$name=$first_name.$last_name;
echo $posting;
}
?>

<!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-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="post2.php" method="post">
<p>First Name:<input type="text" name="first_name" size="10"></p>
<p>Last Name:<input type="text" name="name2" size="10"></p>
<p>Email:<input type="text" name="email" size="15">
<p>Posting:<textarea name="posting" rows="15" cols="20"></textarea></p>
<input type="submit" name="submit" value="Send my feedback" />
</body>
</html>
Acesystech
New php-forum User
New php-forum User
 
Posts: 35
Joined: Mon Aug 20, 2012 10:27 pm
Location: Ernakulam

Re: My Form does not work. Here's why.

Postby PreetiGarg » Mon Dec 31, 2012 12:12 am

Acesystech wrote:hi varun :)

plz try this one..

<?php
if(isset($_REQUEST['submit']))
{
$first_name=$_POST['first_name'];
$last_name=$_POST['name2'];
$email=$_POST['email'];
$posting=$_POST['posting'];
$name=$first_name.$last_name;
echo $posting;
}
?>

<!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-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="post2.php" method="post">
<p>First Name:<input type="text" name="first_name" size="10"></p>
<p>Last Name:<input type="text" name="name2" size="10"></p>
<p>Email:<input type="text" name="email" size="15">
<p>Posting:<textarea name="posting" rows="15" cols="20"></textarea></p>
<input type="submit" name="submit" value="Send my feedback" />
</body>
</html>


Thank you Its really working...
User avatar
PreetiGarg
New php-forum User
New php-forum User
 
Posts: 14
Joined: Fri Dec 28, 2012 2:27 am

Re: My Form does not work. Here's why.

Postby seoitc » Wed Mar 06, 2013 5:57 pm

Thank you for the article . I see there are so many interesting in your article.
seoitc
New php-forum User
New php-forum User
 
Posts: 5
Joined: Wed Dec 12, 2012 11:11 am

Next

Return to PHP General

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.