|
Posted by Jerry Stuckle on 11/17/06 22:41
Jerim79 wrote:
> Jerim79 wrote:
>
>>e_matt...@hotmail.com wrote:
>>
>>>>Here is the basic gist of what I have:
>>>>
>>>><?php
>>>>$FName=$_Post['FName'];
>>>>
>>>>if ($FName !=""){
>>>>header("Location: write_to_database.php");
>>>>}
>>>>else{
>>>> if ($FName=""){
>>>> $errormsg1="Please enter a first name":
>>>>}
>>>>?>
>>>>
>>>><html>
>>>><body>
>>>>Registration
>>>><?php if ($errormsg1 !="") echo $errormsg1); ?>
>>>><br />
>>>><form action="(this page)"
>>
>>method="Post">
>>
>>>>First Name: <input type="text" name="FName">
>>>><br />
>>>><input type="submit" name="submit value="Submit">
>>>></form>
>>>></body>
>>>></html>
>>>>
>>>>Forgetting the problem about loading the errors up before filling out
>>>>the form, I can't get this to work at all. After I click on submit it
>>>>just brings me back to the same page, with no error messages.
>>>
>>>Try:
>>><?php
>>>
>>> $FName = "";
>>> $errorMsg1 = "";
>>>
>>> if ($_POST) {
>>>
>>> if ( isset($_POST['FName']) ) {
>>> $FName = $_POST['FName'];
>>> }
>>>
>>> Now you can validate $FName, because it's either empty or set to
>>>user's value. Don't write to database without validating all data.
>>>The rest looks good. Good luck.
>>
>>Since I am still learning, I just wanted state the logic of the code.
>>First we set $FName equal to nothing. Then we set $errorMsg1 equal to
>>nothing. Next we say "if the method is $_POST" then check to see if
>>$_POST['FName'] has been set. If it has, then set $FName equal to
>>$_POST['FName']
>>
>>I still can't get it to display the error message.
>
>
> Here is the current, non-working code:
>
> <?php
> $FName="";
> $errormsg1="";
>
> if( isset($_POST['FName']){
> $FName=$_POST['FName'];
> }
>
> if ($FName !=""){
> header("Location: write_to_database.php");
> }
> else{
> if ($FName=""){
> $errormsg1="Please enter a first name":
> }
> ?>
> <html>
> <body>
> Registration
> <?php if ($errormsg1 !=""){ echo $errormsg1; } ?>
> <form action="new.htm" method="POST">
> First Name: <input type="text" name="FName">
> <br />
> <input type="submit" name="submit" value="Submit">
> </form>
> </body>
> </html>
>
I do things a little differently, but basically the same thing. This
should work. Note that the <?php MUST be the first thing in you file -
no whitespace or anything before it.
<?php
$errormsg1="";
$FName = "";
if (isset($_POST['submit'] && $_POST['submit'] == 'Submit') {
// Repeat the following for each field you wish to validate
if (isset($_POST['FName']))
$FName = trim($_POST['FName'];
if ($FName == "")
$errormsg1 = "Please enter a first name<br>\n";
// End of repeated code
if ($errormsg1 == "")
header("Location: write_to_database.php");
}
?>
<html>
<body>
Registration
<?php if ($errormsg1 !=""){ echo $errormsg1; } ?>
<form action="new.htm" method="POST">
First Name: <input type="text" name="FName" value="<?$FName?>">
<br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
The first if checks to see if the submit button was pressed.
The next if checks to see if FName was posted to the page. If so, it
trims white space before and after (in case the user entered a blank
space) and sets it into $FName.
The next if checks to see if FName is an empty string. If so, it sets
errormsg1.
You can repeat these statements to validate other fields.
Now - if errormsg1 is empty, it will call your write_to_database page
(more problems here - in a minute).
If there was an error, or the submit button was not pressed, it will
fall through to the rest of your code.
Finally, I added code to your input field to echo $FName. This will
fill in the field if your user has entered a first name but may be
missing other data. Otherwise your form will come up blank and your
user will have to reenter everything.
Now - the problem with your code. When you call the header() function
to transfer control, the data in $_POST is NOT sent with it. You will
lose all of the data that was posted to your page. Rather than
transferring control, you should write to the database here in this
page, i.e.
if ($errormsg1 == "") {
// code to connect to and write to your database
header('Location: thankyou.html');
}
Where 'thankyou.html' is a page that gives them a message thanking them
for their input. At least I'm assuming you don't wish to return to this
form. If so, you can leave out the header call. And if you don't want
the form to show the $FName again, just set it to "" again at this
location.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|