trim function issues


Posted on 16th Feb 2014 07:03 pm by admin

Hi guys, total noob here...

So I've been tinkering around with a html and am using php to email the subitted data to me. It's all working fine, except now I've changed the rules of the form a little and I can't seem to make it do a very simple task of converting field data to a string so that I can have it in the body of the email.

Here's the code:

<?php

//If the form is submitted
if(isset($_POST['submit'])) {

//Check to make sure that the name field is not empty
if(trim($_POST['name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['name']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}

//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'mickyginger@googlemail.com'; //Put your own email address here
$subject = 'Interest from the website';
$body = $name . $email;
$headers = 'From:' .$name. '<'.$email.'>' . "rn" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>

Which works fine and dandy, only there are other fields like 'date' and 'venue' which I want to add to the body of the email regardless of whether they have been left blank or not.

Basically I want it to do something like this:

If the form is submitted

if name is not empty
$name = trim($_POST['name']);

if email is not empty
$email = trim($_POST['name']);

if number is empty OR if not
$number = trim($_POST['number']);

if date is empty OR if not
$date = trim($_POST['date']);

if there is no error
$body = $name . $email . $number . $date

seems like it should be really simple but I've tried everything I can think of with no joy...

No comments posted yet

Your Answer:

Login to answer
173 Like 28 Dislike
Previous forums Next forums
Other forums

Problem related to Creation of PDF File?
Hi All,
I am facing a problem related to creation pdf file. when I am creating a pdf file of do

Production of mango in processing industry
Hi,

We have one scenario ,company is in process industry,they are manufacturing MANGO pul

Help with translating C code into assembler code
Hi im doing a project that moves a robot around a maze avoiding walls and need some help with conver

SAP BCS. BPS
Hi all,

I am seeking reading note on the following in BW

BCS, BPS
ADVANCE

Delete all from a table
I can not get the following code to work. If I run the sql statement in the db it will delete all t

Placing and array within an array then sorting it!
I have a page that runs two large mysql queries and saves the results into arrays, in php I then per

Calling All PHPFREAKS Blackberry Users
Are you a member of PHPFreaks and have a blackberry? Well RIM just recently released Blackberry Mes

When i am Canseling the Billing Document in VF11 I am getting Shortdump
Hi Experts

When i am Canseling the Billing Document in VF11 I am getting Shortdump.

rookie looking for help coding a CSS form with PHP
I'm trying to figure out how to add PHP code to my xhtml form so that it is a working form embedded

PHP, Arrays & A Lot Of Confusion
I've been doing basic PHP work on sites for a few years now, but I've never been tempted to learn ho

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash