Simple Forms with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ameesas
Forum Newbie
Posts: 14
Joined: Thu Feb 12, 2004 5:43 pm
Location: Nevada City

Simple Forms with php

Post by ameesas »

Hello,
I am trying to get a simple form to work on my website. I used the tutorial off of phpfreaks to create a form. I can see the form on my website, and I have the backend php to perform the POST method, but when I fill out the form, the page goes blank and nothing happens. Where am I supposed to place the form and the .php to get it to work?
Talk to me like I am a 1st grader, I am pretty new at this.

Thanks to anyone for your help.

http://www.ameechapman.com to check out the form (go to contact)
I will change it to suit me as soon as it is working...
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

It looks liek you have everythign setup ok, with the form action. So it must be something in your contact.php. Post that and we can help ya.
ameesas
Forum Newbie
Posts: 14
Joined: Thu Feb 12, 2004 5:43 pm
Location: Nevada City

Post by ameesas »

Ok, here is the php called contact.php
THANKS!


<html>
<head>
</head>
<body>

<?php
/* grabs the POST variables and puts them into variables that we can use */
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$company=$_POST['company'];
$email=$_POST['email'];
$website=$_POST['website'];
$countryCode=$_POST['countryCode'];
$phone=$_POST['phone'];
$phoneExt=$_POST['phoneExt'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$zipCode=$_POST['zipCode'];
$heardAbout=$_POST['heardAbout'];
$inquiring=$_POST['inquiring'];

//---------VALIDATION-------->
if($firstName){//----> CHECK input
}
else{
$error.="Please, go back and fill out your first name<br>\n";//----> ERROR if no input
}

if($lastName){//----> CHECK input
}
else{
$error.="Please, go back and fill out your last name<br>\n";//----> ERROR if no input
}

if($email){//----> CHECK input
}
else{
$error.="Please, go back and fill out your e-mail address<br>\n";//----> ERROR if no input
}

if($phone){//----> CHECK input
}
else{
$error.="Please, go back and fill out your phone number<br>\n";//----> ERROR if no input
}

if($address){//----> CHECK input
}
else{
$error.="Please, go back and fill out your mailing address<br>\n";//----> ERROR if no input
}

if($city){//----> CHECK input
}
else{
$error.="Please, go back and fill out your city name<br>\n";//----> ERROR if no input
}

if($zipCode){//----> CHECK input
}
else{
$error.="Please, go back and fill out your zip code<br>\n";//----> ERROR if no input
}
//-------->ERROR FREE??
if($error==""){
echo "Thank you for inquiring about us! A receipt of your submission will be e-mailed to you almost

immediately.";
//----------------------------------
$mailContent="--------CONTACT--------\n"
."First Name: ".$firstName."\n"
."Last Name: ".$lastName."\n"
."Company: ".$company."\n"
."E-mail: ".$email."\n"
."Website: ".$website."\n\n--------PHONE--------\n"
."Phone: ".$countryCode." ".$phone."\n"
."Extension: ".$phoneExt."\n"
."Fax: ".$fax."\n"
."Mobile: ".$mobile."\n\n--------ADDRESS--------\n"
."Street Address: ".$address."\n"
."City: ".$city."\n"
."State: ".$state."\n"
."Country: ".$country."\n"
."Zip Code: ".$zipCode."\n\n--------INFO--------\n"
."Where did you hear about us? ".$heardAbout."\n"
."Inquiring About: ".$inquiring."\n"
//----------------------------------
$toAddress="ameechapmanband@hotmail.com"; /* change this! */
$subject="Website Contact"; /* change this! */
$recipientSubject="Amee Chapman's Contact Form"; /* change this! */
$receiptMessage = "Thank you ".$firstName." for inquiring about Amee Chapman!\n\n\nHere is what you submitted to

us:\n\n"
."--------CONTACT--------\n"
."First Name: ".$firstName."\n"
."Last Name: ".$lastName."\n"
."Company: ".$company."\n"
."E-mail: ".$email."\n"
."Website: ".$website."\n\n--------PHONE--------\n"
."Phone: ".$countryCode." ".$phone."\n"
."Extension: ".$phoneExt."\n"
."Fax: ".$fax."\n"
."Mobile: ".$mobile."\n\n--------ADDRESS--------\n"
."Street Address: ".$address."\n"
."City: ".$city."\n"
."State: ".$state."\n"
."Country: ".$country."\n"
."Zip Code: ".$zipCode."\n\n--------INFO--------\n"
."Where did you hear about us? ".$heardAbout."\n"
."Inquiring About: ".$inquiring."\n"
//----------------------------------
mail($email, $subject, $receiptMessage,"From:$toAddress");
//----------------------------------
mail($toAddress,$recipientSubject,$mailContent,"From:$email");
//--->echo $mailContent;

//////////////////////////////////////// CONNECT TO MYSQL DB ////////////////////
// OPEN CONNECTION --->
$connection=mysql_connect("www.hyberia.net","amee", "chapman") or die("Unable to connect!"); /* change this! */

mysql_select_db("generalContact.sql") or die("Unable to select database!"); /* change this! */

// EXECUTE QUERY --->
$query="INSERT INTO generalContact (
firstName,
lastName,
company,
email,
website,
countryCode,
phone,
phoneExt,
mobile,
fax,
address,
city,
state,
country,
zipCode,
heardAbout,
inquiringOn)
VALUES(
'".$firstName."',
'".$lastName."',
'".$company."',
'".$email."',
'".$website."',
'".$countryCode."',
'".$phone."',
'".$phoneExt."',
'".$mobile."',
'".$fax."',
'".$address."',
'".$city."',
'".$state."',
'".$country."',
'".$zipCode."',
'".$heardAbout."',
'".$inquiring."')";
//////----->
$result=mysql_query($query) or die("Error in query:".mysql_error());
//if ($result)
//echo mysql_affected_rows()." row inserted into the database effectively.";

// CLOSE CONNECTION --->
mysql_close($connection);

///////////////////////////////////////////////////////////////////////////////////
}
else{

print "Sorry, but the form cannot be sent until the fields indicated are filled out completely -

<br>\n";
print "$error<br>\n";
print "<br>\n";
print "<br>\n";
print "Please use your \"Back\" button to return to the form to correct the omissions. Thank

you.<br>\n";
}

