Jump to content

PHP Form Mailer


MaizeNBlueJ

Recommended Posts

Alright guys. Let me preface this by saying I'm not a programmer. That'll be obvious once you see the code. Anyway, I'm creating a form mailer for a web site that I'm creating for a local mayoral candidate. I'm moving right along with it, and have it functioning for the most part, but I have a few minor things that I haven't been able to get to work properly.

 

I also want to say that I have been googling and researching most of the day today, so I have put in quite a bit of effort to try and figure this out on my own. With that said, here are the issues I'm having:

 

1) I have a input box that's hidden via CSS that needs to be blank in order to send the form, otherwise it should quit. The problem is, it's still delivering the message, in spit of the fact that it meets the first condition. Please note that I've got the CSS code commented out at the moment so I can test the input.

2) I'm having trouble getting data from a checkbox array to show up in my test email messages.

 

Here's the HTML Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>****</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function validateForm()
{
var x=document.forms["contact"]["fname"].value
var y=document.forms["contact"]["lname"].value
var z=document.forms["contact"]["email"].value
if (x==null || x=="")
  {
  alert("Please enter your first name.");
  return false;
  }
if (y==null || y=="")
  {
  alert("Please enter your last name.");
  return false;
  } 
if (z==null || z=="")
  {
  alert("Please enter your email address.");
  return false;
  } 
}
</script>
<script type="text/javascript">
function toggle_checkboxes()
{
var rb=document.forms["contact"]["VolRadio"];
var cb=document.forms["contact"]["voljobs"];
	if (rb[0].checked)
  		{
   			cb[0].disabled = "";
		cb[1].disabled = "";
   			cb[2].disabled = "";
   			cb[3].disabled = "";
   			cb[4].disabled = "";
  		}
	else
  		{
   			cb[0].disabled = "disabled";
		cb[1].disabled = "disabled";
   			cb[2].disabled = "disabled";
   			cb[3].disabled = "disabled";
   			cb[4].disabled = "disabled";
  		}
}
</script>
</head>

<body>

<div class="container">
  <div class="header"><img src="../images/header.jpg" alt="***" width="960" height="125" id="Insert_logo" style="background: #FF0; display:block;" /> 
    <!-- end .header --></div>
  <div class="menu">
    <ul class="nav">
      <li>
        <div align="center"><a href="index.html">
          <h4>Home</h4>
        </a></div>
      </li>
      <li>
        <div align="center"><a href="personal.html">
          <h4>Personal</h4>
        </a></div>
      </li>
      <li>
        <div align="center"><a href="whereistand.html">
          <h4>Where I Stand</h4>
        </a></div>
      </li>
      <li>
        <div align="center"><a href="press.html">
          <h4>Press Releases</h4>
        </a></div>
      </li>
      <li>
        <div align="center"><a href="getinvolved.html">
          <h4>Get Involved</h4>
        </a></div>
      </li>
      <li class="first">
        <div align="center"><a href="#">
          <h4>Contact</h4>
        </a></div>
      </li>
      <li>
        <div align="center"><a href="links.html">
          <h4>Links</h4>
        </a></div>
      </li>      
    </ul></div>
    <div class="sidebar1" align="center">
    <script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
  version: 2,
  type: 'profile',
  rpp: 10,
  interval: 6000,
  width: 240,
  height: 500,
  theme: {
    shell: {
      background: '#333333',
      color: '#ffff00'
    },
    tweets: {
      background: '#cccccc',
      color: '#000000',
      links: '#ffff00'
    }
  },
  features: {
    scrollbar: true,
    loop: false,
    live: false,
    hashtags: true,
    timestamp: true,
    avatars: false,
    behavior: 'all'
  }
}).render().setUser('BurgerforJasper').start();
</script>
    <!-- end .sidebar1 -->
    <br><a href="http://www.facebook.com/pages/***" target="_blank"><img src="../images/facebook.png"/></img></a>    <a href="http://twitter.com/#!/***" target="_blank"><img src="../images/twitter-white.png"></img></a></div>
  <div class="content">
