Forum Moderators: coopster

Message Too Old, No Replies

PHP Contact form

code summary of contact form

         

w9914420

2:23 am on Mar 21, 2005 (gmt 0)

10+ Year Member



hi all

well I have managed to create my contact page form which works better than I expected
here is a sample of the code.

PHP code:

<?php
$content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris vel magna. Mauris risus nunc, tristique varius, gravida in, lacinia vel, elit. Nam ornare, felis non faucibus molestie, nulla augue adipiscing mauris, a nonummy diam ligula ut risus. Praesent varius. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';
?>
<p><?=$content;?></p>

<?php
$errmsg = ''; // error message
$errmsg2 = '';// error heading
$name = ''; // sender's name
$company = ''; // sender's company name
$telephone = ''; // senders telephone number
$email = ''; // sender's email address
$message = ''; // the message itself
$browser = $HTTP_USER_AGENT;
$ip = $REMOTE_ADDR;

if(isset($_POST['send']))
{
$name = $_REQUEST['name'] ;
$company = $_REQUEST['company'] ;
$telephone = $_REQUEST['telephone'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

if(trim($name) == '')
{
$errmsg2 = 'ERROR';
$errmsg = 'Please enter your name';
}
else if(trim($company) == '')
{
$errmsg2 = 'ERROR';
$errmsg = 'Please enter the name of your company';
}
else if(trim($telephone) == '')
{
$errmsg2 = 'ERROR';
$errmsg = 'Please enter a contact number';
}
else if(trim($email) == '')
{
$errmsg2 = 'ERROR';
$errmsg = 'Please enter your email address';
}
else if(!isEmail($email))
{
$errmsg2 = 'ERROR';
$errmsg = 'Your email address is not valid';
}
else if(trim($message) == '')
{
$errmsg2 = 'ERROR';
$errmsg = 'Please enter your message';
}
else if($errmsg == '')
{
$content = 'cool';
}
if($errmsg == '')
{

if(get_magic_quotes_gpc())
{
$company = stripslashes($company);
$telephone = stripslashes($telephone);
$subject = stripslashes($subject);
$message = stripslashes($message);

}

well this is what I have so far. the validation works has expected but I want to do one more thing if possible I want the $content value to change to the word cool after the from is processed or if the value of $errmsg = '';

being playing with it for a while and still no joy. Can it be done.

all help is welcome

regards

w9914420

dreamcatcher

8:27 am on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi w9914420, welcome to WebmasterWorld :)

What isn`t happening? Is the valid not being set? You can use the unset() function to clear the $content variable before you assign a new value.

Also, with your error checking being an if else statement only one value will ever show. So, if someone enters no name AND no e-mail address you will only see the one error message. Might be a good idea to add each value to an array, then loop through it afterwards.

One final thing, check for a valid e-mail address. If you are just checking for a blank field, someone could just type 'a' and it would process. Ok, so by checking for a valid e-mail address its not saying someone is actually going to enter their actual e-mail address, but at least they will have to enter an e-mail address.

Might also be a good idea to have an option like 'If you want a reply, please enclose a valid e-mail address'.

As for the error checking in an array, something like this is what I would do:


$errmsg = array();

if(trim($name) == '')
{
$errmsg[] = 'Please enter your name';
}
if(trim($company) == '')
{
$errmsg[] = 'Please enter the name of your company';
}
if(trim($telephone) == '')
{
$errmsg[] = 'Please enter a contact number';
}

and so on..then do a check afterwards to see if the array holds any values:

if (!empty($errmsg))
{
foreach ($errmsg as $error)
{
echo $error . "<br>\n";
}
}

Hope that helps.

dc

w9914420

2:05 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



hi dream catcher thanks for the heads up on the php.

I do have a email validator included in the script, its just that the function is declared further down the script.

as for the unset function I tried to implement that but Im a bit confused for example

<?php
function destroy_foo()
{
global $foo;
unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo;
?>

now this comes into action when the script is executed and it does work, my problem is targeting my $content varible. so for example

i would write.

<?php
function destroy_content()
{
global $content;
unset($content);
}

$content = 'u have been nuked';
destroy_content();
echo $content;
?>

This i guess needs to be placed at the bottom after all the if statments have been cleared and the form as processed.

dreamcatcher

2:29 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In your latest example, you are setting the $content variable, then destroying it, then echoing it, so it will hold no value.

Should be something like:

$content = 'blah blah';
destroy_content();
$content = 'u have been nuked';
echo $content;

dc

w9914420

5:28 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



[code]<?php
$content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris vel magna. Mauris risus nunc, tristique varius, gravida in, lacinia vel, elit. Nam ornare, felis non faucibus molestie, nulla augue adipiscing mauris, a nonummy diam ligula ut risus. Praesent varius. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';
?>
<p><?=$content;?></p>

<?php

$errmsg = ''; // error message
$errmsg2 = '';// error heading
$name = ''; // sender's name
$company = ''; // sender's company name
$telephone = ''; // senders telephone number
$email = ''; // sender's email address
$message = ''; // the message itself
$browser = $HTTP_USER_AGENT;
$ip = $REMOTE_ADDR;

if(isset($_POST['send']))
{
$name = $_REQUEST['name'] ;
$company = $_REQUEST['company'] ;
$telephone = $_REQUEST['telephone'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

if(trim($name) == '')
{
$errmsg2 = 'ERROR!';
$errmsg = 'Please enter your name.';

}
else if(trim($company) == '')
{
$errmsg2 = 'ERROR!';
$errmsg = 'Please enter the name of your company.';
}
else if(trim($telephone) == '')
{
$errmsg2 = 'ERROR!';
$errmsg = 'Please enter a contact number.';
}
else if(trim($email) == '')
{
$errmsg2 = 'ERROR!';
$errmsg = 'Please enter your email address.';
}
else if(!isEmail($email))
{
$errmsg2 = 'ERROR!';
$errmsg = 'Your email address is not valid.';
}
else if(trim($message) == '')
{
$errmsg2 = 'ERROR!';
$errmsg = 'Please enter your message.';
}
if($errmsg == '')
{

if(get_magic_quotes_gpc())
{
$company = stripslashes($company);
$telephone = stripslashes($telephone);
$subject = stripslashes($subject);
$message = stripslashes($message);

}



mail( "someone.co.uk", "Contact page request",
"Name of the company\n$company\n\nTel: number\n$telephone\n\nThe message left was...\n$message\n\nBrowser\n$browser\n\nIP Address\n$ip",
"From: $name <$email>" );
?>
<?php
function destroy_content();
{
global $content;
unset($content);
}

$content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris vel magna. Mauris risus nunc, tristique varius, gravida in, lacinia vel, elit. Nam ornare, felis non faucibus molestie, nulla augue adipiscing mauris, a nonummy diam ligula ut risus. Praesent varius. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';
destroy_content();
$content = 'u have been nuked';
echo $content;
?>
<h3>THANK YOU!</h3>
<p><span>Your message has been sent.</span></p>
<?php
}
}[code]

----------------------------------------------------

thanks again dream catcher.

this is how the code looks at the momement, the $content variable does not change the <p><?=$content;?></p> which is what im wanting to change. I m still a bit lost as to where best to edit my code.

any thoughts?

dreamcatcher

9:04 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have short tags enabled on your server?

<p><?=$content;?></p>

Try:

<p><?php $content;?></p>

dc

w9914420

10:04 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



hi dreamcather

thanks again for responding

i did a quick test and my server does not have the short tags available. from your experience would you say that may code should work. Im gonna have to consider another way of doing this if that is the case.

dreamcatcher

11:30 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don`t have short tags enabled, then you need to use <?php. Using <?= will not work.

Sorry. Why not use the <?php syntax?

w9914420

11:51 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



dream catcher im sorry, I do have shorttags enabled i have tried using that php syntax before and it has not worked. would it be too much for me to send you my code so that you can have a proper look at it.

dreamcatcher

10:06 am on Mar 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, sticky it to me, or use the e-mail address in my profile.

dc