?>
</body>
</html>
ameesas
Forum Newbie
Posts: 14
Joined: Thu Feb 12, 2004 5:43 pm
Location: Nevada City

Post by ameesas »

Oh, by the way, I don't have MySql, but I didn't want to take that out until I knew what I was doing. I only want the response e-mailed to me, not put in a database (unless I can post it to a Notepad type thing that will keep all of my responses in one file).

Thanks
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

No changes, just making it more readable:

Code: Select all

<html>
<head>
</head>
<body>

<?php
/* grabs the POST variables and puts them into variables that we can use */
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$company=$_POST['company'];
$email=$_POST['email'];
$website=$_POST['website'];
$countryCode=$_POST['countryCode'];
$phone=$_POST['phone'];
$phoneExt=$_POST['phoneExt'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$zipCode=$_POST['zipCode'];
$heardAbout=$_POST['heardAbout'];
$inquiring=$_POST['inquiring'];

//---------VALIDATION-------->
if($firstName){//----> CHECK input
}
else{
$error.="Please, go back and fill out your first name<br>\n";//----> ERROR if no input
}

if($lastName){//----> CHECK input
}
else{
$error.="Please, go back and fill out your last name<br>\n";//----> ERROR if no input
}

if($email){//----> CHECK input
}
else{
$error.="Please, go back and fill out your e-mail address<br>\n";//----> ERROR if no input
}

if($phone){//----> CHECK input
}
else{
$error.="Please, go back and fill out your phone number<br>\n";//----> ERROR if no input
}

if($address){//----> CHECK input
}
else{
$error.="Please, go back and fill out your mailing address<br>\n";//----> ERROR if no input
}

if($city){//----> CHECK input
}
else{
$error.="Please, go back and fill out your city name<br>\n";//----> ERROR if no input
}

if($zipCode){//----> CHECK input
}
else{
$error.="Please, go back and fill out your zip code<br>\n";//----> ERROR if no input
}
//-------->ERROR FREE??
if($error==""){
echo "Thank you for inquiring about us! A receipt of your submission will be e-mailed to you almost

immediately.";
//----------------------------------
$mailContent="--------CONTACT--------\n"
."First Name: ".$firstName."\n"
."Last Name: ".$lastName."\n"
."Company: ".$company."\n"
."E-mail: ".$email."\n"
."Website: ".$website."\n\n--------PHONE--------\n"
."Phone: ".$countryCode." ".$phone."\n"
."Extension: ".$phoneExt."\n"
."Fax: ".$fax."\n"
."Mobile: ".$mobile."\n\n--------ADDRESS--------\n"
."Street Address: ".$address."\n"
."City: ".$city."\n"
."State: ".$state."\n"
."Country: ".$country."\n"
."Zip Code: ".$zipCode."\n\n--------INFO--------\n"
."Where did you hear about us? ".$heardAbout."\n"
."Inquiring About: ".$inquiring."\n"
//----------------------------------
$toAddress="ameechapmanband@hotmail.com"; /* change this! */
$subject="Website Contact"; /* change this! */
$recipientSubject="Amee Chapman's Contact Form"; /* change this! */
$receiptMessage = "Thank you ".$firstName." for inquiring about Amee Chapman!\n\n\nHere is what you submitted to

us:\n\n"
."--------CONTACT--------\n"
."First Name: ".$firstName."\n"
."Last Name: ".$lastName."\n"
."Company: ".$company."\n"
."E-mail: ".$email."\n"
."Website: ".$website."\n\n--------PHONE--------\n"
."Phone: ".$countryCode." ".$phone."\n"
."Extension: ".$phoneExt."\n"
."Fax: ".$fax."\n"
."Mobile: ".$mobile."\n\n--------ADDRESS--------\n"
."Street Address: ".$address."\n"
."City: ".$city."\n"
."State: ".$state."\n"
."Country: ".$country."\n"
."Zip Code: ".$zipCode."\n\n--------INFO--------\n"
."Where did you hear about us? ".$heardAbout."\n"
."Inquiring About: ".$inquiring."\n"
//----------------------------------
mail($email, $subject, $receiptMessage,"From:$toAddress");
//----------------------------------
mail($toAddress,$recipientSubject,$mailContent,"From:$email");
//--->echo $mailContent;

//////////////////////////////////////// CONNECT TO MYSQL DB ////////////////////
// OPEN CONNECTION --->
$connection=mysql_connect("www.hyberia.net","amee", "chapman") or die("Unable to connect!"); /* change this! */

mysql_select_db("generalContact.sql") or die("Unable to select database!"); /* change this! */

// EXECUTE QUERY --->
$query="INSERT INTO generalContact (
firstName,
lastName,
company,
email,
website,
countryCode,
phone,
phoneExt,
mobile,
fax,
address,
city,
state,
country,
zipCode,
heardAbout,
inquiringOn)
VALUES(
'".$firstName."',
'".$lastName."',
'".$company."',
'".$email."',
'".$website."',
'".$countryCode."',
'".$phone."',
'".$phoneExt."',
'".$mobile."',
'".$fax."',
'".$address."',
'".$city."',
'".$state."',
'".$country."',
'".$zipCode."',
'".$heardAbout."',
'".$inquiring."')";
//////----->
$result=mysql_query($query) or die("Error in query:".mysql_error());
//if ($result)
//echo mysql_affected_rows()." row inserted into the database effectively.";

// CLOSE CONNECTION --->
mysql_close($connection);

///////////////////////////////////////////////////////////////////////////////////
}
else{

print "Sorry, but the form cannot be sent until the fields indicated are filled out completely -

<br>\n";
print "$error<br>\n";
print "<br>\n";
print "<br>\n";
print "Please use your "Back" button to return to the form to correct the omissions. Thank

you.<br>\n";
}

