Jump to content

New to PHP. Can somebody please check why I am getting syntax errors.


Greenmachine

Recommended Posts

Hi all,

        I am new to PHP and wish to use it simply to collect form data and email it to myself.

 

From what I have gathered from PHP tutorials I have write the following script but I am getting syntax error warnings and cant figure out why.

 

Can somebody please tell me where I am going wrong and if this script will actually work?

 

The line I am getting errors on is

<?php
//message contains
$message = "Name:$name</br>Email:$email</br>Phone:$phone</br>Preferred Date:$date</br>Best Time To Contact:$time";
?>

Thank you in advance.


<body>

<?php

//my email
$recipient = "info@hairlosslasergroup.co.uk";

//persons name
$name = htmlspecialchars($_POST['name']);

//persons email
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
    die("E-mail address not valid");
}

//persons phone number
$phone   = htmlspecialchars($_POST['phone']);

//preferred date
$date = htmlspecialchars($_POST['comments']);

//best time
$time = htmlspecialchars($_POST['time']);

//check required fields
$required = check_input($_POST['required'], "Please go back and complete all required fields");

//email subject
$subject = ($_POST['subject']

//message contains
$message = "Name:$name</br>Email:$email</br>Phone:$phone</br>Preferred Date:$date</br>Best Time To Contact:$time";

//message
mail($recipient, $subject, $message);

//redirect to thank you page
header('Location:$redirect');

?>

</body>

</html>

Link to comment
Share on other sites

My form code is: 

     <form id="form1" name="form1" method="post" action="">
      <p>*required </p>
        <input name="redirect" type="hidden" value="thankyou.html" />
        <input name="subject" type="hidden" value="HLLG consultation Kensington" />
      <p>Name*</p>
        <input name="name" type="text" id="name" tabindex="10" size="20" maxlength="25" />
      <p> </p>
      <p>Email*</p>
        <input name="email" type="text" id="email" tabindex="20" size="30" maxlength="50" />
      <p> </p>
      <p>Phone*</p>
        <input name="phone" type="text" id="phone" tabindex="30" size="20" maxlength="20" />
      <p> </p>
      <p>Preferred Date</p>
        <input name="date" type="text" id="date" value="dd/mm/yy" size="20" maxlength="10" />
      <p> </p>
      <p>Best Time To Contact</p>
        <select name="time" id="time" tabindex="50">
          <option value="morning" selected="selected">Morning</option>
          <option value="afternoon">Afternoon</option>
          <option value="evening">Evening</option>
        </select>
        <input name="required" type="hidden" id="required" value="name,email,phone" />
        <input type="submit" name="submit" id="submit" value="Submit" tabindex="60" />
        
</form>

Link to comment
Share on other sites

use this section of code

here was your error

//email subject

$subject = ($_POST['subject']

 

 

<body>

<?php

//my email
$recipient = "info@hairlosslasergroup.co.uk";

//persons name
$name = htmlspecialchars($_POST['name']);

//persons email
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
    die("E-mail address not valid");
}

//persons phone number
$phone   = htmlspecialchars($_POST['phone']);

//preferred date
$date = htmlspecialchars($_POST['comments']);

//best time
$time = htmlspecialchars($_POST['time']);

//check required fields
$required = check_input($_POST['required'], "Please go back and complete all required fields");

//email subject
$subject = $_POST['subject'];

//message contains
$message = "Name:$name</br>Email:$email</br>Phone:$phone</br>Preferred Date:$date</br>Best Time To Contact:$time";

//message
mail($recipient, $subject, $message);

//redirect to thank you page
header('Location:$redirect');

?>

</body>

</html>

Link to comment
Share on other sites

Thank you for the reply. Much help. Should of spotted that myself really.

 

Sorry but I have a couple of newbie questions if you could help.

 

If the php script is in its own .php file and just connect to the form by action, does it need to be enclosed in <body> tags

 

and what file on the server does the .php file go into? the root folder /httpdocs or the php file.

 

Thanks again for your help

Link to comment
Share on other sites

properly formatted html is the best, the page would contain additional information in the head, but it will still run without doing so

 

you can place the php file anywhere you want, as long as know where to link to it

 

one example would be to create a folder named contact under root, and then name this file index.php

 

now you would be able to visit the file as http://mysite.com/contact

 

 

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.