<img src="../images/***.png" width="178" class="fltlft" />
    <h3 align="right">Contact</h3>
    <p align="right">Please fill out the form below to contact me.</p>
    <form id="contact" name="contact" action="jbformmail.php" onsubmit="return validateForm();" method="post">
    <table width="500px" id="tablefloat">
      <tr>
      <td align="left"><label for="fname">First Name *</label></td>
      <td><input name="fname" type="text" class="textboxsize" /></td>
      </tr>
      <tr>
      <td align="left"><label for="lname">Last Name *</label></td>
      <td><input name="lname" type="text" class="textboxsize"/></td>
      </tr>
      <tr>
      <td align="left"><label for="address">Addresss</label></td>
      <td><input name="address" type="text" value="" class="textboxsize" /></td>
      </tr>
      <tr>
      <td align="left"><label for="phone">Phone Number</label></td>
      <td><input name="phone" type="text" class="textboxsize" /></td>
      </tr>
  <tr>
      <td align="left"><label for="email">Email Address *</label></td>
      <td><input name="email" type="email" class="textboxsize" /></td>
      </tr>
      <tr><td></td>
      <td align="right"><label>Are you willing to volunteer your time to the campaign?</label>
      <br />
      <label>
        <input type="radio" name="volradio" value="Yes" id="VolunteerYesNo_0" onclick="toggle_checkboxes()" checked="checked"/>
        Yes</label>  
      <label>
        <input type="radio" name="volradio" value="No" id="VolunteerYesNo_1" onclick="toggle_checkboxes()" />
        No</label><br /><br /></td>
        </tr>
      <tr>
      <td></td>
      <td align="right">If Yes, what are you willing to do? (Check all that apply.)</td>
      </tr>
      <tr>
        <td></td>
        <td width="292" align="right"><label>Walk In Parades</label> <input type="checkbox" name="voljobs[]" value="Walk In Strassenfest Parade" id="voljobs_0" /></td>        
      </tr>
      <tr>
        <td></td>
        <td align="right"><label>Participate in Phone Banks</label> <input type="checkbox" name="voljobs[]" value="Participate in Phone Banks" id="voljobs_1" /></td>
      </tr>
      <tr>
        <td></td>
        <td align="right"><label>Help at Campaign Headquarters</label> <input type="checkbox" name="voljobs[]" value="Help at Campaign Headquarters" id="voljobs_2" /></td>
      </tr>
      <tr>
        <td></td>
        <td align="right"><label>Walk Door to Door</label> <input type="checkbox" name="voljobs[]" value="Walk Door to Door" id="voljobs_3" /></td>
      </tr>
      <tr>
        <td></td>
        <td align="right"><label>Work On Election Day</label> <input type="checkbox" name="voljobs[]" value="Work on Election Day" id="voljobs_4" /><br /><br /></td>
      </tr>
    <tr>
    <td align="left" valign="top"><label for="comments">Comments</label></td>
    <td><textarea name="comments" rows="20" cols="20" class="textboxsize" ></textarea></td></tr>
    <tr>
    <td></td>
    <td align="right"><input type="submit" value="Submit" /> <input type="reset" /></td>
    </tr></table>
    <input class="special" name="url" type="text" /> 
    </form>
    <h4>***</h4><p>***</p>
  <!-- end .content --></div>
  <div class="footer">
    <p>This .footer contains the declaration position:relative; to give Internet Explorer 6 hasLayout for the .footer and cause it to clear correctly. If you're not required to support IE6, you may remove it.</p>
    <!-- end .footer --></div>
  <!-- end .container --></div>
</body>
</html>

 

And here's the PHP script:

<?php
$to = "my@email.com";
$email = $_POST['email'] ;
$fname = $_POST['fname'] ;
$lname = $_POST['lname'] ;
$address = $_POST['address'] ;
$phone = $_POST['phone'] ;
$volradio = $_POST['volradio'] ;
$voljobs = $_POST['voljobs'] ;
$url = $_POST['url'] ;
$comments = $_POST['comments'] ;
$subject = "Web Submission from: $fname $lname";
$headers = "From: noreply@***.com";
$body = "Name: $fname $lname \n\n Email: $email \n\n Address: $address \n\n Phone: $phone \n\n Volunteer: $volradio \n\n Jobs: $vj \n\n Comments: $comments";
$send = mail($to, $subject, $body, $headers);
foreach($voljobs as $vj)
if($url !== "")
{
echo "Sorry, could not complete your request.";
exit();
}
else
{
$send;
echo "Submission successful. <a href=***/>Click here</a> to return to the web site.";
}
?>

 

Like I said, I'm a PHP newbie, and I've gotten a decent amount done, but I'm having trouble finishing it off. Any help you can provide is appreciated. Thanks guys!

Link to comment
Share on other sites

1) Why don't you just make the css box a: type="hidden"?

 

Also, $send = mail($to, $subject, $body, $headers); will send the mail as soon as that line is read. You need to put that code inside your else.

 

The mail function returns true or false. So when you try to run "$send" like you are, your just making a random statement that does nothing.

 

