Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 22nd, 2005, 05:42 PM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing variables

I can't get the variable to pass to php in the following

-----------------------
in kform.htm I have (this form works ok)

<HTML>
<HEAD>
First Form
<?HEAD>
<BODY>

<FORM METHOD="POST" ACTION="kform1.php">
<P> First Name: <INPUT TYPE = "text" Name = "firstname SIZE = "20">
<P><P>

<INPUT TYPE="submit" VALUE="Send info">
<INPUT TYPE="reset">

</FORM>
</BODY>
</HTML>
------------------------------
in kform1.php , I have

<?php

$firstname = $_GET['firstname'];

echo "Hi, your first name is: $firstname" ;

?>

---------------------------
When I run this I get

Notice: Undefined index: firstname in C:\Program Files\BadBlue\PE\kform1.php on line 4
Hi, your first name is:


 
Old November 23rd, 2005, 06:52 AM
Authorized User
 
Join Date: Oct 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

If your method of sending the form is POST, the way to access the variable is $_POST['firstname'] not $_GET['firstname']

try:

<?php

$firstname = $_POST['firstname'];

echo "Hi, your first name is: $firstname" ;

?>

Or you could argue why even create another variable ($firstname) use:

<?php

echo "Hi, your first name is: $_POST[firstname]" ;

?>
>>>>note the missing '' around $firstname because the variable is encased in double quotes

HTH

David
 
Old December 2nd, 2005, 11:25 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 1 Time in 1 Post
Default

Try this:
a) on form kform.htm add an attribute NAME="submit" after input TYPE="submit" have a space in between.

b) on <INPUT TYPE="text" add the " after firstname:
NAME="firstname" SIZE="20">

c) on form kform1.php:


<?php
  $firstname = $_POST['firstname'];
  // This means if submit button is not click.
  if( !array_key_exits('submit', $_POST) || empty($firstname) )
  {
    print <<<_HERE;

    <FORM METHOD="POST" ACTION="kform1.php">
      <P>First Name: <INPUT TYPE="text" NAME="firstname" SIZE="20">
      </P> <P />
      <INPUT TYPE="submit" NAME="submit" VALUE="Send info">
      <INPUT TYPE="reset">
    </FORM>
_HERE;
  }
  else
  {
    echo "Hi, your first name is: $firstname";
  }
?>

Note: the word _HERE; should be at the leftmost side of your code to work.
Hope this helps.
John M.

 
Old December 9th, 2005, 04:37 PM
Registered User
 
Join Date: Dec 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, use the array $_POST instead of $_GET, so it's $_POST[firstname] instead of $_GET[firstname]






Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Variables silve1999 Pro Java 1 June 15th, 2006 12:54 PM
Passing Variables CMOS Classic ASP Basics 2 January 22nd, 2006 07:38 PM
Passing variables karlirvin PHP How-To 4 December 2nd, 2005 08:02 PM
Passing variables acko ASP.NET 1.x and 2.0 Application Design 1 December 23rd, 2003 09:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.