Forum Moderators: coopster

Message Too Old, No Replies

How to Stay on Same Page

         

Unpredict2ble

4:29 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



I have a newsletter form on my main page (index.html) that adds the user to a MySQL database thru a PHP script (addemail.php). When the user enters information into my form and clicks the 'Subscribe' button, they're taking to a confirmation page.

How do I make it to where, once the user fills in the form info, and then click 'Subscribe', they stay on the main page. I don't want the confirmation page opening up.

Thanks for reading!

</Unpredict2ble>

tn_2011

4:36 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



you could try adding

header("Location: mainpage.php");


at the end of your addemail.php page. The script will run, then go back to your main page.
hope this helps ....

rocknbil

4:38 pm on Dec 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<p>All your regular content here. this file is named "main.php" so as you see it posts back to itself.</p>

<?php
if ($_POST['email-address']) {
// Process your form here
echo 'Thank you for your interest.';
}
else { ?>
<form method="post" action="main.php">
<p><label for="email">Email:</label>
<input type="text" name="email-address" id="email" value=""></p>
<p><input type="submit" value="subscribe"></p>
</form>
<?php }?>

<p>The rest of your content here</p>


If the form has been posted, it won't show the form. If the form has not been posted, it will show the form. All on the same page view.

Unpredict2ble

5:05 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



I been trying the header() for the past 20mins. Here's my addemail.php script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SymphoniRose.com</title>
</head>
<body>

<?php
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$email = $_POST['email'];

$hostname='host';
$username='user';
$password='pass';
$dbname='dbname';

$dbc = mysqli_connect($hostname, $username, $password, $dbname) OR DIE ('Unable to connect to database! Please try again later.');

$query = "INSERT INTO email_list (first_name, last_name, email) " .
"VALUES ('$first_name', '$last_name', '$email')";

mysqli_query($dbc, $query) OR DIE ('Error querying database.');

mysqli_close($dbc);

echo 'Thank you for joining our Mailing List.<br />';

header( 'Location: http://www.symphonirose.com/index.html');
?>

</body>
</html>


The problem is i keep getting an error message saying 'Warning headers already sent blah blah blah......' If the header() function is suppose to go at the end of the PHP script, how is it suppose to work?

</Unpredict2ble>

topr8

5:37 pm on Dec 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



get rid of all the html tags on the page, you don't need them when you are just redirectign back to the main page - that is also causing your error

Unpredict2ble

6:15 pm on Dec 20, 2011 (gmt 0)

10+ Year Member




get rid of all the html tags on the page, you don't need them when you are just redirectign back to the main page - that is also causing your error


Nice! I didn't know PHP scripts could stand alone without any HTML at all. Very new to this. Thanks to everyone for their input.


<?php
if ($_POST['email-address']) {
// Process your form here
echo 'Thank you for your interest.';
}
else { ?>
<form method="post" action="main.php">
<p><label for="email">Email:</label>
<input type="text" name="email-address" id="email" value=""></p>
<p><input type="submit" value="subscribe"></p>
</form>
<?php }?>


I "will" understand this sooner than later. I can understand its an IF/ELSE statement. I think its saying, "IF there is an email address, then say something like, 'email already subscribed?' else add it?"

</Unpredict2ble>

g1smd

6:55 pm on Dec 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Never begin your PHP script with a DOCTYPE.

Put all the PHP "logic" at the beginning of the file, and then the HTML page will begin half way down the script (and the HTML page part will begin with a DOCTYPE).

This allows the PHP "logic" part of the PHP script to add redirect and other HTTP headers before the HTML page is begun to be sent.

Unpredict2ble

8:02 pm on Dec 20, 2011 (gmt 0)

10+ Year Member





Never begin your PHP script with a DOCTYPE.

Put all the PHP "logic" at the beginning of the file, and then the HTML page will begin half way down the script (and the HTML page part will begin with a DOCTYPE).

This allows the PHP "logic" part of the PHP script to add redirect and other HTTP headers before the HTML page is begun to be sent.


Thank you so much for that clarification. I'm definitely going to go change some of my pages, so as to start practicing good habits. It makes so much sense now that I start thinking about some of the other tasks I got in mind, that I was confused about.

</Unpredict2ble>

g1smd

9:19 pm on Dec 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You've seen the separation of content and presentation by the use of CSS for styling (instead of <font> tags etc), now look to separating logic from content too.

Building pages in a modular manner takes a little longer but it then makes additions, changes and updates massively easier in the future.

rocknbil

5:10 pm on Dec 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a clarification of what's going on . . .

When you do this

<html>
<?php
// some PHP
?>
<title>More HTML</title>

This is a valid (though not all that efficient) use of PHP. But when you attempt to use header(), you will always get that error because the very first line above - "<html>" is page output. The server sends a content type header. So when you try to print a location header, it's too late, "headers already sent."

As for this,

I think its saying, "IF there is an email address, then say something like, 'email already subscribed?' else add it?"


Close. When you post a form, it posts data to the server. That data, for form method="post", is present in the PHP $_POST variable. So what it's saying is "if someone has posted this form, 'do something', otherwise display the form."

"Add it" is another whole topic. You have to cleanse the data, and also check to see if it already exists in the database, THEN add it. All that goes inside the first "if," but to be useful, we'd need to change our logic a little. If there's an error, we'd want to display the form.

<?php
if ($_POST['email-address']) {
// "Check input" would be some function that
// you write to check the email address,
// see if it already exists in the database, and
// if there's an error, store it in $err. The function
// would return a string containing the error, such as
// "invalid email address" or "email already exists"
$err = check_input($_POST['email-address']);
if ($err) { echo display_form($err); }
else {
// Some logic or function to add to DB, then
echo 'Thank you for your interest.';
}
}
else { echo display_form(); }
?>

Now we've moved all our form into a function, "display_form()". The form accepts one optional parameter: any errors.

<?php
function display_form($error=null) {
$form=null;
if ($error) { $form .= "<p>$error</p>"; }
$form .= '
<form method="post" action="main.php">
<p><label for="email">Email:</label>
<input type="text" name="email-address" id="email" value=""></p>
<p><input type="submit" value="subscribe"></p>
</form>
';
return $form;
}
?>

So what happens here:

- if the form hasn't been submitted, it displays the form.
- if someone submits the form,
-- Check the data. If errors,
--- display form with errors
-- if no errors,
--- Store the data in the database
--- Display confirmation message

There are a million and a half ways to do this, this is not the "best" solution but don't want to lose you in the details. :-)