Link to home
Start Free TrialLog in
Avatar of ammartahir1978
ammartahir1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

contact form

Hi All,

i have a simple contact form with name, company, telephone, email and message
i want to email it across to my email address but its giving me an email but when i test my code online with php tester it works fine.

am i missing something!!!!!!!

i have attached the php code and error

error is
Parse error: syntax error, unexpected '~' in /data/22/2/92/133/2092785/user/2294727/htdocs/mailer.php on line 70
if(isset($_POST['submit'])) {
$to = "myemail@myemail.com";
$subject = "This is an automated message from System.";
$name_field = $_POST['name'];
$company_field = $_POST('company');
$telephone_field = $_POST('telephone');
$email_field = $_POST['email'];
$message = $_POST['message'];
 
$body = "From: $name_field\n Company: $company_field\n Telephone: $telephone_field\n E-Mail: $email_field\n Message:\n $message";
 
echo "Message has been send to $to!";
mail($to, $subject, $body);
} else {
echo "Your message has not been sent";
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

The error is reported on line 70.  We have a code snippet here with only 16 lines.  Please post the entire code so we can see all of the line numbers. thanks.
Avatar of ammartahir1978

ASKER

hi Ray, thats the problem i have only this code!!!! shall i post the form design as well if that helps?
Avatar of Jagadishwor Dulal
Got your error check your code:
$company_field = $_POST('company'); //used ('company') () bracket instead of []
$telephone_field = $_POST('telephone');/used ('telephone') () bracket instead of []

Open in new window

They seems like functions

must be
$company_field = $_POST['company'];
$telephone_field = $_POST['telephone'];

Open in new window

Avatar of kumaranmca
kumaranmca

Greetings Ammartahir,

Currently you put the code no issues. Its working perfectly.
 
I think'~' added in your code. Please delete and try your code..Its work for you
Parse error: syntax error, unexpected '~'

Thanks & Regards,
Kumaran
hi Jag i still do get what the error is.

Hi Kumaran there is no ~ in my code thats the whole code which i posted here.
Hi ammartahir,

Okay, Can you attach your php file with thread...I will review and get back to you. Because you getting error display on line 70. But you posted program code only 16 lines.
hi kumaran, this is the whole file, i used only this code in my php
mailer.php
YOur file is corrupted post it on txt extension.
here you go Jag mailer.txt
Post your form code also here
Still there was mistake try using this code if possible post your form code also.

<?php
if(isset($_POST['submit'])) {
$to = "myemailaddress";
$subject = "This is an automated message from System.";
$name_field = $_POST['name'];
$company_field = $_POST['company'];
$telephone_field = $_POST['telephone'];
$email_field = $_POST['email'];
$message = $_POST['message'];
 
$body = "From: $name_field\n Company: $company_field\n Telephone: $telephone_field\n E-Mail: $email_field\n Message:\n $message";
 
echo "Message has been send to $to!";
mail($to, $subject, $body);
} else {
echo "Your message has not been sent";
}
?>

Open in new window

now its saying unexpected ?

form code attached
<form method="post" action="mailer.php">
											<table  cellspacing="0" cellpadding="0" class="form">
											  <tr>
												<td  style="width:198px; height:128px">
													<table  cellspacing="0" cellpadding="0">
													  <tr>
														<td  style="width:198px; height:27px"><input name="name" type="text" value="your name:"></td>
													  </tr>
													   <tr>
														<td  style="width:198px; height:27px"><input name="company" type="text" value="your company:"></td>
													  </tr>
													   <tr>
														<td  style="width:198px; height:27px"><input name="telephone" type="text" value="telephone:"></td>
													  </tr>
													   <tr>
														<td  style="width:198px; height:27px"><input name="email" type="text" value="e-mail"></td>
													  </tr>
													</table>
												</td>
												<td  style="width:202px; height:128px">
													<table  cellspacing="0" cellpadding="0">
													  <tr>
														<td  style="width:202px; height:109px"><textarea name="message" cols="0" rows="0">message:</textarea></td>
													  </tr>
													  <tr>
														<td  style="width:202px; height:19px">
														<br style="line-height:1px">
														<img src="images/spacer.gif" alt="" width="116" height="1"><strong><input type="reset" value="reset" name="reset"</strong>
                                                        <img src="images/spacer.gif" alt="" width="15" height="1"><strong><input type="submit" value="Submit" name="submit"</strong>		</td>
													  </tr>
													</table>
												</td>
											  </tr>
											</table>
											</form>

Open in new window

When there is a parse error with "unexpected" it usually means that somewhere earlier in the script there was a missing terminator - perhaps missing a right paren, or a semicolon.  But that said, if there is an error reported on line 70 and you only have 16 (or 18) lines of code, you are not running the script we are looking at here.  The script you are running has at least 70 lines of code.  There may be an error in the paths to the script you are testing.  Programmers commonly use "signal" notices to visualize information about their scripts.  For example, you might make the script write its name in the browser output with something like this:

echo $_SERVER["REQUEST_URI"];

But that aside, try installing this version of the script (make the modifications that make sense for use in your environment).  If this script runs without errors, check your inbox and spam folder.  If there is still no message found, you will want to work with your server administrator to ensure that the server has the mail functions enabled.  The PHP.net web site may be helpful - definitely worth reading carefully!
http://us.php.net/manual/en/book.mail.php

Best of luck with it, ~Ray
<?php // RAY_form_to_email.php
error_reporting(E_ALL);


// SEND MAIL FROM A FORM


// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS - CHANGE THESE FOR YOUR WORK
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";



// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE THROUGH STRING CONCATENATION
    $content    = NULL;
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
}


// A FORM TO TAKE CLIENT INPUT FOR THIS SCRIPT
$form = <<<ENDFORM
<form method="post">
Please enter your contact information
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>
ENDFORM;

echo $form;



// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    // IF MAGIC QUOTES IS ON, WE NEED TO REMOVE SLASHES
    $str = stripslashes($str);

    // REMOVE EXCESS WHITESPACE
    $rgx
    = '#'                // REGEX DELIMITER
    . '\s'               // MATCH THE WHITESPACE CHARACTER(S)
    . '\s+'              // MORE THAN ONE CONTIGUOUS INSTANCE OF WHITESPACE
    . '#'                // REGEX DELIMITER
    ;
    $str = preg_replace($rgx, ' ', $str);

    // REMOVE UNWANTED CHARACTERS
    $rgx
    = '#'                // REGEX DELIMITER
    . '['                // START OF A CHARACTER CLASS
    . '^'                // NEGATION - MATCH NONE OF THE CHARACTERS IN THIS CLASS
    . 'A-Z0-9'           // KEEP LETTERS AND NUMBERS
    . '&+:?_.,/\-'       // KEEP SOME SPECIAL CHARACTERS (ESCAPED HYPHEN)
    . ' '                // KEEP BLANKS
    . ']'                // END OF THE CHARACTER CLASS
    . '#'                // REGEX DELIMITER
    . 'i'                // CASE-INSENSITIVE
    ;
    $str = preg_replace($rgx, NULL, $str);

    return trim($str);
}

Open in new window

hi ray thank you for your post can you please let me know what parts i need to change according to my script because that all i want to receive in my email????
Is there anything can be done?
ASKER CERTIFIED SOLUTION
Avatar of ammartahir1978
ammartahir1978
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello