...

View Full Version : Php contact form, not attaching data



DemonicB
06-01-2007, 07:39 PM
So I'm trying to make a simple contact form...
My php knowledge is really basic, and almost non-existant.
So I looked for a free script and adjusted it... But it seems something is nothing going as it's supposed to...
Could somebody help me out with this? :)

I was told my second php file (sendeail.php, isn't receiving the data as it should)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Form </title>
</head>
<body>

<form method="post" action="sendeail.php">

<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />

All fields are required to be filled in!<br /><br />

Your Name: <br />
<input type="text" name="visitor" size="35" />
<br />
Your Email:<br />
<input type="text" name="visitormail" size="35" />
<br />
Your Gamename:<br />
<input type="text" name="gamename" size="35" />
<br />
Your Age:<br />
<input type="text" name="age" size="2" />
<br />
Your sex:<br />
<select name="sex" size="1">
<option value=" Male ">Male </option>
<option value=" Female ">Female </option>
</select>
<br />
Your favourite class:<br />
<select name="class" size="1">
<option value=" Engineer ">Engineer </option>
<option value=" Medic ">Medic </option>
<option value=" Field Ops ">Field Ops </option>
<option value=" Covert Ops ">Covert Ops </option>
<option value=" Soldier ">Soldier </option>
</select>
<br />

Your favourite class:<br />
<select name="class" size="1">
<option value=" Engineer ">Engineer </option>
<option value=" Medic ">Medic </option>
<option value=" Field Ops ">Field Ops </option>
<option value=" Covert Ops ">Covert Ops </option>
<option value=" Soldier ">Soldier </option>
</select>

Why should we let you join KMD?:
<br />
<textarea name="notes" rows="4" cols="40"></textarea>
<br />
<input type="submit" value="Send Mail" />
</form>

</body>
</html>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>


<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes) || empty($gamename) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

$todayis = date("l, F j, Y, g:i a") ;

$sex = $sex;
$class = $class;
$subject = "Application";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
From: $visitor ($visitormail)\n
Gamename: $gamename \n
Age: $age \n
Sex: $sex \n
Favourite class: $class
Why we should let him in KMD: $notes \n


Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $visitormail\r\n";


mail("myemail@gmail.com", $subject, $message, $from);

?>

<p align="center">
Date: <?php echo $todayis ?>
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> )
<br />

Attention: <?php echo $attn ?>
<br />
Message:<br />
<?php $notesout = str_replace("\r", "<br/>", $notes);
echo $notesout; ?>
<br />
<?php echo $ip ?>

<br /><br />
<a href="contact.php"> Next Page </a>
</p>

</body>
</html>

I've uploaded what I've got now here: http://www.ilfiorfiore.be/kmdtest/contact.php

All help appreciated :)
Thanks

ddanatzko
06-01-2007, 07:51 PM
You're not pulling the data from the $_POST array. So, if you need to get these values (Notes, Visitor, Visitormail, etc.) you have to have some way of referencing them in the page you're sending the "post" to. So, Here's an easy way of determining which variables that you're receiving on the page.

in sendeail.php code in:

print_r($_POST).

This will print all of the values sent to this page from your form. Then you can see how you need to reference the variables in your sendeail.php script

DemonicB
06-01-2007, 09:10 PM
Like I said, my knowledge of PHP is really limited. Could someone give me a solid example of how I call upon the data in $_POST, using my code, so I can do the rest myself?

Thanks in advance...

ddanatzko
06-01-2007, 09:39 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>


<?php
// Begin Display of $_POST data
print_r($_POST);
echo "<br /><br />";
// End

// The above code will display an array of the variables that were passed to this page in the superglobal $_POST.
// If you add this to your code, and post the results from Array ( ... )
// I can tell you what you're missing

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes) || empty($gamename) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

$todayis = date("l, F j, Y, g:i a") ;

$sex = $sex;
$class = $class;
$subject = "Application";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
From: $visitor ($visitormail)\n
Gamename: $gamename \n
Age: $age \n
Sex: $sex \n
Favourite class: $class
Why we should let him in KMD: $notes \n


Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $visitormail\r\n";


mail("myemail@gmail.com", $subject, $message, $from);

?>

<p align="center">
Date: <?php echo $todayis ?>
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> )
<br />

Attention: <?php echo $attn ?>
<br />
Message:<br />
<?php $notesout = str_replace("\r", "<br/>", $notes);
echo $notesout; ?>
<br />
<?php echo $ip ?>

<br /><br />
<a href="contact.php"> Next Page </a>
</p>

</body>
</html>

ddanatzko
06-01-2007, 09:46 PM
The quickest way to reference the variables passed from the $_POST is like so:

$visitor = $_POST['visitor'];

that's assuming that there is piece of code in your form with the name="visitor"

same applies for any other named input

DemonicB
06-01-2007, 09:49 PM
Array ( [ip] => 81.240.210.192 [httpref] => [httpagent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4 [visitor] => Bernd [visitormail] => something@something.com [gamename] => threri [age] => 19 [sex] => Male [class] => Field Ops [notes] => fgfg )

thanks for helping man :)

DemonicB
06-01-2007, 10:02 PM
Hmmm...


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>


<?php
// Begin Display of $_POST data
print_r($_POST);
echo "<br /><br />";
// End
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$gamename = $_POST['gamename'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$class = $_POST['class'];
$notes = $_POST['notes'];

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
if(empty($visitor) || empty($visitormail) || empty($notes) || empty($gamename) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}

$todayis = date("l, F j, Y, g:i a") ;

//$sex = $sex;
//$class = $class;
$subject = "Application";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
From: $visitor ($visitormail)\n
Gamename: $gamename \n
Age: $age \n
Sex: $sex \n
Favourite class: $class
Why we should let him in KMD: $notes \n


Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $visitormail\r\n";


mail("*********@gmail.com", $subject, $message, $from);

?>

<p align="center">
Date: <?php echo $todayis ?>
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> )
<br />

Attention: <?php echo $attn ?>
<br />
Message:<br />
<?php $notesout = str_replace("\r", "<br/>", $notes);
echo $notesout; ?>
<br />
<?php echo $ip ?>

<br /><br />
<a href="sendeail.php"> Next Page </a>
</p>

</body>
</html>

right, so that's what I did for all the variables... But it's still not doing anything, did I missinterpret what you said?

http://www.ilfiorfiore.be/kmdtest/contact.php

ddanatzko
06-01-2007, 10:02 PM
so the easiest way to read the output is like this:



Array
(
[ip] => 81.240.210.192
[httpref] =>
[httpagent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8.1.4) Gecko/20070515 firefox/2.0.0.4
[visitor] => Bernd
[visitormail] => something@something.com
[gamename] => threri
[age] => 19
[sex] => Male
[class] => Field Ops
[notes] => fgfg
)


The sqaure brackets [] contain the name of the Key and the arrow, => points to the value. so its

Key => Value

the Key will be visitor in --- <input type="text" name="visitor" />---whereas the value will be what is submitted by the user, unless a value is specified in the input tag on the form

so if you want to reference the variables passed from your form, you can do one of two things:

1. use $_POST['key'] where key is the name of the input on the form...or
2. assign the $_POST['key'] to another variable in the beginning of your script like I mentioned above --> $visitor = $_POST['visitor'];

With number 2, if you're referencing the variable multiple times, it just simplifies ref'ing that variable. Make Sense?

ddanatzko
06-01-2007, 10:05 PM
what are you getting in your message?

DemonicB
06-01-2007, 10:09 PM
Absolutely nothing, I'm getting a blank screen... :(
And no e-mail.

aedrin
06-01-2007, 10:14 PM
I'm getting a blank screen...



