Jump to content

Have PHP form that works - just need to tell it to redirect user to thanks page


theatereleven

Recommended Posts

Hey forum!  I'm a COMPLETE PHP newbie.  Searched around and got the form below to work.  Inserting basic data from a form into a MySQL database, and it works.  The only thing that I need now is help on two things:

 

1) I need it to direct the user (input person) to a thankyou.php page.

2) I need to send a confirmation email to them and myself.

 

Can anyone help me?  That would rock!!!! Below is the code for my php form:

 

 

<html>

<body>

 

$hostname="myhost";

$username="myusername";

$password="mypassword";

$dbname="ifdcustomers";

$usertable="CustomerInfo";

$first_name = "firstNAME";

$last_name = "lastNAME";

 

 

<?php

$con = mysql_connect("myserver","myusername","mypassword");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("ifdcustomers", $con);

 

$sql="INSERT INTO CustomerInfo (firstNAME, LastNAME)

VALUES

('$_POST[first_name]','$_POST[last_name]')";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";

 

mysql_close($con)

?>

 

</body>

</html>

Link to comment
Share on other sites

The thing is...to redirect you cannot have any output.  I.E. no html, or any words, or anything.

 

A redirect is simple:

 

header("Location: thankyou.php");

 

^ that assumes that thankyou.php is in the same folder as that php file you posted

Link to comment
Share on other sites

thanks for the help.  i'm kinda new (great at HTML from scratch but just getting into PHP)

 

so should I lose the echo statement that says one record added?  i guess i don't know what to take out of my current form and replace with that line you gave me.

 

Link to comment
Share on other sites

Just take out the two pieces of html at the top and then the two at the bottom.  Also, remove the echo statement.  Then simply put that redirect code wherever in the script you want them redirected.

 

Also, I forgot -> it is more secure if you add an exit(); after each redirect.  So, the code would be:

 

header("Location: thankyou.php");
exit();

Link to comment
Share on other sites

Is there a reason why you need to redirect to the thank you page? I usually prefer to have the thank you message in the same script.

 

.
.
.
if (!mysql_query($sql,$con)) {
  die('Error: ' . mysql_error());
}

//SEND E-MAIL MESSAGE TO USER AND WEBMASTER
/* add code to send message here */

//DISPLAY THANK YOU MESSAGE
echo "<p>Thanks, your information has been added to the database.</p>";

mysql_close($con)
?>

</body>
</html>

Link to comment
Share on other sites

I removed the echo statement and html.  Still not redirecting....hmmm.  It sends me to a blank page that says this: 

 

$hostname="address.net"; $username="username"; $password="password"; $dbname="ifdcustomers"; $usertable="CustomerInfo"; $EMAIL = "EMAIL"; $skillONE = "skillONE"; header("Location: thankyou.php");exit();

 

Link to comment
Share on other sites

I'm getting no where fast....below is the code i'm using now.  It posts to the DB fine, but will not redirect.  If I add the line: header("Location: thankyou.php"); anywhere PHP errors out talking about the header. =(

 

 

<?php

$hostname="test.net";

$username="test";

$password="test";

$dbname="ifdcustomers";

$usertable="CustomerInfo";

$EMAIL = "EMAIL";

$skillONE = "skillONE";

$con = mysql_connect("test.net","test","test");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());  }

mysql_select_db("ifdcustomers", $con);

$sql="INSERT INTO CustomerInfo (EMAIL, skillONE)

VALUES

('$_POST','$_POST[skillONE]')";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

mysql_close($con)

 

?>

Link to comment
Share on other sites

It's probably because an error is displaying?

 

Also, you can not have ANY characters before the <?php.  That means you can't have spaces, either.

 

Also, there are miscellaneous other errors in your script.  For example, the parts where you call $_POST need to have ' around the field names.

 

I fixed what I noticed:

 

<?php
$hostname = "test.net";
$username = "test";
$password = "test";
$dbname = "ifdcustomers";
$usertable = "CustomerInfo";
$EMAIL = "EMAIL";
$skillONE = "skillONE";
$con = mysql_connect("test.net","test","test");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());  }
mysql_select_db("ifdcustomers", $con);
$sql="INSERT INTO CustomerInfo (EMAIL, skillONE)
VALUES
('{$_POST['EMAIL']}','{$_POST['skillONE']}')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con)

?>

 

I have a question...why do you declare all the variables at the top, but you don't use them for anything?

Link to comment
Share on other sites

This might seem like a dumb question, But since your code is enclosed in <?php ?> tags. And its just being echo'd to the browser.. Are you running a PHP and Apache Server? You cant execute a php file by clicking on it. It has to be run from a server. At the top of your browser does it say "http://localhost", "http://127.0.0.1", "http://192.168.1.100", "http://192.168.1.101", "http://192.168.1.102", or "http://192.168.1.103".. etc..

Link to comment
Share on other sites

947740!!!!!!  You did it!!!!!!!!!!  Thanks so much.

 

To answer your question - I have no idea on the variables.  "PHP newbie" means i scoured the NET shot gunning code until something worked. =)

 

So yes....have more learning to do.

 

Link to comment
Share on other sites

947740!!!!!!  You did it!!!!!!!!!!  Thanks so much.

 

To answer your question - I have no idea on the variables.  "PHP newbie" means i scoured the NET shot gunning code until something worked. =)

 

So yes....have more learning to do.

 

 

Glad I could help.

Link to comment
Share on other sites

To answer your question - I have no idea on the variables.  "PHP newbie" means i scoured the NET shot gunning code until something worked. =)

 

 

Those variables are likely to have come from a tutorial on connecting to a MySQL database. I've seen a lot of scripts which declare their variables first:

$hostname = "test.net";
$username = "test";
$password = "test";

 

 

Then, use the variables to connect to the database:

$con = mysql_connect($hostname, $username, $password);

 

 

This is my preferred method also since it makes the code a little cleaner. Therefore easier to maintain.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.