Jump to content

[SOLVED] Mailing List Form Not Doing Much


snuggles79

Recommended Posts

Hi

 

I have designed a mailing list form for my site using PHP and it seems to be stuck in a loop - it's not doing anything on the submit button being clicked other than redisplaying the form - no entry to SQL server or error messages.

 

I'm new to PHP / SQL coding so any help / pointers would be more than gladly appreciated.

 

Below is the code I have at the moment for it:

 


<?

$mailform = "<FORM method=\"POST\" action=\"$PHP_SELF\"> 
<INPUT type=\"hidden\" name=\"op\" value=\"ds\">  
    <span class=\"subtitle\">Join Our Free Mailing Lists</span><br />
   <strong>Name:</strong><br />
   <input name=\"name\" type=\"text\" value=\"$_name\" size=\"20\">
   <br>
<strong>E-mail:</strong><br /> 
<input type=\"text\" name = \"email\" value=\"$name\" size=\"20\"><br>
<label><strong>Weigh-In Day</strong><br />
<select name=\"weighday\">
  <option value=\"  \"> </option>
  <option value=\"Monday\">Monday</option>
  <option value=\"Tuesday\">Tuesday</option>
  <option value=\"Wednesday\">Wednesday</option>
  <option value=\"Thursday\">Thursday</option>
  <option value=\"Friday\">Friday</option>
  <option value=\"Saturday\">Saturday</option>
  <option value=\"Sunday\">Sunday</option>
</select>
</label>
<p></p>
<strong>E-mails Wanted:</strong> <br />
<table width=\"100%\" border=\"0\">
  <tr>
    <td>Daily Motivation </td>
    <td><input type=\"checkbox\" name=\"dailymot\" value=\"Yes\" /></td>
  </tr>
  <tr>
    <td>Weekly Motivation</td>
    <td><input type=\"checkbox\" name=\"weeklymot\" value=\"Yes\" /></td>
  </tr>
  <tr>
    <td>eVolve News </td>
    <td><input type=\"checkbox\" name=\"news\" value=\"Yes\" /></td>
  </tr>
  <tr>
    <td>Special Offers </td>
    <td><input type=\"checkbox\" name=\"offers\" value=\"Yes\" /></td>
  </tr>
  <tr>
    <td>Weekly Recipes </td>
    <td><input type=\"checkbox\" name=\"recipes\" value=\"Yes\" /></td>
  </tr>
</table>


<P><INPUT type=\"submit\" value=\"Sign Up\"></p>

</FORM>

"; 




if ($op != "ds") {

echo "$mailform";

} else if ($op == "ds") {


if ($name == "") {

$namefor = "
<font color=red>Please enter your name!</font><br>
";
$send = 0;

}

if ($email == "") {

$emailfor = "
<font color=red>Please enter your e-mail address!</font><br>
";
$send = 0;

}

if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email))
{

$emailsyn = "
<font color=red>Please enter email in correct format!</font><br>
";
$send = 0;

}


if ($send != "0") {

$name=$_POST['name'];
$email=$_POST['email'];
$weighday=$_POST['weighday'];
$dailymot=$_POST['dailymot'];
$weeklymot=$_POST['weeklymot'];
$news=$_POST['news'];
$offers=$_POST['offers'];
$recipes=$_POST['recipes'];


if ($dailymot !== "Yes") {$dailymot = "No";}
if ($weeklymot !== "Yes") {$weeklymot = "No";}
if ($news !== "Yes") {$news = "No";}
if ($offers !== "Yes") {$offers = "No";}
if ($recipes !== "Yes") {$recipes = "No";}

mysql_connect(SQL LOGIN DETAILS) or die(mysql_error());
mysql_select_db("evolvepl_maillist") or die(mysql_error());
mysql_query("INSERT INTO `people` VALUES ('$name', '$email', '$weighday', '$dailymot','$weeklymot', '$news', '$recipes', '$offers')");
Print "Your information has been successfully added to the database.";} 

} else if ($send == "0") {

echo "$namefor";
echo "$emailfor";
echo "$emailsyn";
echo "$mailform";

}

?>

Link to comment
Share on other sites

scripting hardly ever works first time ;)

 

Tell me about it - took me about 2 days to get my navigation menu script working.

 

One thing I often forget is to remember the ; at the end of the css lines hehe

 

Well the problem of the blank action is now solved - thank you so much for that! - as I have changed the first line to read:

 

  $mailform = "<form action=\"$_SERVER[php_SELF]\" method=\"post\">
<input name=\"op\" type=\"hidden\" value=\"ds\">  

 

However, there must still be something wrong in the script as it still does nothing when submitted and fields are left empty.

 

