LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-05-2007, 10:17 AM   #1
ashesh0326
Member
 
Registered: Jan 2007
Distribution: Suse 10.0, FC 6
Posts: 93

Rep: Reputation: 15
Question PHP having issues while accepting variables from forms


Well, I just had another question.
It seems like I'm having trouble passing variables from forms to my php scripts.
For example, this is an extremely rudimentary script and yet it refuses to work in my case. No matter how many things I try, it's giving me the same, 'form data incomplete message'

First, we have an html form that looks like this:
Quote:
<html>
<form action=mypage.php method=post>
<input type = "text" name=email>
<input type=text name=first_name>
<input type=submit name=submit value="yes">
</form>
</html>
And mypage.php is:
Quote:
<?php
if (isset($submit) && $submit=="yes")
{
echo "Thank you for submitting your form.";
}
else {
?>
<h3>form data incomplete</h3>
<form action=mypage.php method = post>
<input type=text name = email>
<input type=text name=first_name>
<input type=submit name=submit value=yes>
</form>
<?php
}
?>
And it refuses to work. I had the same problem with a more complex form, and I had decided that I'd resolve the issue later.
As it seems now, I don't think I have any idea as to what's wrong with what.

Can anyone please help? Thanks in advance.

This is what I got when I checked apache's error_log:
Quote:
[Sun May 06 19:33:15 2007] [error] [client ::1] PHP Notice: Undefined variable: first_name in /var/www/work/mypage.php on line 3, referer: http://ashesh.homeip.net/work/form.html
I also changed the form method to GET and checked the variable names and everything seems ok. Why is it then that PHP is having issues with recognizing the variables? Help please!
 
Old 05-05-2007, 11:34 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
you are being very inconsistent about quoting your variables in html. Try fixing that.

Also in the code your provide, there is no variable first_name on line 3. Line 3 is { and nothing else. In fact, there is no code anyplace you provided that references first_name, other than to define it for a form.

Hence, your samples are useless to anyone here for debugging.

Also, why do this: if (isset($submit) && $submit=="yes") ?

If $submit == "yes" then clearly isset($submit) will be true. If $submit <> "yes" then the whole thing is false anyway. Just ask if( $submit=="yes").
 
Old 05-05-2007, 05:50 PM   #3
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
You are not using the $_POST var. Your mypage.php doesn't know what var to work with.

html:

[HTML]
<html>
<form action=mypage.php method=post>
<input type = "text" name=email>
<input type=text name=first_name>
<input type=submit name=submit value="yes">
</form>
</html>
[/HTML]

is fine, but you should label your input fields

mypage.php:

PHP Code:
<?php
$submit
=$_POST['submit'];
if (
$submit=="yes")
{
echo 
"Thank you for submitting your form.";
}
else {
?>
<html>
<form action=mypage.php method=post>
<input type = "text" name=email>
<input type=text name=first_name>
<input type=submit name=submit value="yes">
</form>
</html>
<?php
}
?>
It may be better to just use the php page. Since submit has not been set, it will only display the form. Once it is re-loaded and set, it will only display the "thank you" part.

Something like this all on "mypage.php. Try and see if you like it:

PHP Code:
<?php
// Set the vars from our incoming POST data
$first_name=$_POST['first_name'];
$email=$_POST['email'];
$submit=$_POST['submit'];

// Check if submit has the value "yes"
if ($submit=="yes") {
    echo 
"Thank you for submitting your form.<br>";
    echo 
"you have entered a first name of \"$first_name\" and an email of \"$email\"<br>";
} else {
?>

<html>
<form action=mypage.php method=post>
First Name:<input type = "text" name="first_name"><br>
Email:<input type=text name="email"><br>
<input type=submit name=submit value="yes">
</form>
</html>

<?php
}
?>

Last edited by jaykup; 05-05-2007 at 06:02 PM.
 
Old 05-05-2007, 08:50 PM   #4
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Quote:
Originally Posted by ashesh0326
Why is it then that PHP is having issues with recognizing the variables? Help please!
You probably have register_globals off. PHP will only create individual variables for each form element if the register_globals setting in turned on in your php.ini file. However, as of PHP 4.2 register_globals was turned off by default because it can pose security problems. As jaykup suggested, you should use $_POST and $_GET instead.
 
Old 05-07-2007, 08:01 AM   #5
ashesh0326
Member
 
Registered: Jan 2007
Distribution: Suse 10.0, FC 6
Posts: 93

Original Poster
Rep: Reputation: 15
Thank you so much guys. The problem has been solved now. register_globals was indeed off, and switching it on solved the problem once and for all.


Thanks again!
 
  


Reply

Tags
form, forms, html, php, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Html Forms and PHP LXer Syndicated Linux News 0 01-09-2007 02:33 AM
MySQL results to html forms using PHP xemous Programming 3 08-15-2005 04:27 PM
Best mailer to use for a website with PHP email forms gbkyle Linux - Software 1 07-07-2005 05:00 AM
passing an array from html-forms to php prabhatsoni Linux - Software 2 06-04-2005 01:11 AM
php problem accepting variables rootyard Programming 3 02-11-2004 08:35 AM


All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration