Jump to content

Recommended Posts

I am making a contact script and I put a code into this script where he can read the users login name and e-mail adress and that gives the error.

 

Here is the script: (He gives an syntax error on the last line)

<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
 ?>

Username:<input type="text" value="<?php echo  $_SESSION['Username'];?>" id="username"/><br/>
Email: <input type="text" value="<?php echo  $_SESSION['EmailAddress'];?>" id="email"/>
<?php
/* Set e-mail recipient */
$myemail  = "[email protected]";

/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");

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

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

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

Your contact form has been submitted by:

Name: $yourname
E-mail: $email
URL: $website

Like the website? $likeit
How did he/she find it? $how_find

Comments:
$comments

End of message
";

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

/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
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();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/242822-error-in-contact-script/
Share on other sites

your function show error at the end is using a variable named $myError - I cant see where, from the code given, that variable is actualy declaired.  Also - are you absoloutly certain you want to run exit(); from within the function (not that it's a problem, just something that's worth thinking about)?

 

P.S. - the ACTUAL error message is always appreciated  ;D

you have to edit this part

 

Code: [select]

 

/* Let's prepare the message for the e-mail */

$message = "Hello!

 

Your contact form has been submitted by:

 

Name: $yourname

E-mail: $email

URL: $website

 

Like the website? $likeit

How did he/she find it? $how_find

 

Comments:

$comments

 

End of message

";

 

to this

 

Code: [select]

/* Let's prepare the message for the e-mail */

$message = "Hello!

 

Your contact form has been submitted by:

 

Name: " . $yourname ."

E-mail: " . $email ."

URL:" . $website ."

 

Like the website? $likeit

How did he/she find it?" . $how_find ."

 

Comments:" .

$comments . "

 

End of message

@Muddy_Funster it's there, look on line ~25 and ~67

 

@ZulfadlyAshBurn that's not the problem, it is legal syntax to have variables inside a string, as long as it is enclosed by double quotes (which it is).

 

Here are the problems:

 

  • The syntax error is: unexpected end. This is because you have a missing closing brace } for the first if statement if($_SESSION...). I think you want to add the missing closing brace after the inputs but inside the <?php opening tag.
  • When the above is fixed, the next problem is that you're not initialising the session so you won't have access to $_SESSION. You should call session_start() at the top of the script.
  • The next problem is you have some inputs at the top, but they aren't wrapped in a form. The need a <form> tag with an action set to a processing script.

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.