...

View Full Version : Form Displays Echo before submitted



XiledWeb
12-27-2006, 02:46 AM
Hi, hope everyone had a great Christmas.

The form I'm having problem with is this:


<div id="headnav"><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Please Enter Your
<label>Name: <input type="text" name="name" /></label> <label>Email: <input type="text" name="email" /></label><input type="submit" value="SUBMIT" />
</form>

<?php
// A new author has been entered
// using the form below.

include 'db.inc.php';

$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO author SET
name='$name',
email='$email'";
if (@mysql_query($sql)) {
echo 'Thanks for signing up! <a href="submit.php">Submit an article</a>';
} else {
echo 'Error adding new author: ' .
mysql_error() . '</p>';
}

?>

What I want to happen is after the user has logged in and clicked 'submit', the message will come up 'Thanks for signing up!'. But, its already appearing before the submit button has been clicked.

I'm sure there's a fault with my php or form somewhere, I just can't place it.

whizard
12-27-2006, 03:04 AM
The problem is that after it makes the form, it just keeps going and executes the PHP code below. You need to either store the form handling code in a different file or make the form handling code execute only if the form has been submitted..

Maybe like this:


<form action="<?php echo $_SERVER['PHP_SELF']; ?>?submit=true">
//Rest of form stuff here

<?php
$submit = $_GET['submit'];
if($submit == "true")
{
//Code to handle form
}
?>


HTH, Dan

Brandoe85
12-27-2006, 03:22 AM
You could also just check isset($_POST['your submit button name']); Make sure to give your submit button a name, and use it with $_POST.

good luck.

XiledWeb
12-27-2006, 04:01 AM
Ok, so now I have:


<form action="<?php echo $_SERVER['PHP_SELF']; ?>?submit=true" method="post">
<p>Please Enter Your
<label>Name: <input type="text" name="name" /></label> <label>Email: <input type="text" name="email" /></label><input type="submit" value="SUBMIT" /></form>
<?php

// A new author has been entered
// using the form below.

include 'db.inc.php';

$submit = $_GET['submit'];
if($submit == "true")
{
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO author SET
name='$name',
email='$email'";
if (@mysql_query($sql)) {
echo 'Thanks for signing up! <a href="submit.php">Submit an article</a>';
} else {
echo '<p>Error adding new author: ' .
mysql_error() . '</p>';
}
}
?>

The 'thanks for signing up!' message is displaying aswell as the form - is it possible to have the form 'disappear' and have just the message?

danielwarner
12-27-2006, 12:32 PM
<?php
$submit = $_GET['submit'];
if($submit != "true")
{
echo '

<form action="' . $_SERVER['PHP_SELF'] . '?submit=true" method="post">
<p>Please Enter Your
<label>Name: <input type="text" name="name" /></label> <label>Email: <input type="text" name="email" /></label><input type="submit" value="SUBMIT" /></form>

';
}

// A new author has been entered
// using the form below.

if($submit == "true")
{

include 'db.inc.php';
$name = $_POST['name'];
$email = $_POST['email'];
$sql = "INSERT INTO author SET
name='$name',
email='$email'";
if (@mysql_query($sql)) {
echo 'Thanks for signing up! <a href="submit.php">Submit an article</a>';
} else {
echo '<p>Error adding new author: ' .
mysql_error() . '</p>';
}

}
?>

XiledWeb
12-27-2006, 08:49 PM
Aha! Excellent. It appears I had a few things the wrong way round...

I guess my next question would be, how do I keep the 'thanks for signing up!' (or some kind of welcome message) message on my site at all times?

whizard
12-27-2006, 09:21 PM
When the person logs in, store his info in a SESSION variable (http://us3.php.net/manual/en/ref.session.php). Then on each page, check to see if the session var exists, and if it does, echo the welcome msg.

EDIT: Looks like I have more time to explain than I thought I did:
In order to use sessions, you must put this line:
session_start();
at the very very beginning of each page.

Then, you can do this on the log in page


//Code to log user in
$_SESSION['logged_in'] = "true";


...and this on every page where you want to display a msg if the person is logged in



if($_SESSION['logged_in'] == "true")
{
echo $welcome_msg;
}


HTH
Dan

XiledWeb
12-28-2006, 12:17 AM
Is it going to be much harder to code this if I want the message to remain if a user leaves the site, and then comes back again?

whizard
12-28-2006, 12:22 AM
You would need some way of determining that it was the user when they came back.

You could use a cookie, which is easy to setup, but it will expire after a while, and some people disable them.

Your other option is to use a database and a login script, which is harder to setup, and people will still have to login to get the message, but is permanent.

Dan

XiledWeb
12-28-2006, 12:43 AM
I think I would like to use the 'cookie' method for now, I'll add a disclaimer that the site requires cookies (Users only need to sign in if they want to add an article to the site - regular viewers can view the articles without having to sign up).

whizard
12-28-2006, 03:03 AM
Here is the PHP page on cookies...
http://us3.php.net/manual/en/features.cookies.php

I was going to give example codes, but then it struck me that I wasn't sure exactly what you are trying to do.

Who are you trying to show the welcome message to?
Everyone?
Some People? - If so, how will you know which people?

Unfortuantly, I'm gonna be in Virginia tomorrow and New York (State) Fri-Sun, so I hope someone else will be able to pick up the slack for a while.
(I may check these boards once tomotrrow night, but I hope your problem is solved by then!)

For now, check out the PHP cookies link and see what that does for you!

HTH
Dan

XiledWeb
12-28-2006, 03:29 AM
Here is the PHP page on cookies...
http://us3.php.net/manual/en/features.cookies.php

I was going to give example codes, but then it struck me that I wasn't sure exactly what you are trying to do.

Who are you trying to show the welcome message to?
Everyone?
Some People? - If so, how will you know which people?


I'm trying to show the welcome message to those who have completed the form.

Will check out the link - thanks!

whizard
12-28-2006, 03:35 AM
I'm trying to show the welcome message to those who have completed the form.

So, when someone submits your form, you put this code in the form handler



setcookie(*/ Put your values here */);


and in any page you want to show a welcome message, you would put something like this:


if(isset($_COOKIE['NAME YOU PUT IN SETCOOKIE'];))
{
print("Thank You for signing up!");
}


HTH, Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum