Wrox Programmer Forums
|
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 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 April 11th, 2013, 06:12 PM
Registered User
 
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?
 
Old April 17th, 2013, 05:31 AM
Friend of Wrox
 
Join Date: May 2011
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")
 
Old April 18th, 2013, 03:50 AM
Registered User
 
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">
 
Old April 18th, 2013, 08:48 AM
Friend of Wrox
 
Join Date: May 2011
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..





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





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