Hi I'm trying to redirect to the next page after data is sumbmitted to mysql. I have so far
<?php
$name = "";
$email = "";
$msg_to_user = "";
if ($_POST['name'] != "") {
include_once "newsletter/connect_to_mysql.php";
// Be sure to filter this data to deter SQL injection, filter before querying database
$name = $_POST['name'];
$email = $_POST['email'];
$sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
$numRows = mysql_num_rows($sql);
if (!$email) {
$msg_to_user = '<br /><br /><span style="font color="FF0000">Please type an email address ' . $name . '.</span>';
} else if ($numRows > 0) {
$msg_to_user = '<br /><br /><font color="FF0000">' . $email . ' is already in the system.</font>';
} else {
$sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime)
VALUES('$name','$email',now() )") or die (mysql_error());
$msg_to_user = '<br /><br /><div style="color=#F47926;">Thanks ' . $name . ', Now lets select what you need.</div>';
$name = "";
$email = "";
}
header("Location: order.php");
}
?>
This sends the data to my database and everything works great, but I'd like to send it to another page at order.php thats already created to continue. The form I've got right now is here
<form style="border:0;padding-left:10px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset style="text-align:left;" id="free">
<span style="text-align:left;font-size:30px;color:#F47926;">
Ready to get started?
</span>
<div class="centerForm">
Name:<br />
<input name="name" type="text" maxlength="36" size="35" value="" /><br />
Email:<br />
<input name="email" type="text" maxlength="36" size="35" value="" /><br />
<br /><br /><div style="width:179px;margin-left:auto;margin-right:auto;"><input type="image" src="continue-to-selection.png" name="mySubmitBtn" alt="continue to selection" /></div>
</fieldset>
</form>
The email function worked before, but it stopped working as I began changes. This I'm sure is something simple and I'm sure I can work it out. My issue is that I'm trying to re-direct after submission of data to mysql, the user to a page order.php. Are the changes that I need to make supposed to be within the form or through the header:location I'm trying to use. Thanks Note: I shortened urls to view it within the post preview here so I'm aware that the order.php must be full path. Correct me if I'm wrong, but just letting you all know I am aware and this was not the issue.
there are 2 approches that I would suggest you try. 1st one (personally prefered) is to use ajax and jquery to submit the form and then display the thank you message without refreshing the page. After that use settimeout function to delay the redirect for x seconds and then redirect to the order page.
2nd approche: is after the data get saved in the database store a success message in a session. then in the header of the order page, use an if statement based on the session. if it's success or true then show the message the thank you message in the order page.
If you want to see an example of the solutions provided, let me know.
thanks
Here's a good rule - Avoid echoing HTML from php. Close the script tag write your HTML and continue your PHP
Also @vidit is right. try this
else {
$sql_insert = mysql_query("INSERT INTO newsletter (name, email, dateTime)
VALUES('$name','$email',now() )") or die (mysql_error());
$msg_to_user = '<br /><br /><div style="color=#F47926;">Thanks ' . $name . ', Now
lets select what you need.</div>';
//$name = "";
//$email = "";
session_start();
$_SESSION['name'] = $name;
$_SESSION['email']=$email;
}
and in the next file you can get the email and name back using
session_start();
$email = $_SESSION['email'];
$name= $_SESSION['name'];
Also look into PDO as an alternative if you are using PHP>5.1
Also, it wouldnt hurt to add some session handling which you can learn here
Cheers!