error_reporting(E_ALL);
ini_set("display_errors", true);

ddanatzko
06-01-2007, 10:15 PM
sorry dude, I gotta run out for a few hours. I'll check back a little later to see if you've fixed the problem or not. If not, I'll dig through your code then.

DemonicB
06-01-2007, 10:22 PM
Thanks you two!
But there seems to be a problem with my empty field checking code.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>

<!-- Reminder: Add the link for the 'next page' (at the bottom) -->
<!-- Reminder: Change 'YourEmail' to Your real email -->

<?php
// Begin Display of $_POST data
print_r($_POST);
echo "<br /><br />";
// End
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$gamename = $_POST['gamename'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$class = $_POST['class'];
$notes = $_POST['notes'];
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];

error_reporting(E_ALL);
ini_set("display_errors", true);

//if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
//{
//echo "<h2>Use Back - Enter valid e-mail</h2>\n";
//$badinput = "<h2>Feedback was NOT submitted</h2>\n";
//echo $badinput;
//}
//if(empty($visitor) || empty($visitormail) || empty($notes) || empty($gamename) {
//echo "<h2>Use Back - fill in all fields</h2>\n";
//}

$todayis = date("l, F j, Y, g:i a") ;

//$sex = $sex;
//$class = $class;
$subject = "Application";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
From: $visitor ($visitormail)\n
Gamename: $gamename \n
Age: $age \n
Sex: $sex \n
Favourite class: $class
Why we should let him in KMD: $notes \n


Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $visitormail\r\n";


mail("ramsbernd@gmail.com", $subject, $message, $from);

?>

<p align="center">
Date: <?php echo $todayis ?>
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> )
<br />

Attention: <?php echo $notes ?>
<br />
Message:<br />
<?php $notesout = str_replace("\r", "<br/>", $notes);
echo $notesout; ?>
<br />
<?php echo $ip ?>

<br /><br />
<a href="sendeail.php"> Next Page </a>
</p>

</body>
</html>

It works like that, but you're able to send empty fields... And that's not exactly what I'm trying to achieve.
I do get an e-mail now, which is great...

So, any suggestions for me?
Thanks for that error checking code, fixed some minor errors I had.

ddanatzko
06-01-2007, 11:39 PM
try this and see if it works



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>

<!-- Reminder: Add the link for the 'next page' (at the bottom) -->
<!-- Reminder: Change 'YourEmail' to Your real email -->

<?php
// Begin Display of $_POST data
print_r($_POST);
echo "<br /><br />";
// End
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$gamename = $_POST['gamename'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$class = $_POST['class'];
$notes = $_POST['notes'];
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];

error_reporting(E_ALL);
ini_set("display_errors", true);

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
}
elseif(empty($visitor) || empty($visitormail) || empty($notes) || empty($gamename) {
echo "<h2>Use Back - fill in all fields</h2>\n";
}
else{
$todayis = date("l, F j, Y, g:i a") ;

//$sex = $sex;
//$class = $class;
$subject = "Application";

$notes = stripcslashes($notes);

$message = " $todayis [EST] \n
From: $visitor ($visitormail)\n
Gamename: $gamename \n
Age: $age \n
Sex: $sex \n
Favourite class: $class
Why we should let him in KMD: $notes \n


Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";

$from = "From: $visitormail\r\n";


mail("ramsbernd@gmail.com", $subject, $message, $from);

?>

<p align="center">
Date: <?php echo $todayis ?>
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> )
<br />

Attention: <?php echo $notes ?>
<br />
Message:<br />
<?php $notesout = str_replace("\r", "<br/>", $notes);
echo $notesout; ?>
<br />
<?php echo $ip ?>

<br /><br />
<a href="sendeail.php"> Next Page </a>
</p>

</body>
</html>
<? }
?>

DemonicB
06-01-2007, 11:50 PM
Nope, gives me a blank page again :/ so weird...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum