Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To 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:38 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

<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 22nd, 2005, 05:41 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

When using the POST method you must use the $_POST superglobal to access the information.

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

HTH!


Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old November 22nd, 2005, 06:15 PM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the quick reply. I'm still doing something wrong. I'm getting the following error message

Notice: Undefined index: firstname

--------------
HTML is (I was missing the ending " on firstname)

<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>

-----------
revised php is

<?php


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

?>



 
Old November 22nd, 2005, 06:19 PM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Never mind it works. I hadn't refreshed the html page.

Thanks

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

First error from the original form. You are missing a (") double apostrophe after Name="firstname
Then on your kform1.php,
Try This:
<?php
// first this will check if submit button was click from the
// HTML form
if (!array_key_exists('submit', $_POST))
{
    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">

_HERE;
}
elseif(empty($_POST['firstname']) // This will give an error message
{
    print <<<_HERE

<FORM METHOD="POST" ACTION="kform1.php">
<P> First Name: <INPUT TYPE="text" Name ="firstname" SIZE="20"><br />
Name is required to continue...
<P><P>

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

_HERE;
}
else
{

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

}
?>

It takes practice to learn your syntax.
John M.





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 acko ASP.NET 1.x and 2.0 Application Design 1 December 23rd, 2003 09:40 AM
Passing variables jesseleon Access VBA 4 October 24th, 2003 01:45 PM





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