Jump to content

Cannot pass variables between html and php


lparsons

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.