using forms, need to reuse values after POST and getting undefined variable erro

I need my form to reuse the input values if the values doesn’t pass my constraints/checks after post but the values should be cleared upon post if all constraints/checks are met. How do I do this?

I’m also getting Undefined variable error above each input field.
What am I doing wrong?

[php]

<?php if (isset($_POST['name'])) {$name = $_POST['name']; } if (isset($_POST['email'])) {$email = $_POST['email']; } if (isset($_POST['subject'])) {$subject = $_POST['subject']; } if (isset($_POST['enquiry'])) {$enquiry = $_POST['enquiry']; } if (isset($_POST['captcha'])) {$captcha = $_POST['captcha']; } echo ""; echo "

Your Name
"; echo "

"; echo "

Email Address
"; echo "

"; echo "

Subject
"; echo "

"; echo "

Enquiry
"; echo "$enquiry

"; echo "

Captcha
"; echo "

\"CAPTCHA\"

"; echo "refresh"; ?>

[/php]

First thing to work on doing, is to separate html from the php. That is part of your issue.

Your undefined warnings are due to using variables that don’t exists. Try something more like this:

[php]$name = isset( $_POST[‘name’] ) ? $_POST[‘name’] : ‘’;[/php]

That will give default values to all of you variables.

It also looks like you are trying to validate base on javascript as well?

Why in the world are you echoing HTML?

Probably because a lot of tutorials and “educational” classes/courses/whatever tell you to. Which are also often the reasons for other horrendous acts done to push the name of programming language X down in the mud :smiley:

Note only that, but I seen posters also promote doing that. I remember this poster on a different forum even flaming and calling people who disagree with it. The only good thing is he was eventually banned. :smiley:

I know there at times I intermingle PHP and HTML, but I’m always wondering if there’s a way I can do it without doing that. Most of the time I just leave it be for it’s working and like the old saying if it ain’t broken don’t fix it. However, with the OP’s script that is just leading to major problems and if not it isn’t very portable, by that I mean it can’t be easily transferred to another project. Just my .02 cents.

If you have any code you want reviewed / discussed, please post it (in a separate thread). I guess it depends on how you “intermingle” them though.

For those not in the know, Php has what is called “Alternative syntax for control structures” which makes for a really clean way to “escape” Php in your controls.

http://php.net/manual/en/control-structures.alternative-syntax.php

Sponsor our Newsletter | Privacy Policy | Terms of Service