Post email from input into the database

Hello. My problem is:

1.I need submit button to be not active before real email will be written down. I have this form now:

<form action ="/includes/newsletter-signup.php" method="post" id="submit-email">

<input "autocomplete="off" type="email" name="email-address" id ="txtmail"
 placeholder="<?php echo $lang['e-mail-footer']; ?>">

<input disabled="true" class="btn-disabled btn-reset button button-blue" 
type="submit" id = "btn-reset" value="<?php echo $lang['sign-up']; ?>">

and code:


// news subscription:
		rules: {
			txtmail: {
				required: true,
				email: true

		messages: {
			email: "Please enter a valid email address"

	}); // end reset-form
  $("#txtmail").on("blur", function(){

And I do not know if it works because it proceed nothing to database and there is 2 question.
2. Is it the right way to collect email from the input and put it into the database(newsletter_subscribers TABLE and sub_email ROW)?
This file below is newsletter-signup.php which is mentioned in the code before.

	$email_data = $_POST['email-address']; 
	$sql_email = "INSERT INTO newsletter_subscribers (sub_email)VALUES ($email_data)";

Thanks in advance for every help.

I’m not sure what your question is, but this is not an easy thing to debug without having access to your project. If you can run your server using a debugger, that would be ideal. Otherwise, have it output something to the console when it receives a POST request at your specified path.

The code looks okay to me. The problem that I do see is that, although you’ve constructed your query correctly, you’re not executing it.

To keep things simple, first open connection to the database and then run your query stored in $sql_email. Only problem being your $sql_query is a variable and you need to concatenate it to your query. Also the value needs to be wrapped in single quotes

So your

$sql_email = "INSERT INTO newsletter_subscribers (sub_email)VALUES ($email_data)";

should really be

$sql_email = "INSERT INTO newsletter_subscribers (sub_email)VALUES ('" . $email_data . "')";

You will need a php extension for sql connectivity called mysqli

Making your final code look like

$hostname = "localhost";
$username = "username";
$password = "password";
$dbname = "some-db";

// Establish a connection with the db
$conn = new mysqli($hostname, $username, $password, $dbname);
// Check if connection was successful
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

$email_data = $_POST['email-address']; 
$sql_email = "INSERT INTO newsletter_subscribers (sub_email)VALUES ('" . $email_data . "')";

if ($conn->query($sql_email ) === TRUE) {
    echo "New email record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;

1 Like

Thank you so much for help!