?>
</body>
</html>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

try changing

Code: Select all

if($error==""){
to

Code: Select all

if(!$error){
that may/not work, but its worth a try
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Change the top of contact.php from ...

<?php
/* grabs the POST variables and puts them into variables that we can use */

to

<?php
error_reporting(E_ALL);
/* grabs the POST variables and puts them into variables that we can use */

and see if any warnings/notices show up that might give some clues.
ameesas
Forum Newbie
Posts: 14
Joined: Thu Feb 12, 2004 5:43 pm
Location: Nevada City

Post by ameesas »

I made those two changes, no errors came up when I made the changes, still no e-mail back when testing.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I'd start adding some debug echo's.
For example, make the first lines of contact.php like this (even before the html bit)
<?php
echo 'Debug 1<br />';
?>

Then start adding similar lines at various points further down the script, changing the Debug number everytime, 1..2..3 etc..
Then at least you should see how far the script is getting before it dies and see if you can pin down the exact line where it's happening.
ameesas
Forum Newbie
Posts: 14
Joined: Thu Feb 12, 2004 5:43 pm
Location: Nevada City

Post by ameesas »

Do I need to do this on the server side or on my local page? Will this debug my page? as I am working on it ie. Server markups?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If you add it to the server side contact.php then we can see the results too ;) This code will/should echo the Debug lines until the script dies, but finding out which line is causing the script to die we can probably find out what's wrong, or at least where to look :o
ameesas
Forum Newbie
Posts: 14
Joined: Thu Feb 12, 2004 5:43 pm
Location: Nevada City

Post by ameesas »

I'm sorry, I told you I was new at this. Trying to add the echos in and I am getting nowhere. Can you give me an example using some of the code above? I will past it in and we can try it.
Thanks.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

Code: Select all

<?php 
echo "Debug 1<br />";
?>
<html>
<head>
</head>
<body>

<?php
/* grabs the POST variables and puts them into variables that we can use */
$firstName=$_POST['firstName'];
$lastName=$_POST['lastName'];
$company=$_POST['company'];
$email=$_POST['email'];
$website=$_POST['website'];
$countryCode=$_POST['countryCode'];
$phone=$_POST['phone'];
$phoneExt=$_POST['phoneExt'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$country=$_POST['country'];
$zipCode=$_POST['zipCode'];
$heardAbout=$_POST['heardAbout'];
$inquiring=$_POST['inquiring'];

//---------VALIDATION-------->
if($firstName){//----> CHECK input
echo "Debug 2<br />";
}
else{
$error.="Please, go back and fill out your first name<br>\n";//----> ERROR if no input
}

if($lastName){//----> CHECK input
echo "Debug 3<br />";
}
else{
$error.="Please, go back and fill out your last name<br>\n";//----> ERROR if no input
}

if($email){//----> CHECK input
echo "Debug 4<br />";}
else{
$error.="Please, go back and fill out your e-mail address<br>\n";//----> ERROR if no input
}

if($phone){//----> CHECK input
echo "Debug 5<br />";}
else{
$error.="Please, go back and fill out your phone number<br>\n";//----> ERROR if no input
}

if($address){//----> CHECK input
echo "Debug 6<br />";}
else{
$error.="Please, go back and fill out your mailing address<br>\n";//----> ERROR if no input
}

if($city){//----> CHECK input
echo "Debug 7<br />";}
else{
$error.="Please, go back and fill out your city name<br>\n";//----> ERROR if no input
}

if($zipCode){//----> CHECK input
echo "Debug 8<br />";}
else{
$error.="Please, go back and fill out your zip code<br>\n";//----> ERROR if no input
}
//-------->ERROR FREE??
if(!$error){
echo "Thank you for inquiring about us! A receipt of your submission will be e-mailed to you almost

immediately.";
try taht and see if you get anything...
ameesas
Forum Newbie
Posts: 14
Joined: Thu Feb 12, 2004 5:43 pm
Location: Nevada City

Post by ameesas »

Page goes blank after hitting submit. Is this a weird problem or am I too much of a novice to deal with this?
Thanks again for your help.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, if that's the code that you can currently view by going to http://www.ameechapman.com/contact.php then something else is wrong as it's not even 'doing' the first debug echo.
Does a simple page, let's call it test.php with only the following code work on the server ok?

Code: Select all

<?php phpinfo() ?>
If so, can you provide a link to test.php so we can see it?
Post Reply