Forum Moderators: coopster
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
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
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.
<?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?