Jump to content

[SOLVED] help with form code


CyberShot

Recommended Posts

I am trying to make a form for a church website. for prayer requests. I need the form to be sent over email. I filled out the form and all went as expected, but I didn't get an email. Can you check it out to see what I did wrong?

 

The form

 

<form action="handle_form.php" method="post">
<fieldset>
<legend>Enter your prayer request below</legend>
    <p>Name:</p>
<input type="text" name="name" id="name" />
    <p>Email:</p>
<input type="text" name="email" id="email"  />
    
    <p>Gender:<input type="radio" name="gender" value="M" />Male
    <input type="radio" name="gender" value="F"  />Female</p>
    
    <p>would you like to be contacted?</p>
    <select name="contact">
    <option value="y">Yes</option>
    <option value="n">NO</option>
    </select>
    
    <p>Your Prayer Request:<br /><textarea name="comments" rows="10" cols="40"></textarea></p>
    
    </fieldset>
    
    <div><input type="submit" name="submit" value="Send"/>
    </div>
    </form>

 

the code to handle the form

 

<?php

if(!empty($_REQUEST['name']))
{
	$name = $_REQUEST['name'];
}
else
{
	$name = NULL;
	echo "Please put in your name";
}

if(!empty($_REQUEST['email']))
{
	$email = $_REQUEST['email'];
}
else
{
	$email = NULL;
	echo "Please put in your email";
}

if(!empty($_REQUEST['comments']))
{
	$comments = $_REQUEST['comments'];
}
else
{
	$comments = NULL;
	echo "Please add some comments";
}

if(isset($_REQUEST['contact']))
{
	$contact = $_REQUEST['contact'];
if($contact == 'y') {
	echo 'we will contact you shortly';
}elseif ($contact == 'n') {
	echo 'you will not be contacted'; 
} else { 
	$contact = NULL;
	echo 'you should say yes or no to being contacted';
}
}

$mailto = "my-email.net";
mail($mailto, $name, $email, $contact, $comments);
or die("failure");
?>

 

Link to comment
Share on other sites

You have a problem with your logic.  The email function gets called no matter what, even if user gets 1 or more error messages.  You need to rewrite your conditions to add error messages to a variable, and then only call the mail function if there is no error message variable. (and echo the variable if there is).

 

But that's not why you are not getting an email.  You have your arguments all wrong.  Read the manual entry for mail to see what the arguments are supposed to be.

Link to comment
Share on other sites

This is what it's supposed to be:

 

mail  ( string $to , string $subject  , string $message  [, string $additional_headers  [, string $additional_parameters  ]] )

 

This is what you have:

 

mail($mailto, $name, $email, $contact, $comments);

 

I color coded them so you can compare.  Judging by the names of yours vs. theirs, does it really look like to you that you have the right vars being used as the right arguments?

Link to comment
Share on other sites

I have been googling this stuff for a while, I also have been going through a book. I have also been looking at other forms that I know work. forms that I am using right now at this very min on another site. here is the code for that form

 


<?PHP
$email = $HTTP_POST_VARS[email];
$mailto = "my-email.net";
$mailsubj = "information from mysite.com";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Values submitted from mysite.com :\n";
while (list ($key, $val) = each ($HTTP_POST_VARS))
{
if ($key!="submit")
{
$mailbody .= "$key : $val\n";
}
}
mail($mailto, $mailsubj, $mailbody, $mailhead);
?>

 

  So I figured that I could keep the same pattern and all would be well. when I look at what you posted above, it makes no sense because it first, I am a very new to php and second, it doesn't follow the same logic that the web and the books are telling me. I am trying to get it right as you posted

Link to comment
Share on other sites

it printed this to the browser

 

Array
(
    [name] => adf
    [email] => adf
    [gender] => M
    [contact] => y
    [comments] => ad
    [submit] => Send
)

 

of course, I just pushed buttons that don't mean anything. just to put something in the fields

Link to comment
Share on other sites

I found a simple form that works. I will modify it to my needs

 

<form name="form" method="post" action="testform.php">
<p class="bodymd">Your Name<br>
<input type="text" name="Name">
</p>
<p class="bodymd">Your Email<br>
<input type="text" name="Email">
</p>
<p class="bodymd">Comments or Questions<br>
<textarea name="Comments" rows="5" cols="40"></textarea>
</p>
<p class="bodymd">
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Form">
</p>
</form>

 


<?php

if (($Name == "") || ($Email == "") || ($Comments == ""))
{
echo "<form name=form method=post action=contact_thanks.php>";
echo "<p class=bodymd>All three fields of this form are required, I really don't think that's too much to ask...</p>";
echo "<p class=bodymd>Fill in the ones you missed, they are listed below.</p>";
}
if ($Name == "")
{
echo "<p class=bodymd>Your Name<br><input type=text name=Name></p>";
}
else
{
echo "<input type=hidden name=Name value=$Name>";
}
if ($Email == "")
{
echo "<p class=bodymd>Your Email<br><input type=text name=Email></p>";
}
else
{
echo "<input type=hidden name=Email value=$Email>";
}
if ($Comments == "")
{
echo "<p class=bodymd>Comments or Questions<br><textarea name=Comments rows=5 cols=40></textarea></p>";
}
else
{
echo "<input type=hidden name=Comments value=$Comments>";
}

if (($Name == "") || ($Email == "") || ($Comments == ""))
{
echo "<input type=submit name=Submit value=Submit>";
echo "<input type=reset name=Reset value=Clear Form>";
echo "</form>";
}
else
{
$message = "Name: $Name\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $Email\r\nReply-To: $Email\r\n";
mail ("my-email.net", "mail subject goes here", $message, $extra);
echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>";
echo "<p class=bodymd>A response will be sent to $Email as soon as possible.</p>";
}
?>

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.