Welcome to WebmasterWorld Guest from 54.145.136.73

Forum Moderators: coopster & jatar k

Whats wrong with my Form Submission?

   
8:45 pm on Feb 29, 2012 (gmt 0)



upon submitting the form, I get this..

Parse error: syntax error, unexpected T_VARIABLE in /homepages/37/d404090856/htdocs/c5/send_form_email.php on line 30


Here is the code for the submission....


<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['suject'];
$details = $_POST['details'];

//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}

if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}

$email_from = 'contact@example.com';//<== update the email address
$email_subject = "$subject";
$email_body = "$details"

$to = "formsubmissions@example.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers = "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: http://example.com/thanks-for-contacting');


// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}

?>


here is the form

<form id="payment" method="post" action="send_form_email.php">
<fieldset>
<ol>
<li>
<label for=name>Name</label>
<input id=name name=name type=text placeholder="First and last name" required autofocus>
</li>
<li>
<label for=email>Email</label>
<input id=email name=email type=email placeholder="example@domain.com" required>
</li>
<li>
<label for=phone>Phone</label>
<input id=phone name=phone type=tel placeholder="(000) 000-0000" required>
</li>
<li>
<label for=subject>Subject</label>
<input id=subject name=subject type=text required>
</li>
<li>
<label for=details>Details</label>
<textarea id=details name=details rows=5 required></textarea>
</li>
</ol>
</fieldset>
<fieldset>
<button type="submit" value="submit">Submit!</button>


</fieldset>
</form>




Any help would be greatly appreciated.

Thanks

[edited by: eelixduppy at 9:36 pm (utc) on Feb 29, 2012]
[edit reason] exemplified domain [/edit]

9:39 pm on Feb 29, 2012 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member




$email_body = "$details"


This line needs a semi-colon at the end...
9:56 pm on Feb 29, 2012 (gmt 0)



I fixed that and then got....

error; you need to submit the form!
Warning: Cannot modify header information - headers already sent by (output started at /homepages/37/d404090856/htdocs/c5/send_form_email.php:5) in /homepages/37/d404090856/htdocs/c5/send_form_email.php on line 36


I was able to get rid of the warning. But I cannot figure out how to get rid of the "error; you need to submit the form!"

I thought that is what I was doing when I hit submit!

any ideas?
10:07 pm on Feb 29, 2012 (gmt 0)



apparently even with the error, the form did still go through. I just got an email, but there is one problem... why does it say that it is from cgi-mailer@perfora.net ?

I have the from address set to contact@mywebsite.com
10:09 pm on Feb 29, 2012 (gmt 0)



$subject = $_POST['suject'];


Should surely read
$subject = $_POST['subject'];

Regarding the error, try adding the following to the top of your page:
//suppress notices (picky PHP error messages)
error_reporting(E_ALL ^ E_NOTICE);
10:16 pm on Feb 29, 2012 (gmt 0)



good catch, however that did not solve the error...
10:24 pm on Feb 29, 2012 (gmt 0)



Also,
$email_body = "$details"

Should read
$email_body = "$details";
10:31 pm on Feb 29, 2012 (gmt 0)



Is the php page an include on another page? If not, remove the
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}


You can always replace it later once the form is working if you decide to include('page.php'); the form
10:48 pm on Feb 29, 2012 (gmt 0)



okay. that last part seemed to fix the problem. As you may have figured, I copied and pasted the php, just changing a few things here and there as needed.

Thanks so much for the help working out the bugs.
11:06 pm on Feb 29, 2012 (gmt 0)



Here's a very basic but more complete solution for you. This fixes the 'from' value in the received email and redirects to a local 'thanks.php' page which you'll need to add.

<html>
<head></head>
<body>

<form id="payment" method="post" action="mail.php">
<fieldset>
<ol>
<li>
<label for=name>Name</label>
<input id=name name=name type=text placeholder="First and last name" required autofocus>
</li>
<li>
<label for=email>Email</label>
<input id=email name=email type=email placeholder="example@domain.com" required>
</li>
<li>
<label for=phone>Phone</label>
<input id=phone name=phone type=tel placeholder="(000) 000-0000" required>
</li>
<li>
<label for=subject>Subject</label>
<input id=subject name=subject type=text required>
</li>
<li>
<label for=details>Details</label>
<textarea id=details name=details rows=5 required></textarea>
</li>
</ol>
</fieldset>
<fieldset>
<button type="submit" value="submit">Submit!</button>&nbsp;<button type="reset" value="reset">Reset</button>
</fieldset>
</form>

</body>
</html>


Create 'mail.php'

<?php

$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$details = $_POST['details'];

if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}

if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}

$email_subject = "$subject";
$email_body = "$details";

$to = "you@yourdomain.fu"; //CHANGE
$headers = "From: $name";
$headers = "Reply-To: $visitor_email";
mail($to, $email_subject, $email_body, "From: $name <$email>\r\nContent-Type: text/plain; charset=\"UTF-8\"\r\n$headers");
header('Location: thanks.php');

function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}

?>
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month