Strings in PHP mail form

21 views
Skip to first unread message

Daniely W

unread,
Jul 17, 2013, 3:57:23 AM7/17/13
to xamp...@googlegroups.com
I have a PHP mail form and I'm trying to get it to send a certain way. Here is my PHP code:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$tel2 = $_POST['tel2'];
$tel3 = $_POST['tel3'];
$message = $_POST['message'];
$message = wordwrap($message, 70, "\r\n");

$mail_to = 'mye...@mydomain.com';
$subject = 'Message from site visitor, '.$name;

$body_message = 'From: '.$name."\n";
$body_message .= 'Email: '.$email."\n";
$body_message .= 'Phone number: '.$tel . $tel2 . $tel3."\n";
$body_message .= 'Message: '.$message;

$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        // alert('Thank you for contacting us. We will get back to you shortly.');
        window.location = 'thankyou.php';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        // alert('Message failed. Please, send an email to mye...@mydomain.com');
        window.location = 'mailerror.php';
    </script>
<?php
}
?>

What I'm trying to do is have the phone number sent from the form's 3 phone number input fields in this format:

(123) 456-7890

How do I put the parentheses and hyphen in the PHP code without getting a T_CONSTANT_ENCAPSED_STRING error?

vikas dwivedi

unread,
Jul 17, 2013, 4:36:08 AM7/17/13
to xamp...@googlegroups.com
I don't think you will get any error with this code if you use parentheses and hyphen.'

If you are facing any error message then please paste here, and share your form 
through which you are posting.


regards !
vikas dwivedi 



--
Ask New Question from : https://groups.google.com/forum/?fromgroups#!newtopic/xamppdev
 
Create filter of mails from this group http://www.wikihow.com/Create-a-Filter-in-Gmail
---
You received this message because you are subscribed to the Google Groups "PHP Developers' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xamppdev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xamppdev/e5aa7ffa-ca86-41b0-ab07-7ed68b37dc08%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Daniely W

unread,
Jul 17, 2013, 5:25:23 AM7/17/13
to xamp...@googlegroups.com
Thanks for the response!

I tried placing parentheses around $tel and a hyphen between $tel2 and $tel3, that's when I get this error when trying to submit the form:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in sendmail.php on line 16


Here's the form code:

<form class="form-horizontal" action="sendmail.php" method="post">

    <div class="control-group">
        <label class="control-label" for="name">Name<span class="required">*</span></label>
        <div class="controls">
            <input class="span3" type="text" name="name" placeholder="Full name" required>
        </div>
    </div>

    <div class="control-group">
        <label class="control-label" for="email">Email<span class="required">*</span></label>
        <div class="controls">
            <input class="span3" type="email" name="email" placeholder="em...@example.com" required>
        </div>
    </div>

    <div class="control-group">
        <label class="control-label" for="name">Phone</label>
        <div class="controls">
            (<input class="span1" type="tel" name="tel" placeholder="555" maxlength="3">)
            <input class="span1" type="tel" name="tel2" placeholder="555" maxlength="3"> -
            <input class="span1" type="tel" name="tel3" placeholder="5555" maxlength="4">
        </div>
    </div>

    <div class="control-group">
        <label class="control-label" for="message">Message<span class="required">*</span></label>
        <div class="controls">
            <textarea class="span3" rows="5" name="message" placeholder="Your message" required></textarea>
        </div>
    </div>

    <div class="form-actions">
        <button type="submit" class="btn btn-success">Submit</button>
        <button type="reset" class="btn">Reset</button>
    </div>
</form>

Where and how exactly do I place the parentheses and hyphen in the PHP code?

vikas dwivedi

unread,
Jul 17, 2013, 5:47:02 AM7/17/13
to xamp...@googlegroups.com
from my end it's working ok.. unable to reproduce the problem.

regards !
vikas dwivedi 


--
Ask New Question from : https://groups.google.com/forum/?fromgroups#!newtopic/xamppdev
 
Create filter of mails from this group http://www.wikihow.com/Create-a-Filter-in-Gmail
---
You received this message because you are subscribed to the Google Groups "PHP Developers' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xamppdev+u...@googlegroups.com.

Daniely W

unread,
Jul 17, 2013, 6:09:11 AM7/17/13
to xamp...@googlegroups.com
It works when line 16 has:


$body_message .= 'Phone number: '.$tel . $tel2 . $tel3."\n";

But, when I change it and put the parentheses and hyphen in like this:

$body_message .= 'Phone number: '.'('$tel')' . $tel2'-'. $tel3."\n";

THAT's when I get the error. What am I doing wrong?

vikas dwivedi

unread,
Jul 17, 2013, 6:33:59 AM7/17/13
to xamp...@googlegroups.com
Okay Please correct your code as in green highlighted.

$body_message .= 'Phone number: '.'('$tel')' . $tel2'-'. $tel3."\n";

$body_message .= 'Phone number: '.'('.$tel.')' . $tel2.'-'. $tel3."\n";
OR
$body_message .= "Phone number: ({$tel}) {$tel2} - $tel3 \n";

PS: double quotes parse the variables.

regards !
vikas dwivedi 


Ashish Choudhary

unread,
Jul 17, 2013, 6:35:58 AM7/17/13
to xamp...@googlegroups.com
Absolutely right Vikas Sir,
There was a concatenation issue with it. It will work fine now Dani.


Daniely W

unread,
Jul 17, 2013, 6:55:36 AM7/17/13
to xamp...@googlegroups.com
Thank you, it works!

I put in an extra space with the code you gave, like this:

$body_message .= 'Phone number: '.'('.$tel.')' . ' '. $tel2.'-'. $tel3."\n";

I appreciate your help very much!!

Daniely W

unread,
Jul 17, 2013, 6:57:28 AM7/17/13
to xamp...@googlegroups.com
Yes Ashish, it works great now!
Reply all
Reply to author
Forward
0 new messages