0

Tried my best with the title of this question but here goes...

Im setting up a landing page (using dreamweaver) for an app I'm working on and I will be asking potential users to leave their email if they are interested which I will store in a MySQL database.

To do this, on my index.html page I have implemented the following code:

<form action="email.php" method="post" />    
<p><input type="text" name="email1" />
<input type="submit" value="Submit" />
</p>
</form>

This creates a simple text entry box for the email and a submit button.

This is of course is linked to the email.php file which contains the following code:

<?php

//I replace these with the correct details    
define('DB_NAME', 'Name');
define('DB_USER', 'User');
define('DB_PASSWORD', 'PW');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . myself_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

echo 'Connected successfully';

$value = $_POST['email1'];

$sql = "INSERT INTO Emails (email) VALUES ('$value')";

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

mysql_close();

?>

When I view my site, the text entry box is there, I can go and enter my details and submit and this sends that info to my MySQL table, Great! However the browser opens the email.php file aka www.domain.com/email.php which displays "Connected successfully" as per my test code above.

Is it possible for me to implement the php code in the background? I do not want visitors who leave their email on my landing page to be redirected to a new page when they click submit instead just a friendly thank you message on the page they are on.

I hope I have explained this well enough and I apologies in advance if this answer has already been cracked on here before.

Thanks!

user1993376
  • 61
  • 1
  • 7
  • 1
    What you want is AJAX, and it's been asked/answered a few bajillion times on this site: http://stackoverflow.com/questions/5298401/basic-php-and-ajax – Marc B Jul 12 '13 at 21:50
  • Yes, you could for example use Ajax to push the email to the server. BUT before you do that 1) learn why you should NOT be using the php mysql_* calls anymore (they're old, obsolete and deprecated cruft) and 2) read about SLQ Injection and how to protect against it. Luckily, the remedy for both illnesses is largely the same, ie PDO with prepared statements – fvu Jul 12 '13 at 21:50
  • you are doing it just fine. just print a user friendly message in php and don't complicate things :) – Aris Jul 12 '13 at 21:51
  • The behavior your hoping to achive is called an asynchronous event, meaning it happens outside of the main thread of exuecution. The best way to do this from a web browser is through an Ajax type post. – JSON Jul 12 '13 at 22:34
  • @fvu - we don't know that he's using the older API from ignorance and that's not a fair assumption. I still work on older hosts that don't have newer versions of MySQL servers required by MySQLi. – JSON Jul 12 '13 at 22:39
  • 1
    @ClosetGeek show me where I mention or even suggest ignorance. I am merely stating two important facts and their remedy. And by pointing to the real risk of SQL injection and the consequences that may (or rather will) have some day, save him/her embarrassment and damage to his/her business. And all that in just 4 lines :-) – fvu Jul 12 '13 at 22:49
  • @fvu - the clear implication that he doesn't know better and should learn not to use it. That would be ignorance. I dont mean it as blunt as it comes off, and you are right to suggest not to use it. – JSON Jul 12 '13 at 23:28
  • Hi guys, Thanks for the advise! I have now implemented AJAX into what I am trying to do and it is working exactly how I wanted. Also, thank you for the heads up on MySQL injections. This is something I have heard of but I will spend this weekend learning more. Cheers – user1993376 Jul 13 '13 at 12:15

1 Answers1

0

The best practice when handling POST requests is to redirect user to a different page once the save (database) operation is complete. This is called a PRG pattern. http://en.wikipedia.org/wiki/Post/Redirect/Get

<?php
    // Handle POST here
    header("Location: thankyou.html");
?>

There are a number of reasons behind this, the most important one being something called a "double POST problem".

See also:

Community
  • 1
  • 1
anttix
  • 7,709
  • 1
  • 24
  • 25