It also doesn't populate the database when they are filled in  :'(

Link to comment
Share on other sites

Ok, firstly, I'd recommend using full tags as short tags are slowly getting discontinued, so <?php in place of <?

 

(that's not the problem here).. there are a couple of similar problems, I'll tell you one, and you should be able to sort the rest..

 

 

if ($op != "ds") {

 

You're checking  $op but haven't assigned a variable to it, your form is still POST data so;

 

$op = (isset($_POST['op'])) ? $_POST['op'] : 0;

Link to comment
Share on other sites

 

if ($op != "ds") {

 

You're checking  $op but haven't assigned a variable to it, your form is still POST data so;

 

 

Isn't $op set when the form is submitted by this form field:

 

 <input name=\"op\" type=\"hidden\" value=\"ds\">  

 

There is a \ before the first " but the forum seems to have killed it  ::)

 

Good point about the <? - have changed that - thank you!

 

 

Link to comment
Share on other sites

it is sent as POST data <form method="post">.

 

That means you have to check your post data (which is an associative array).

 

So $_POST['op'] will contain the value from <input name="op" type="hidden" value="ds">

 

Same goes for name and your other form inputs

Link to comment
Share on other sites

I've just had a look at your script and noticed a few things...

 

1. You don't have a name for your submit button and haven't checked when it is being clicked

2. Your NAME text box is being given $_name and EMAIL $name

3. checkbox types should really be using "checked" instead of "value"

 

I hope you don't mind but I found this way easier to use...

<?php
  $msg='Please complete the form:';
  $name=$_POST['name'];
  $email=$_POST['email'];
  $weighday=$_POST['weighday'];
  if ($_POST['dailymot']) {$dailymot='Yes';} else {$dailymot='No';}
  if ($_POST['weeklymot']) {$weeklymot='Yes';} else {$weeklymot='No';}
  if ($_POST['news']) {$news='Yes';} else {$news='No';}
  if ($_POST['offers']) {$offers='Yes';} else {$offers='No';}
  if ($_POST['recipes']) {$recipes='Yes';} else {$recipes='No';}

  if ($_POST['subsend']) {
    if ($name == "") {
      $msg='<font color=red>Please enter your name!</font>';
    } else {
      if ($email == "") {
        $msg='<font color=red>Please enter your e-mail address!</font>';
      } else {
        if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email)) {
          $msg='<font color=red>Please enter email in correct format!</font>';
        } else {
          mysql_connect(SQL LOGIN DETAILS) or die(mysql_error());
          mysql_select_db("evolvepl_maillist") or die(mysql_error());
          mysql_query("INSERT INTO `people` VALUES ('$name', '$email', '$weighday', '$dailymot','$weeklymot', '$news', '$recipes', '$offers')");
          Print "Your information has been successfully added to the database.";}
        }
      }
    }
  }
?>
<html>
<head>
</head>
<body>
  <?=$msg?><br><br>
  <FORM method="POST" action="<?=$_SERVER['PHP_SELF']?>"> 
  <INPUT type="hidden" name="op" value="ds">
  <span class="subtitle">Join Our Free Mailing Lists</span><br />
  <strong>Name:</strong><br />
  <input name="name" type="text" value="<?=$_name?>" size="20">
  <br>
  <strong>E-mail:</strong><br /> 
  <input type="text" name = "email" value="<?=$name?>" size="20"><br>
  <label><strong>Weigh-In Day</strong><br />
  <select name="weighday">
  <option value="  "> </option>
  <option value="Monday">Monday</option>
  <option value="Tuesday">Tuesday</option>
  <option value="Wednesday">Wednesday</option>
  <option value="Thursday">Thursday</option>
  <option value="Friday">Friday</option>
  <option value="Saturday">Saturday</option>
  <option value="Sunday">Sunday</option>
  </select>
  </label>
  <p></p>
  <strong>E-mails Wanted:</strong> <br />
  <table width="100%" border="0">
    <tr>
      <td>Daily Motivation </td>
      <td><input type="checkbox" name="dailymot" checked="checked" /></td>
    </tr>
    <tr>
      <td>Weekly Motivation</td>
      <td><input type="checkbox" name="weeklymot" checked="checked" /></td>
    </tr>
    <tr>
      <td>eVolve News </td>
      <td><input type="checkbox" name="news" checked="checked" /></td>
    </tr>
    <tr>
      <td>Special Offers </td>
      <td><input type="checkbox" name="offers" checked="checked" /></td>
    </tr>
    <tr>
      <td>Weekly Recipes </td>
      <td><input type="checkbox" name="recipes" checked="checked" /></td>
    </tr>
  </table>
  <P><INPUT type="submit" name="subsend" value="Sign Up"></p>
  </FORM>
</body>
</html>

Link to comment
Share on other sites

Wow thank you so so much - works almost like a dream - a couple of bits for me to tweak but near enough spot on.

 

I knew my code was messy - I tried using an online tutorial to create it.

 

My original version of the form was much more similar to the one you have recoded for me - the only problem was the error messages were appearing at the top of the page rather than at the top of the mailing list part of the page.

 

So now you have cured that problem for me!

 

I did notice problem 2) and had corrected it before you posted your response .... damn typos hehe

 

