Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: PHP and MySQL 24-Hour Trainer
This is the forum to discuss the Wrox book PHP and MySQL 24-Hour Trainer by Andrea Tarr ; ISBN: 978-1-1180-6688-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: PHP and MySQL 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 11th, 2013, 06:12 PM
Registered User
Points: 26, Level: 1
Points: 26, Level: 1 Points: 26, Level: 1 Points: 26, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 11

I've created the form but can't find the function "contactForm" as referenced in the following code:

Code:
	<input type="submit" value="Submit" name="contactForm" />
The coding

Code:
	<?php 
	if($_GET['contactForm'] == "Submit") {
		echo 'Thank you for submitting the form';
	}
	?>
produces the error:
"Notice: Undefined index: contactForm in C:\Users\chuck\Desktop\Documents\php\xampp\htdocs\ php24\lesson11.php on line 18"

when I click on the 'submit' button because 'contactForm' is not found.

Where is 'contactForm'? or am I doing something wrong?
Reply With Quote
  #2 (permalink)  
Old April 17th, 2013, 05:31 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

The error is due to the GET array not having an index of 'contactForm'. And if you are using a form then the usual method of getting data from a form is via the POST array.

Try changing the 'if' line to the following;
Code:
if( isset($_POST['contactForm']) && $_POST['contactForm'] == "Submit")
Reply With Quote
  #3 (permalink)  
Old April 18th, 2013, 03:50 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

this error due to reason wrong method u have to used POST method and action.


<form name="Control......" method="post" action="URL">
Reply With Quote
  #4 (permalink)  
Old April 18th, 2013, 08:48 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Ok I think the above is a little confusing... so.

$_GET and $_POST are used to obtain information passed to a webpage.

How this information is passed determines which one you use. So in your example you're using a form so you would use the $_POST array and then set the method to post for your form like so;
Code:
<form name="my_form" method="post" action=" ... ">
where the 'action' is the URL of the file that will handle the data. You could also use 'get' as the form method but it's normally set to 'post'.

Copy the following and save it as 'test_form.php' where you can run it from via a browser;
Code:
<?php

if($_POST['colour'] == 'red')
{
	print '<p style = "color:red;">Red is your favourite color</p>';
}
elseif($_POST['colour']== 'yellow')
{
	print '<p style = "color:yellow;">Yellow is your favourite color</p>';
}
elseif($_POST['colour'] =='green')
{
	print '<p style = "color:green;">Green is your favourite color</p>';
}
elseif($_POST['colour'] == 'blue')
{
	print '<p style = "color:blue;">Blue is your favourite color</p>';
}
else
{
	print '<p class = "error">Select your favourite color</p>';
}
if( isset($_POST['submit']) )
{
	echo '<pre>';
	print_r($_POST);
	echo '</pre>';
}

echo '<form method="post" action="test_form.php">';
echo '<select name="colour" size="1">';
echo '<option value="0">Select Colour</option>';
echo '<option value="red">Red</option>';
echo '<option value="yellow">Yellow</option>';
echo '<option value="green">Green</option>';
echo '<option value="blue">Blue</option>';
echo '</select>';
echo '<input type="submit" name="submit" value="submit" />';
echo '</form>';

?>
when ran for the first time you'll get 4 notices about an undefined index 'colour' which is because as the form has not yet been submitted the index 'colour' does not exist in the $_POST array.

Select a colour from the list and click Submit you'll then be shown a message and also the contents, data passed from form, of the $_POST array.

$_GET is normally used to obtain data passed via a URL such as;

http://localhost/test.php?key=value&colour=red

in the above example you'd use something like the following to obtain the passed values;

$key = ( isset($_GET['key']) ? $_GET['key'] : '');
$colour = ( isset($_GET['colour']) ? $_GET['colour'] : '');
$food = ( isset($_GET['food']) ? $_GET['food'] : '');

$key would then contain 'value' and $colour would contain 'red' and $food would be an empty string as it's not in the URL.

Last edited by UseLess; April 18th, 2013 at 08:51 AM..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 11 figure 11-7 relative positioning pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 November 29th, 2007 06:11 AM
Chapter 11: kiley-s BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 10 March 8th, 2007 08:19 PM
Chapter 11 zaidqais Visual Basic 2005 Basics 0 June 2nd, 2006 04:09 AM
Chapter 11 kappa3 Wrox Book Feedback 0 October 8th, 2003 11:21 AM



All times are GMT -4. The time now is 09:34 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.