0

I'm new to web development and have been wrestling with this problem for several hours now, so I've decided to turn to your wisdom. I'm trying to design a little webpage with a database for my wife to store her recipes in, but I'm having trouble getting form submission to work. Here is the code for the webpage where I take the form information in:

<html><body>
Enter the information below to add a new ingredient for use in your recipes.
<form action="add_to_database.php" method="post">
Name: <input name="name" type="text" />
Serving: <input type="text" name="serving" />
Calories: <input type="text" name="calories" />
<input type="submit" />
</form>
</body></html>

And here is some silly code I've been trying to display on the page to see if I can even get the form submission to work:

<html><body>
<?php
$name = $_POST['name'];
echo $name."<br />";
?>
</body></html>

Unfortunately, the page comes back as completely back (after I hit the submit button). What's absolutely baffling to me is that I've copied and pasted the examples from this page into some files and everything seems to work fine. So it seems as though apache and php are working correctly, but I'm messing up somewhere along the way. My apologies in advance if this seems like a stupid question but for the life of me I can't figure it out.

9
  • try echo $_POST['name']; and see what it returns. Commented Jul 22, 2011 at 4:15
  • Do you have an index.php page? Can you post up add_to_database.php. If you have an index.php, doing a simple echo 'test'; at the top will see if php is working. Then you can go from there. Commented Jul 22, 2011 at 4:16
  • When you view source in the browser, do you see the <html><body>? Do you see the PHP source code? Commented Jul 22, 2011 at 4:17
  • advice: don't offer to make a system like this for a wife / husband / partner. It will take way longer than you expected and you'll get totally bored and frustrated and if you ever finish it she won't bother using it. I've been there. Buy her a book. Commented Jul 22, 2011 at 4:25
  • @rackemup420 echo $_POST['name']; returns the same blank page (my apologies for the misspelling above). Commented Jul 22, 2011 at 4:53

3 Answers 3

2

Do you name the other file as add_to_database.php where the form is submitted. Instead you can test on teh same page by removing the add_to_database.php from the form action.

form action="" method="post">
    Name: <input name="name" type="text" />
    Serving: <input type="text" name="serving" />
    Calories: <input type="text" name="calories" />
    <input type="submit" />
</form>

and write the php code on the same page as

$name = $_POST['name'];

echo $name;

If this is not working for you. Create a php file named test.php and type phpinfo(); there.

Sign up to request clarification or add additional context in comments.

Comments

1

Verify that your page is indeed being processed by PHP - obvious question, but does your PHP file have a .php extension? Do other things like phpinfo() or echo "Test"; work?

Check the error log in /var/log/apache2/error.log or similar (if on Linux, dunno where that'd be on Windows).

Try turning display_errors on in the PHP configuration (this is a good idea only for a development install, not a production server).

Comments

0

a bit of a longshot, but if you can verify that php is pro essing the page. Clean up your html, you are missing the dtd, head, and encoding. . hard to say how a browser is going to interpret a form (missing a name attribute) without these.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.