lparsons Posted July 18, 2008 Share Posted July 18, 2008 Hello I am working with an Ubuntu system that I just set up with Apache2, MySQL, and PHP5. I would like to set up a form that allows me to enter data into a small MySQL database. I have been stuck with problems getting php pages to take variables from html forms. I've reduced my form to just two fields to try to simplify testing: The form is "example.html": <html> <body> <form action=mypage.php action=post> <input type=text name=email> <input type=text name=first_name> <input type=submit name=submit value=yes> </form> </body> </html> And then the mypage.php is as follows: <?php echo "submit was"; $stuff = $_POST['submit']; //echo _$POST[submit]; echo "$stuff"; echo " or maybe"; echo $submit; echo "<br>"; echo "email was"; $email = $_POST['email']; //echo "_$POST[email]"; echo "$email"; echo "<br>"; if (isset($submit) && $submit="yes"){ echo "thank you for submitting"; }else{ ?> <form action=mypage.php action=post> <input type=text name=email> <input type=text name=first_name> <input type=submit name=submit value=yes> </form> <?php } ?> None of those attempts in mypage.php give any result from the variables for "submit" or "email". I turned on "register_globals" in /var/www/.htaccess to try to help the situation, and that also made no difference. Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/ Share on other sites More sharing options...
jesushax Posted July 18, 2008 Share Posted July 18, 2008 Use the form field names to get the values $stuff1 = $_POST['email']; $stuff2 = $_POST[first_name']; Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593443 Share on other sites More sharing options...
LooieENG Posted July 18, 2008 Share Posted July 18, 2008 You should put "" in the HTML <form action="mypage.php" action="post"> <input type="text" name="email"> <input type="text" name="first_name"> <input type="submit" name="submit" value="yes"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593444 Share on other sites More sharing options...
KevinM1 Posted July 18, 2008 Share Posted July 18, 2008 This may or may not be the issue, but put your form attributes (i.e. action) within quotes (action="mypage.php"). Also, register_globals doesn't do what you think it does, and can be dangerous to have on from a security standpoint. It's best to leave it off. Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593446 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 To confirm form data being sent, put this at the top of your PHP page: print_r($_POST); Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593447 Share on other sites More sharing options...
JonnyThunder Posted July 18, 2008 Share Posted July 18, 2008 Working example.... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Test</title> </head> <body> <?php if (isset($_POST['submit'])) { print "Email: {$_POST['email']}<br />\n"; print "First name: {$_POST['first_name']}<br />\n"; } else { ?> <form action="" method="post" name="testform"> <input name="email" type="text" /> <input name="first_name" type="text" /> <input name="submit" type="submit" value="Yes" /> </form> <?php } ?> </body> </html> Will work if your PHP is working correctly. :-) Tested on Vertrigo (PHP 5) Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593454 Share on other sites More sharing options...
lparsons Posted July 18, 2008 Author Share Posted July 18, 2008 Will work if your PHP is working correctly. :-) Tested on Vertrigo (PHP 5) That is the only solution I have seen so far that works. Now I just need to figure out why. Even more so, I'd love to know why nothing else works. I guess it wouldn't kill me to skip html-only forms altogether and just do everything in php. To confirm form data being sent' date=' put this at the top of your PHP page:[/quote'] The result was "Array()". So I guess it confirmed what I suspected - nothing was passed from my html form to my php script. I went back and turned globals back off, as well. You should put "" in the HTML I went through and added quotes for all of those fields in the html. It unfortunately made no difference. Use the form field names to get the values I think I already tried that, in the code I submitted. Please correct me if I'm missing something. Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593491 Share on other sites More sharing options...
LooieENG Posted July 18, 2008 Share Posted July 18, 2008 if (isset($submit) && $submit="yes"){ <?php echo "submit was"; $stuff = $_POST['submit']; //echo _$POST[submit]; echo "$stuff"; echo " or maybe"; echo $submit; $submit was never defined Also, it should be if ($submit == 'yes') 2 equal signs Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593492 Share on other sites More sharing options...
unkwntech Posted July 18, 2008 Share Posted July 18, 2008 As JonnyThunder hinted at I think there is probably something wrong with the php installation. Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593493 Share on other sites More sharing options...
JonnyThunder Posted July 18, 2008 Share Posted July 18, 2008 Try doing it with the two files again and see... test.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Test</title> </head> <body> <form action="test.php" method="post" name="testform"> <input name="email" type="text" /> <input name="first_name" type="text" /> <input name="submit" type="submit" value="Yes" /> </form> </body> </html> And test.php <?php if (isset($_POST['submit'])) { print "Email: {$_POST['email']}<br />\n"; print "First name: {$_POST['first_name']}<br />\n"; } else { require('test.html'); } ?> If that works, then you'll know it was badly formatted HTML and not a PHP issue. Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593501 Share on other sites More sharing options...
kenrbnsn Posted July 18, 2008 Share Posted July 18, 2008 The problem is in this line: <form action="mypage.php" action="post"> it should be <form action="mypage.php" method="post"> Not having a "method" attribute in the form tag, made the method "get". Ken Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593507 Share on other sites More sharing options...
JonnyThunder Posted July 18, 2008 Share Posted July 18, 2008 Well spotted chief. I completely missed that one! Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593509 Share on other sites More sharing options...
kenrbnsn Posted July 18, 2008 Share Posted July 18, 2008 I missed it to when I first looked at the code. I finally saw it when I copied the OP's code to my machine and tried it. Ken Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593540 Share on other sites More sharing options...
lparsons Posted July 18, 2008 Author Share Posted July 18, 2008 If that works' date=' then you'll know it was badly formatted HTML and not a PHP issue.[/quote'] Indeed, it appears my HTML is garbage. I'll continue working on straightening that out. What is the purpose of the "/" at the end of the lines in your code? For example: <input name="submit" type="submit" value="Yes" /> Which seems to have made a difference, but I'm not sure why! Not having a "method" attribute in the form tag' date=' made the method "get".[/quote'] Thanks for spotting that. My typo probably would have made my life difficult for some time, I suspect... Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593601 Share on other sites More sharing options...
LooieENG Posted July 18, 2008 Share Posted July 18, 2008 Valid XHTML requires that all tags are closed. i.e. <br> => <br /> <img src="image.jpg" alt="image" /> Quote Link to comment https://forums.phpfreaks.com/topic/115440-cannot-pass-variables-between-html-and-php/#findComment-593649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.