unsubscribe html form using php and my sql

3.2k Views Asked by At

I have an html form where people can subscribe to a mailing list. The form includes form validation and when the form is submitted, the data is stored in a database using My SQL.

Here is the code on the index.html page where the form is

  <form id="subscribe-form" action="send.php" method="post">
    <p id="status"></p>
    <div>
      <label for="title">Title:</label>
      <select class="uniform" name="title" id="title">
        <option>Please Choose</option>
        <option>Mr</option>
        <option>Mrs</option>
        <option>Miss</option>
        <option>Ms</option>
      </select>
    </div>
    <div>
      <label for="firstName">First name:</label>
      <input type="text" id="firstName" name="firstName" />
    </div>
    <div>
      <label for="surname">Surname:</label>
      <input type="text" id="surname" name="surname" />
    </div>
    <div>
      <label for="email">Email:</label>
      <input type="text" id="email" name="email" />
    </div>
    <div>
      <label for="phone">Contact Number:</label>
      <input type="text" id="phone" name="phone" />
    </div>
    <div>
      <label for="title">How did you hear about us?</label>
      <select class="uniform" name="refer" id="refer">
        <option>Please Choose</option>
        <option>Google</option>
        <option>Yahoo</option>
        <option>Word of Mouth</option>
        <option>Others</option>
      </select>
    </div>
    <div>
      <input type="checkbox" name="news_updates" value="1" />
      I'd like to hear about the latest news and events updates</div>
    <div>
      <input class="button" type="submit" value=""/>
    </div>
  </form>

Here is the code for send.php

<?php 

    include ('connection.php');

    $sql="INSERT INTO form_data (title,firstName, surname, email, phone, refer, news_updates)
    VALUES
    ('$_POST[title]', '$_POST[firstName]','$_POST[surname]','$_POST[email]','$_POST[phone]','$_POST[refer]','$_POST[news_updates]')";

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

    mysql_close($connected);    
?>

I would like to make another html (unsubscribe.html) page where people can unsubscribe by entering their email address so that their email address would match the corresponding email that is in the database already and remove it from the My Sql database . I found this tutorial which was kind of helpful - http://www.phpsuperblog.com/php/delete-records-from-mysql-database-with-html-form-and-php/

and this is the form on my unsubscribe.html page.

   <form  id="unsubscribe_form" action="delete.php" method="post">
  <div>
    <label for="email_remove">Email:</label>
    <input type="text" id="email_remove" name="email_remove" />
  </div>
  <div>
    <input name="delete" type="submit" id="delete" value="" class="unsubscribe_btn">
  </div>
</form>

but when I enter method="post" in the unsubscribe form. The data from the form on the subscribe / index.html does not get stored in My Sql, instead they come up as blank. So I am guessing I can't have two "post" method maybe??

If someone could guide me in the right direction that would be much appreciate. Thanks.

1

There are 1 best solutions below

4
On

I guess you are at your learning stage. So, I will suggest you to have a check for POST method being called on the page which receives the post.

Example: in your subscribe.php you should have :

<input class = "button" type = "submit" value = "Subscribe" name = "subscribe" />

in send.php

you must do:

if(!isset($_POST['subscribe'])
{
 header('location: subscribe.html');
}

You must use isset for your pages.

If you could display your delete.php, perhaps I can edit this post and assist you further but, so far... A check is required and you can use as many forms as many you like (even on one page) but, make sure they all have different id/names.

Your delete.php script should be:

<?php
require ('connection.php');  // User require for important functions so that if not found, it throws fatal error
$email = $_POST['email_remove'];
// Check for isset POST
$query = "DELETE from form_data WHERE email = '".$email."'";
if(mysql_query($query)){ echo "deleted";} else{ echo "fail";} 
?>

your delete.php seems OK to me.

Can add the following to Line 2 echo ""; print_r($_POST);

and post array in comments?