Reputation: 653
i am a beginner here
i need a mail subscription thing
So with php and mysql i managed to create this form
<?php
/*
Template name: mail form
*/
// if using a custom function, you need this
$Email = $_POST['email-form'];
$firstn = $_POST['First-name'];
global $wpdb;
$table_name = $wpdb->prefix . "new_mail_form";
$wpdb->insert( $table_name, array( 'email' => $Email, 'name' => $firstn ) )
?>
<form method="post" action="#" enctype="multipart/form-data" id="Submitform" >
<fieldset class="email-form">
<label for="email-form">:</label>
<input type="text" name="email-form" id="email-form" placeholder="Enter Your Email here" />
</fieldset>
<fieldset class="First-name">
<label for="First-name">:</label>
<input type="text" name="First-name" id="First-name" placeholder="Enter Your Name here" />
</fieldset>
< input type="submit" value="Publish Post" tabindex="40" id="submit" name="submit" class="submitbutton" />
</form>
But the problem is that on every page refresh it adds a entry in my sql
so when someone loads the page it automatically adds blank entry
How can i prevent doing that ?/
Upvotes: 0
Views: 43
Reputation: 16989
That's beacuse you're not checking if the form is submitted first. Try this:
if (!empty($_POST)) {
$Email = $_POST['email-form'];
$firstn = $_POST['First-name'];
global $wpdb;
$table_name = $wpdb -> prefix . "new_mail_form";
$wpdb -> insert($table_name, array('email' => $Email, 'name' => $firstn));
}
Upvotes: 1