Sending data to a database or fiel using php

Hi,

I am using HTML to create a simple coming soon page where I offer the option to submit an email address. Do I have to send the email addresses collected ( using html form code) to a php file, which then sends it to mysql database I’ve created with my web host?

If so, does anyone know a simple script that can achieve this?

Thanks is advance,

Php newbie

If you just want a list of email addresses, and don’t need the advanced features of a database I’d just write them to a text file.

Store it above the document root so people can’t download it.
Change /username/ to your username on the server (usually the same as your cPanel login)


if(isset($_POST['email'])) {
   $file = '/home/username/';
   file_put_contents($file . 'emails.txt', $_POST['email'] . "\\r\
", FILE_APPEND);
}

Hi nyfdcss,

Welcome to the Sitepoint Forums!

Do I have to send the email addresses collected ( using html form code) to a php file, which then sends it to mysql database I’ve created with my web host?

Yes, it is obvious if it is your requirement to store the submitted emails in your database created.

For that:

  • Create a form having at least a form element (text field)
  • Submit the form to a script
  • In the submitting script/page/file, you simply connect to the server, select database and get that email address submitted from form and insert it to the table.

This is the complete code but I haven’t tested here. But hope this should work.


<?php
// connect to mysql server
mysql_connect('localhost', 'user', 'pwd') or die(mysql_error());
// select database
mysql_select_db('yourdatabase') or die(mysql_error());

if($_SERVER['REQUEST_METHOD'] == 'POST'){ // check if form was submitted
    $errors = array();
    $email = trim($_POST['email']);
    
    if(empty($email)){
        $errors['email'] = 'E-mail adress is required';
    }
    // you can further check for validating the email
    // search some regular expressions in google
    if(empty($errors)){
        $email = mysql_real_escape_string($email);
        mysql_query("INSERT INTO tblename SET email='$email'") or die(mysql_error());
        header("Location: index.php?sent=1");
        exit();
    }
}
?>
<!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" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="Raju Gautam" />
	<title>Coming Soon</title>
</head>
<body>
    <?php
    if($_GET['sent'] == 1){
        echo 'E-mail was sent successfully.';
    }
    ?>
    <form name="frm1" id="frm1" method="post" accept="index.php">
        Your E-mail: <input type="text" name="email" id="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>" />
        <?php
        if(isset($errors['email'])){
            echo $errors['email'];
        }
        ?>
        <input type="submit" name="btnSubmit" id="btnSubmit" value="Send" />
    </form>
</body>
</html>

Hope this will help you go ahead.

Thanks…i’ll give it a try.