One question - why should checkbox types be using checked instead of value? What is the difference?

 

Thank you so much once again!

Link to comment
Share on other sites

The one thing I love about PHP is you can make a page in so many different ways!

 

The checkboxes are either on or off - they're either ticked or not ticked so assigning a value doesn't make much sense.

 

This line:

if ($_POST['dailymot']) {$dailymot='Yes';} else {$dailymot='No';}

If the checkbox is ticked then the condition of the if() is TRUE meaning $dailymot is assigned "Yes" and if it isn't ticked it becomes false and the else part is called with $dailymot being assigned "No".

 

btw, tabbing code out like I have makes reading soooo much easier when it comes to debugging - called "nesting"

Link to comment
Share on other sites

And now it's all working exactly how I want it to .... so thank you so much for all your help.

 

Here's the coding I have ended up with:

 


<?php
  $msg='<span class="subtitle">Join Our Free Mailing Lists</span>';
  $name=$_POST['name'];
  $email=$_POST['email'];
  $weighday=$_POST['weighday'];
  if ($_POST['dailymot']) {$dailymot='Yes';} else {$dailymot='No';}
  if ($_POST['weeklymot']) {$weeklymot='Yes';} else {$weeklymot='No';}
  if ($_POST['news']) {$news='Yes';} else {$news='No';}
  if ($_POST['offers']) {$offers='Yes';} else {$offers='No';}
  if ($_POST['recipes']) {$recipes='Yes';} else {$recipes='No';}

  if ($_POST['subsend']) {
    if ($name == "") {
      $msg='<span class="mailerr">Please enter your name!</span>';
    } else {
      if ($email == "") {
        $msg='<span class="mailerr">Please enter your e-mail address!</span>';
      } else {
        if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email)) {
    $msg='<span class="mailerr">Please enter email in correct format!</span>';
        } else {
        mysql_connect(SQL LOGIN DETAILS) or die(mysql_error());
          mysql_select_db(DATABASE NAME) or die(mysql_error());
          mysql_query("INSERT INTO `people` VALUES ('$name', '$email', '$weighday', '$dailymot','$weeklymot', '$news', '$recipes', '$offers')");
          $msg='<span class="mailerr">Thank you. You have been subscribed to our mailing lists</span>';}
        }
      }
    }
  
?>
<html>
<head>
</head>
<body>
  <?=$msg?><br><br>
  <FORM method="POST" action="<?=$_SERVER['PHP_SELF']?>">
  <INPUT type="hidden" name="op" value="ds">

  <strong>Name:</strong><br />
  <input name="name" type="text" value="<?=$name?>" size="20">
  <br>
  <strong>E-mail:</strong><br />
  <input type="text" name = "email" value="<?=$email?>" size="20"><br>
  <label><strong>Weigh-In Day</strong><br />
  <select name="weighday">
  <option value="  "> </option>
  <option value="Monday">Monday</option>
  <option value="Tuesday">Tuesday</option>
  <option value="Wednesday">Wednesday</option>
  <option value="Thursday">Thursday</option>
  <option value="Friday">Friday</option>
  <option value="Saturday">Saturday</option>
  <option value="Sunday">Sunday</option>
  </select>
  </label>
  <p></p>
  <strong>E-mails Wanted:</strong> <br />
  <table width="100%" border="0">
    <tr>
      <td>Daily Motivation </td>
      <td><input type="checkbox" name="dailymot" checked="checked" /></td>
    </tr>
    <tr>
      <td>Weekly Motivation</td>
      <td><input type="checkbox" name="weeklymot" checked="checked" /></td>
    </tr>
    <tr>
      <td>eVolve News </td>
      <td><input type="checkbox" name="news" checked="checked" /></td>
    </tr>
    <tr>
      <td>Special Offers </td>
      <td><input type="checkbox" name="offers" checked="checked" /></td>
    </tr>
    <tr>
      <td>Weekly Recipes </td>
      <td><input type="checkbox" name="recipes" checked="checked" /></td>
    </tr>
  </table>
  <P><INPUT type="submit" name="subsend" value="Sign Up"></p>
  </FORM>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.