0

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 ?/

1 Answer 1

1

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));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.