Hi, can anyone help me debug this piece of code please, any help will be much appreciated!

Its the body of a message im sending with mail() function..

Thanks,
Chris

Getting this error in FireBug..

Parse error: syntax error, unexpected '.' in /var/www/virtual/capetownalive.co.za/htdocs/bookings/scripts/xmlHttpRequest.php on line 28

$message = 'Cape Town Alive - Cape Xtreme Booking Request'
. 'Name:' . cleanPosUrl($_POST['posName']);
. 'Email:' . cleanPosUrl($_POST['posEmail']);
. 'Staying:' . cleanPosUrl($_POST['posStaying']);
. 'Country:' . cleanPosUrl($_POST['posCountry']);
. 'Contact:' . cleanPosUrl($_POST['posContact']);
. 'Actvity:' . cleanPosUrl($_POST['posActivity']);
. 'Comments:' . cleanPosUrl($_POST['posComments']); ';

Dani AI

Generated

The parse error "unexpected '.'" happens because the PHP parser found a concatenation operator at the start of a statement. In this thread had semicolons and a stray quote that prematurely ended the assignment, so subsequent lines that begin with a dot become syntax errors. correctly spotted the stray quote; steered the author toward proper quoting and concatenation. A safer pattern is to build the message from an array (no accidental leading dots) and sanitize/validate inputs before mailing.

Example pattern that avoids the common multiline-concatenation pitfall:

<?php
$fields = array(
  'Name'    => isset($_POST['posName'])    ? trim(strip_tags($_POST['posName']))    : '',
  'Email'   => isset($_POST['posEmail'])   ? trim($_POST['posEmail'])               : '',
  'Staying' => isset($_POST['posStaying']) ? trim(strip_tags($_POST['posStaying'])) : '',
  'Country' => isset($_POST['posCountry']) ? trim(strip_tags($_POST['posCountry'])) : '',
  'Contact' => isset($_POST['posContact']) ? trim(strip_tags($_POST['posContact'])) : '',
  'Activity'=> isset($_POST['posActivity'])? trim(strip_tags($_POST['posActivity'])): '',
  'Comments'=> isset($_POST['posComments'])? trim(strip_tags($_POST['posComments'])): '',
);

if (!filter_var($fields['Email'], FILTER_VALIDATE_EMAIL)) {
  $fields['Email'] = '';
}

$message = "Cape Town Alive - Cape Xtreme Booking Request\r\n" . implode("\r\n", array_map(
  function($k,$v){ return $k.': '.$v; }, array_keys($fields), $fields
));
?>

When sending HTML, set the proper MIME header; for plain text use CRLF ("\r\n"). For reference on string concatenation and mail headers see the PHP manual on the string operator (https://www.php.net/manual/en/language.operators.string.php) and mail() (https://www.php.net/manual/en/function.mail.php). Sanitize inputs and validate email addresses (https://www.php.net/manual/en/function.filter-var.php) before including them in messages.

Recommended Answers

All 4 Replies

I have put it in <> where the error is:
in the last line ' is not needed.check it.

$message = 'Cape Town Alive - Cape Xtreme Booking Request'
. 'Name:' . cleanPosUrl($_POST);
. 'Email:' . cleanPosUrl($_POST);
. 'Staying:' . cleanPosUrl($_POST);
. 'Country:' . cleanPosUrl($_POST);
. 'Contact:' . cleanPosUrl($_POST);
. 'Actvity:' . cleanPosUrl($_POST);
. 'Comments:' . cleanPosUrl($_POST); <'>;

Try it now...

Thanks for the quick response, getting this error now:

<b>Parse error</b>: syntax error, unexpected '.' in <b>/var/www/virtual/capetownalive.co.za/htdocs/bookings

/scripts/xmlHttpRequest.php</b> on line <b>33</b><br />

Changed it around a bit, rather look at this code:

$message = 'Cape Town Alive - Cape Xtreme Booking Request' <br> 'Name:' . cleanPosUrl($_POST['posName']); . 'Email:' . cleanPosUrl($_POST['posEmail']); . 'Staying:' . cleanPosUrl($_POST['posStaying']); . 'Country:' . cleanPosUrl($_POST['posCountry']); . 'Contact:' . cleanPosUrl($_POST['posContact']); . 'Actvity:' . cleanPosUrl($_POST['posActivity']); . 'Comments:' . cleanPosUrl($_POST['posComments']);

Try this.

$message = 
"Cape Town Alive - Cape Xtreme Booking Request <br> Name: ". cleanPosUrl($_POST['posName']) ."Email: ". cleanPosUrl($_POST['posEmail']) ."Staying: ". cleanPosUrl($_POST['posStaying']) ."Country:". cleanPosUrl($_POST['posCountry']) ."Contact: ". cleanPosUrl($_POST['posContact']) ."Actvity: ". cleanPosUrl($_POST['posActivity']) ."Comments: ". cleanPosUrl($_POST['posComments']);

Umm.. Why are you having ; at the end of every function call ? Moreover, <br> is a html tag and it should be in quotes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.