homepage Welcome to WebmasterWorld Guest from 54.227.62.141
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Whats wrong with my Form Submission?
agoodwin




msg:4423251
 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]

 

eelixduppy




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


$email_body = "$details"


This line needs a semi-colon at the end...

agoodwin




msg:4423282
 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?

agoodwin




msg:4423294
 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

Seedy




msg:4423296
 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);

agoodwin




msg:4423300
 10:16 pm on Feb 29, 2012 (gmt 0)

good catch, however that did not solve the error...

Seedy




msg:4423308
 10:24 pm on Feb 29, 2012 (gmt 0)

Also,
$email_body = "$details"

Should read
$email_body = "$details";

Seedy




msg:4423313
 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

agoodwin




msg:4423324
 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.

Seedy




msg:4423332
 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;
}
}

?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved