Page 1 of 1

Parse error: syntax error, unexpected T_STRING

Posted: Wed Jun 29, 2011 1:44 pm
by LSanz
Hello, I'm new to your forums. I've been working on a simple (or so I thought) php form. I have to be honest in saying that I found the code online in a tutorial and have tried to configure it too my own needs by deleting some of the fields that were present and replacing them with my own. I'm getting the following error message and would greatly appreciate your help! :D

Error:

Parse error: syntax error, unexpected T_STRING in /home/content/32/4805732/html/contactcma.php on line 10

Here's the php file:

Code: Select all

<?php
/* Set e-mail recipient */
$myemail  = "you@domain.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$address = check_input($_POST['address'], "Enter your address");
$city = check_input($_POST['city'], "Enter your city");
$state = check_input($_POST['state'], "Enter your state");
$zip code = check_input($_POST['zip code'], "Enter your zip code");
$email    = check_input($_POST['email']);
$phone = check_input($_POST['phone'], "Enter your phone number");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the E-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $email

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks_cma_request.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
- - - - - - - - - - - - - - - - -
thanks!
Linda

Re: Parse error: syntax error, unexpected T_STRING

Posted: Wed Jun 29, 2011 4:47 pm
by twinedev
$zip code is not a valid cariable name. Try either $zipCode or $zip_code

Also, once that is take care of, down on line 26, you don't close the quote for $message = ".....

This is a good example of where using a PHP editor comes in handy, both of this were lit up for me as soon as I pasted it into phpED. (there are several free ones available as well, that is just what I use).

-Greg

Re: Parse error: syntax error, unexpected T_STRING

Posted: Wed Jun 29, 2011 8:19 pm
by LSanz
Thank you Greg - I made both of the changes that you suggested and am no longer getting the original error message; now I'm getting this error message:

Parse error: syntax error, unexpected $end in /home/content/32/4805732/html/contactcma.php on line 62



Here's the php file:
- - - - - - - - - - - - - - -

Code: Select all

<?php
/* Set e-mail recipient */
$myemail  = "you@domain.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$address = check_input($_POST['address'], "Enter your address");
$city = check_input($_POST['city'], "Enter your city");
$state = check_input($_POST['state'], "Enter your state");
$zipcode = check_input($_POST['zipcode'], "Enter your zip code");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone'], "Enter your phone number");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* Let's prepare the message for the E-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $email

/* Send the message using mail() function */
mail($myemail, $subject, $message='');

/* Redirect visitor to the thank you page */
header('Location: thanks_cma_request.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

Re: Parse error: syntax error, unexpected T_STRING

Posted: Wed Jun 29, 2011 9:06 pm
by twinedev
We may have had the line numbers not matching. You had added quotes in this line:

Code: Select all

mail($myemail, $subject, $message='');
however it was correct as it was just being

Code: Select all

mail($myemail, $subject, $message);
The part I was referring to is where you define $message:

Code: Select all

$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $email
This is the part that is missing the closing quote (and closing semicolon)

SHould be something line this (adding in something I always recommend for form submissions, the users IP address)

Code: Select all

$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $email

=========================================
Form submitted by IP " . $_SERVER['REMOTE_ADDR'];

Re: Parse error: syntax error, unexpected T_STRING

Posted: Wed Jun 29, 2011 9:17 pm
by twinedev
Also just a hint, even without a full PHP editor with syntax highlighing, you can sort of cheat, by coming here, starting a "New Topic" then use the PHP Code button for the editor to place your code. Then, instead of hitting SUBMIT, do PREVIEW so you can see how it looks. In the preivew (and the sample in this post), you will have to hit the "Expand" link to see the full code without having to use the scrollbar.

Here is your original code, while it will take you a little look through it, but you can see on your original code the the line $zip code =.... that $zip and code are two different colors, indicating it is not seeing code as part of the variable name. Also down where you left off the closing quotes, you can see the coloring remains the same (bright blue for literal text, dark blue (or is that cyan) for variables inside the quotes). Really noticeable is that the comment block below it is not colored the same as comments above it.

Then once you are done making your changes, just browse away from the "New Topic" page. ;-)

Code: Select all

<?php
/* Set e-mail recipient */
$myemail = "you@domain.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$address = check_input($_POST['address'], "Enter your address");
$city = check_input($_POST['city'], "Enter your city");
$state = check_input($_POST['state'], "Enter your state");
$zip code = check_input($_POST['zip code'], "Enter your zip code");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone'], "Enter your phone number");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}

/* Let's prepare the message for the E-mail */
$message = "Hello!

Your contact form has been submitted by:

Name: $name
E-mail: $email

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks_cma_request.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<b>Please correct the following error:</b>

<?php echo $myError; ?>

</body>
</html>
<?php
exit();
}
?>

Re: Parse error: syntax error, unexpected T_STRING

Posted: Thu Jun 30, 2011 12:54 pm
by LSanz
Thanks Greg, I truly appreciate your reply's ("tips").....have a great day!
Linda