2)  Jobs: $vj

 

There is no variable vj until after you do the if. Your php script needs some work. The logic doesnt make sense. It should look more like this:

 

<?php
$to = "my@email.com";
$email = $_POST['email'] ;
$fname = $_POST['fname'] ;
$lname = $_POST['lname'] ;
$address = $_POST['address'] ;
$phone = $_POST['phone'] ;
$volradio = $_POST['volradio'] ;
$voljobs = $_POST['voljobs'] ;
$url = $_POST['url'] ;
$comments = $_POST['comments'] ;
$subject = "Web Submission from: $fname $lname";
$headers = "From: noreply@***.com";
$jobs = "";

foreach ($voljobs as $vj) {
	$jobs.="{$vj} ";
}

$body = "Name: $fname $lname \n\n Email: $email \n\n Address: $address \n\n Phone: $phone \n\n Volunteer: $volradio \n\n Jobs: $vj \n\n Comments: $comments";

if($url !== "") {
	echo "Sorry, could not complete your request.";
	exit();
}
else {
	if (mail($to, $subject, $body, $headers)) {
		echo "Submission successful. <a href=***/>Click here</a> to return to the web site.";
	}
	else {
		echo "Sorry, could not complete your request.";
		exit();
	}
}
?>

Link to comment
Share on other sites

1) Why don't you just make the css box a: type="hidden"?

 

Also, $send = mail($to, $subject, $body, $headers); will send the mail as soon as that line is read. You need to put that code inside your else.

 

The mail function returns true or false. So when you try to run "$send" like you are, your just making a random statement that does nothing.

 

2)  Jobs: $vj

 

There is no variable vj until after you do the if. Your php script needs some work. The logic doesnt make sense. It should look more like this:

 

<?php
$to = "my@email.com";
$email = $_POST['email'] ;
$fname = $_POST['fname'] ;
$lname = $_POST['lname'] ;
$address = $_POST['address'] ;
$phone = $_POST['phone'] ;
$volradio = $_POST['volradio'] ;
$voljobs = $_POST['voljobs'] ;
$url = $_POST['url'] ;
$comments = $_POST['comments'] ;
$subject = "Web Submission from: $fname $lname";
$headers = "From: noreply@***.com";
$jobs = "";

foreach ($voljobs as $vj) {
	$jobs.="{$vj} ";
}

$body = "Name: $fname $lname \n\n Email: $email \n\n Address: $address \n\n Phone: $phone \n\n Volunteer: $volradio \n\n Jobs: $vj \n\n Comments: $comments";

if($url !== "") {
	echo "Sorry, could not complete your request.";
	exit();
}
else {
	if (mail($to, $subject, $body, $headers)) {
		echo "Submission successful. <a href=***/>Click here</a> to return to the web site.";
	}
	else {
		echo "Sorry, could not complete your request.";
		exit();
	}
}
?>

1) I read somewhere that it might be easier for a bot to see if they looked for the "hidden" terminology in the page. I don't know if they've done that yet or not, but it seemed like a good idea.

2) I appreciate your reply. I modified your code and just sent a couple of test emails, including one testing the spam filter. I'll let you know what I find out with it, but I'm guessing it's going to work. I think my main problem was that I didn't realize that it would send as soon as I put the command for mail in. I thought of it as defining a variable. Now that I know that, what you said makes a lot of sense. Since I'm going to (hopefully) be doing a lot more of this kind of thing, I'm going to study up and compare your script to mine to see why yours is right.

 

Thanks again for your help!

 

Edit: Update - It looks like that just about has it. The only issue I see is the fact that instead of listing all the boxes checked, it's only showing the last one checked. For instance, I checked "Participate in Phone Banks" & "Help at Campaign Headquarters" and only the campaign headquarters one shows up.

Link to comment
Share on other sites

$body = "Name: $fname $lname \n\n Email: $email \n\n Address: $address \n\n Phone: $phone \n\n Volunteer: $volradio \n\n Jobs: $jobs \n\n Comments: $comments";

I just sent another test, and I'm waiting for the email to come in, but noticed that with that change, it printed them on my submission page (where it's supposed to tell you whether it failed or not. Any idea why that is?

 

Thanks man. I really appreciate your help.

Link to comment
Share on other sites

Nevermind. I'm losing my marbles, I set up WAMP on my extra PC last night to use as a testing server, and was on the wrong page when I tried to submit the form. I just did another test, so we'll see how it goes, but the problem mentioned in the previous post is not there after all.

 

I apologize for the confusion.

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.