theatereleven Posted May 12, 2010 Share Posted May 12, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/ Share on other sites More sharing options...
947740 Posted May 12, 2010 Share Posted May 12, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057310 Share on other sites More sharing options...
theatereleven Posted May 12, 2010 Author Share Posted May 12, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057312 Share on other sites More sharing options...
947740 Posted May 12, 2010 Share Posted May 12, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057313 Share on other sites More sharing options...
cyberRobot Posted May 12, 2010 Share Posted May 12, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057315 Share on other sites More sharing options...
theatereleven Posted May 12, 2010 Author Share Posted May 12, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057396 Share on other sites More sharing options...
947740 Posted May 12, 2010 Share Posted May 12, 2010 All of your php code needs to be INSIDE the <?php and ?> tags. Link to comment https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057445 Share on other sites More sharing options...
theatereleven Posted May 12, 2010 Author Share Posted May 12, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057455 Share on other sites More sharing options...
947740 Posted May 13, 2010 Share Posted May 13, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057495 Share on other sites More sharing options...
Jiblix Posted May 13, 2010 Share Posted May 13, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057499 Share on other sites More sharing options...
theatereleven Posted May 13, 2010 Author Share Posted May 13, 2010 Yes....it is a Linux based PHP server at godaddy.com. Thanks! Link to comment https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057541 Share on other sites More sharing options...
theatereleven Posted May 13, 2010 Author Share Posted May 13, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057552 Share on other sites More sharing options...
947740 Posted May 13, 2010 Share Posted May 13, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057831 Share on other sites More sharing options...
cyberRobot Posted May 13, 2010 Share Posted May 13, 2010 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 https://forums.phpfreaks.com/topic/201534-have-php-form-that-works-just-need-to-tell-it-to-redirect-user-to-thanks-page/#findComment-1057837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.