...

View Full Version : Completely new to PHP



USMarineNCO
11-14-2006, 04:17 AM
I am probably going to make some people feel VERY smart here with this post. Even some of you complete newbies will feel smart with this post.

I'm building a site that has a form. I'm very used to Windows based hosting due to that being the majority of what I have used in the Marine Corps for the past 11 years. I'm now venturing into other avenues of hosting. Well needless to say the methods used in Windows based hosting is completely different than that of Unix based. On to the point here....My form is completely built and I need it to, on submit, send the data to the database and to the email user specified. Using ASP and MySQL in Windows, I could have written the code blind folded. PHP....someone is gonna need to smack me and hold my hand....I hope there is someone out there willing to give an old dumb Marine a hand!

Thanks in advance,
Carl

P.S. Form is at http://www.cwsenterprise.com/Quote.html it has QUITE a few fields!!

ez4me2c3d
11-14-2006, 05:19 AM
Let an ex Air Force guy help you...

You mentioned MySQL as being a strong point for you, so making the database is not an issue I will presume.

What I think you are looking for is how to take the form fields and format them into an INSERT command for SQL. Possibly even the commnads to connect to an SQL database. Oh an a simple e-mail thing too.

am i right?

SWG
11-14-2006, 05:50 AM
Holy crap... That's a lot of form fields. Ok, have you step your database? Like, have you placed a table to organize and manage the data?

If you haven't let me know... Otherwise, take a look at this...


<form method="post" action="action.php">
<input type="text" name="nameofinput">
<input type="submit" name="submit" value="Submit">
</form>

Ok, now the PHP code:


<?
if(!isset($_POST['submit'])) {
// The name of the submit button is "submit". This means if the submit button has not been clicked yet, show the form.
echo " THE FORM YOU HAVE ON THAT PAGE. ";
// You don't have to use echo... You can end the PHP and then enter the HTML of the form. Then start another PHP section with the rest of the code.
} else {
// If the form has been submitted, do this.
$input = $_POST['nameofinput'];
// The name of the form field is "nameofinput". This basically means $input will display the contents entered inside the form field "nameofinput".
$query = "INSERT INTO nameoftableindatabase (nameofcolumn) VALUES ('$input')";
// The query to insert the data into the database. "nameoftableindatabase" is where the name of the table you made in your database goes. It basically means insert into column values $input. And if $input is what is put into the form field it means whatever is in the form field needs to insert into the database.
$run_query = mysql_query($query);
}
?>

Ok. I explained that horribly... But let me know about the setup of your database... And I'll do a better job.

USMarineNCO
11-14-2006, 11:52 PM
I setup my database and the fields are below.
The table type is MyISAM. My web host allows me to use MyPHPAdmin so the table and fields setup was rather simple. All fields are VARCHAR.
Now....the hard part for me is getting the PHP code to make this form send all 64 fields to the database and to an e-mail.


InfoName
InfoEmail
InfoPhone
InfoPreferredContact
InfoFromZip
InfoToZip
InfoResideType
InfoRoomsNumber
InfoFloorsNumber
KitFridgeUpright
KitFridgeSidebySide
KitStove
KitMicrowaveOven
KitTable
KitChairs
KitOther
DinTable
DinChairs
DinBuffet
DinChinaCabient
DinOther
FamSofa
FamChairs
FamLoveSeat
FamEntertainment
FamStereoYN
FamPiano
FamOther
BedTwin
BedDouble
BedQueen
BedKing
BedBunk
BedWater
BedCrib
BedSingleDresser
BedDoubleDresser
BedTripleDresser
BedNightStand
BedArmoire
BedOther
OfficeDesk
OfficeChairs
OfficeFiling
OfficeSafe
OfficeCopier
OfficeComputers
OfficeOther
GarWorkbench
GarLadders
GarMower
GarGrill
GarWheelbarrow
GarOther
OutPatioTable
OutPatioUmbrella
OutOther
MiscAquarium
MiscGolf
MiscWDCombo
MiscWasher
MiscDryer
MiscVacuum
MiscOther

ez4me2c3d
11-15-2006, 02:28 AM
The guy above gave you a good starting point. Since you are so good at ASP and mySQL, I figure you know how to read a tech doc.

visit this page for inof on connecting to and interacting with MySQL:
http://us2.php.net/manual/en/ref.mysql.php
(scroll down a bit to get to the examples)

and visit this page for information on how to send an e-mail via PHP:
http://us2.php.net/manual/en/function.mail.php

USMarineNCO
11-15-2006, 02:37 AM
Anthony,

Reading those things makes me want to pull my eyes out of my a$$ and pi$$ on them to relieve the burn! I'll work on sorting through it in the morning to see if I can figure anything out. I'm suspecting the connect string is rather similar and that is probably the most important piece if you ask me!

Thanks,
Carl

CFMaBiSmAd
11-15-2006, 03:47 AM
Sending all form fields in an email body, as long as you are not picky about the format, is fairly easy using a foreach loop -

<?php
$message = ""; // create message variable
// The following loops through ALL of the POST data from a form and builds a message
foreach($_POST as $key => $val){
$message .= "Field - $key: Value: $val\n<br />"; // modify this line to get the format you want.
}
echo "$message"; // the $message variable would typically be put into the email body at this point...
?>
For the form at the link you posted in the 1st post here, the output would be as follows -

Field - InfoName: Value: some name
Field - InfoEmail: Value: someemail@someplace.com
Field - InfoPhone: Value: phone
Field - InfoPreferredContact: Value: contact method
Field - InfoFromZip: Value: from zip
Field - InfoToZip: Value: to zip
Field - InfoResideType: Value: House
Field - InfoRoomsNumber: Value: 1
Field - InfoFloorsNumber: Value: 1
Field - KitFridgeUpright: Value:
Field - KitFridgeSidebySide: Value:
Field - KitStove: Value:
Field - KitMicrowaveOven: Value:
Field - KitTable: Value:
Field - KitChairs: Value:
Field - KitOther: Value:
Field - DinTable: Value:
Field - DinChairs: Value:
Field - DinBuffet: Value:
Field - DinChinaCabient: Value:
Field - DinOther: Value:
Field - FamSofa: Value:
Field - FamChairs: Value:
Field - FamLoveSeat: Value:
Field - FamEntertainment: Value: None
Field - FamStereoYN: Value: No
Field - FamPiano: Value: None
Field - FamOther: Value:
Field - BedTwin: Value:
Field - BedDouble: Value:
Field - BedQueen: Value:
Field - BedKing: Value:
Field - BedBunk: Value:
Field - BedWater: Value:
Field - BedCrib: Value:
Field - BedSingleDresser: Value:
Field - BedDoubleDresser: Value:
Field - BedTripleDresser: Value:
Field - BedNightStand: Value:
Field - BedArmoire: Value:
Field - BedOther: Value:
Field - OfficeDesk: Value: None
Field - OfficeChairs: Value:
Field - OfficeFiling: Value: None
Field - OfficeSafe: Value: None
Field - OfficeCopier: Value: None
Field - OfficeComputers: Value:
Field - select: Value: None
Field - OfficeOther: Value:
Field - GarWorkbench: Value: None
Field - GarLadders: Value:
Field - GarMower: Value: None
Field - GarGrill: Value: None
Field - GarWheelbarrow: Value:
Field - select2: Value: None
Field - GarOther: Value:
Field - OutPatioTable: Value:
Field - OutPatioChairs: Value:
Field - OutPatioUmbrella: Value:
Field - OutOther: Value:
Field - MiscAquarium: Value: None
Field - MiscGolf: Value:
Field - MiscWDCombo: Value:
Field - MiscWasher: Value:
Field - MiscDryer: Value:
Field - MiscVacuum: Value:
Field - MiscOther: Value: Misc comment field
Field - Submit: Value: Submit

USMarineNCO
11-15-2006, 04:34 AM
Okay, The below code is something I've figured out from reading all sorts of things online this evening. 90% of learning is reading, and I think I have done enough of that for one night. I've ran the code and it does send the data to the database which is exactly what I needed. It also created an e-mail and sent it, however the e-mail contained NONE of the information from the form. Can someone tell me where I went wrong there? Again, My database is being fed now and that part of this is good to go. I appreciate all the help there. Only issue is the email being generated is not showing data.

ProcessQuote.php


<?php

$InfoName=$_POST['InfoName'];
$InfoEmail=$_POST['InfoEmail'];
$InfoPhone=$_POST['InfoPhone'];
$InfoPreferredContact=$_POST['INfoPreferred'];
$InfoFromZip=$_POST['INfoFromZip'];
$InfoToZip=$_POST['InfoToZip'];
$InfoResideType=$_POST['InfoResideType'];
$InfoRoomsNumber=$_POST['InfoRoomsNumber'];
$InfoFloorsNumber=$_POST['InfoFloorsNumber'];
$KitFridgeUpright=$_POST['KitFridgeUpright'];
$KitFridgeSidebySide=$_POST['KitFridgeSidebySide'];
$KitStove=$_POST['KitStove'];
$KitMicrowaveOven=$_POST['KitMicrowaveOven'];
$KitTable=$_POST['KitTable'];
$KitChairs=$_POST['KitChairs'];
$KitOther=$_POST['KitOther'];
$DinTable=$_POST['DinTable'];
$DinChairs=$_POST['DinChairs'];
$DinBuffet=$_POST['DinBuffet'];
$DinChinaCabient=$_POST['DinChinaCabient'];
$DinOther=$_POST['DinOther'];
$FamSofa=$_POST['FamSofa'];
$FamChairs=$_POST['FamChairs'];
$FamLoveSeat=$_POST['FamLoveSeat'];
$FamEntertainment=$_POST['FamEntertainment'];
$FamStereoYN=$_POST['FamStereoYN'];
$FamPiano=$_POST['FamPiano'];
$FamOther=$_POST['FamOther'];
$BedTwin=$_POST['BedTwin'];
$BedDouble=$_POST['BedDouble'];
$BedQueen=$_POST['BedQueen'];
$BedKing=$_POST['BedKing'];
$BedBunk=$_POST['BedBunk'];
$BedWater=$_POST['BedWater'];
$BedCrib=$_POST['BedCrib'];
$BedSingleDresser=$_POST['BedSingleDresser'];
$BedDoubleDresser=$_POST['BedDoubleDresser'];
$BedTripleDresser=$_POST['BedTripleDresser'];
$BedNightStand=$_POST['BedNightStand'];
$BedArmoire=$_POST['BedArmoire'];
$BedOther=$_POST['BedOther'];
$OfficeDesk=$_POST['OfficeDesk'];
$OfficeChairs=$_POST['OfficeChairs'];
$OfficeFiling=$_POST['OfficeFiling'];
$OfficeSafe=$_POST['OfficeSafe'];
$OfficeCopier=$_POST['OfficeCopier'];
$OfficeComputers=$_POST['OfficeComputers'];
$OfficeOther=$_POST['OfficeOther'];
$GarWorkbench=$_POST['GarWorkbench'];
$GarLadders=$_POST['GarLadders'];
$GarMower=$_POST['GarMower'];
$GarGrill=$_POST['GarGrill'];
$GarWheelbarrow=$_POST['GarWheelbarrow'];
$GarOther=$_POST['GarOther'];
$OutPatioTable=$_POST['OutPatioTable'];
$OutPatioUmbrella=$_POST['OutPatioUmbrella'];
$OutOther=$_POST['OutOther'];
$MiscAquarium=$_POST['MiscAquarium'];
$MiscGolf=$_POST['MiscGolf'];
$MiscWDCombo=$_POST['MiscWDCombo'];
$MiscWasher=$_POST['MiscWasher'];
$MiscDryer=$_POST['MiscDryer'];
$MiscVacuum=$_POST['MiscVacuum'];
$MiscOther=$_POST['MiscOther'];

mysql_connect("localhost", "********", "******") or die(mysql_error());
mysql_select_db("**********") or die(mysql_error());
mysql_query("INSERT INTO `quotes` VALUES ('$InfoName','$InfoEmail','$InfoPhone','$InfoPreferredContact','$InfoFromZip','$InfoToZip','$InfoRes ideType','$InfoRoomsNumber','$InfoFloorsNumber','$KitFridgeUpright',
'$KitFridgeSidebySide','$KitStove','$KitMicrowaveOven','$KitTable','$KitChairs','$KitOther','$DinTab le','$DinChairs','$DinBuffet','$DinChinaCabient','$DinOther','$FamSofa','$FamChairs','$FamLoveSeat', '$FamEntertainment','$FamStereoYN','$FamPiano','$FamOther','$BedTwin','$BedDouble','$BedQueen','$Bed King','$BedBunk','$BedWater','$BedCrib','$BedSingleDresser','$BedDoubleDresser','$BedTripleDresser', '$BedNightStand','$BedArmoire','$BedOther','$OfficeDesk','$OfficeChairs','$OfficeFiling','$OfficeSaf e','$OfficeCopier','$OfficeComputers','$OfficeOther','$GarWorkbench','$GarLadders','$GarMower','$Gar Grill','$GarWheelbarrow','$GarOther','$OutPatioTable','$OutPatioUmbrella','$OutOther','$MiscAquarium ','$MiscGolf','$MiscWDCombo','$MiscWasher','$MiscDryer','$MiscVacuum','$MiscOther')");

$to = "Webmaster@cwsenterprise.com";
$subject = "Testing Code";
$InfoName = $_REQUEST['InfoName'];
$InfoEmail = $_REQUEST['InfoEmail'];
$InfoPhone = $_REQUEST['InfoPhone'];
$InfoPreferredContact = $_REQUEST['INfoPreferred'];
$InfoFromZip = $_REQUEST['INfoFromZip'];
$InfoToZip = $_REQUEST['InfoToZip'];
$InfoResideType = $_REQUEST['InfoResideType'];
$InfoRoomsNumber = $_REQUEST['InfoRoomsNumber'];
$InfoFloorsNumber = $_REQUEST['InfoFloorsNumber'];
$KitFridgeUpright = $_REQUEST['KitFridgeUpright'];
$KitFridgeSidebySide = $_REQUEST['KitFridgeSidebySide'];
$KitStove = $_REQUEST['KitStove'];
$KitMicrowaveOven = $_REQUEST['KitMicrowaveOven'];
$KitTable = $_REQUEST['KitTable'];
$KitChairs = $_REQUEST['KitChairs'];
$KitOther = $_REQUEST['KitOther'];
$DinTable = $_REQUEST['DinTable'];
$DinChairs = $_REQUEST['DinChairs'];
$DinBuffet = $_REQUEST['DinBuffet'];
$DinChinaCabient = $_REQUEST['DinChinaCabient'];
$DinOther = $_REQUEST['DinOther'];
$FamSofa = $_REQUEST['FamSofa'];
$FamChairs = $_REQUEST['FamChairs'];
$FamLoveSeat = $_REQUEST['FamLoveSeat'];
$FamEntertainment = $_REQUEST['FamEntertainment'];
$FamStereoYN = $_REQUEST['FamStereoYN'];
$FamPiano = $_REQUEST['FamPiano'];
$FamOther = $_REQUEST['FamOther'];
$BedTwin = $_REQUEST['BedTwin'];
$BedDouble = $_REQUEST['BedDouble'];
$BedQueen = $_REQUEST['BedQueen'];
$BedKing = $_REQUEST['BedKing'];
$BedBunk = $_REQUEST['BedBunk'];
$BedWater = $_REQUEST['BedWater'];
$BedCrib = $_REQUEST['BedCrib'];
$BedSingleDresser = $_REQUEST['BedSingleDresser'];
$BedDoubleDresser = $_REQUEST['BedDoubleDresser'];
$BedTripleDresser = $_REQUEST['BedTripleDresser'];
$BedNightStand = $_REQUEST['BedNightStand'];
$BedArmoire = $_REQUEST['BedArmoire'];
$BedOther = $_REQUEST['BedOther'];
$OfficeDesk = $_REQUEST['OfficeDesk'];
$OfficeChairs = $_REQUEST['OfficeChairs'];
$OfficeFiling = $_REQUEST['OfficeFiling'];
$OfficeSafe = $_REQUEST['OfficeSafe'];
$OfficeCopier = $_REQUEST['OfficeCopier'];
$OfficeComputers = $_REQUEST['OfficeComputers'];
$OfficeOther = $_REQUEST['OfficeOther'];
$GarWorkbench = $_REQUEST['GarWorkbench'];
$GarLadders = $_REQUEST['GarLadders'];
$GarMower = $_REQUEST['GarMower'];
$GarGrill = $_REQUEST['GarGrill'];
$GarWheelbarrow = $_REQUEST['GarWheelbarrow'];
$GarOther = $_REQUEST['GarOther'];
$OutPatioTable = $_REQUEST['OutPatioTable'];
$OutPatioUmbrella = $_REQUEST['OutPatioUmbrella'];
$OutOther = $_REQUEST['OutOther'];
$MiscAquarium = $_REQUEST['MiscAquarium'];
$MiscGolf = $_REQUEST['MiscGolf'];
$MiscWDCombo = $_REQUEST['MiscWDCombo'];
$MiscWasher = $_REQUEST['MiscWasher'];
$MiscDryer = $_REQUEST['MiscDryer'];
$MiscVacuum = $_REQUEST['MiscVacuum'];
$MiscOther = $_REQUEST['MiscOther'];
$headers = "From: $InfoEmail";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{header( "Location: http://www.cwsenterprise.com/QuoteThanks.html" );}
else
{print "We encountered an error sending your mail, please notify Webmaster@cwsenterprise.com"; }
}

?>

I'm also looking for how to change the email body to say something like "Side by Side Refrigerators" instead of "KitFridgeSidebySide".

I'm shocked to say that I was even able to figure out the code for redirecting to the thanks page. Just the damn body of the email.....it's elusive!

USMarineNCO
11-15-2006, 04:38 AM
Just FYI I'm using http://www.cwsenterprise.com/QuoteTest.html as the test page for the coding in case you wanted to look at the HTML before the PHP.

CFMaBiSmAd
11-15-2006, 05:28 AM
To expand/replace the mnemonics with a full text description, either use a translation array or defined constants -

<?php
// use a translation array -
$expand = array();
$expand['KitFridgeSidebySide'] = 'Side by Side Refrigerators';
$expand['InfoPreferredContact'] = 'Preferred Contact Method';
// add other translations here...

echo $expand['KitFridgeSidebySide'];

echo $expand['InfoPreferredContact'];

// or use defined constants -
define('KitFridgeSidebySide', 'Side by Side Refrigerators');
define('InfoPreferredContact', 'Preferred Contact Method');
// add other define constants here...

echo KitFridgeSidebySide;

echo InfoPreferredContact;
?>In the code you posted above, you have some redundancy -

$InfoName=$_POST['InfoName'];
...
// and
$InfoName=$_REQUEST['InfoName'];
... The $_POST/$_REQUEST pairs refer to the same value from the form and make the assignment to the same variable name. After you do this once in the code (with the POST...) it is not necessary to do it the second time (with the REQUEST...)

USMarineNCO
11-15-2006, 11:59 AM
The $_POST/$_REQUEST pairs refer to the same value from the form and make the assignment to the same variable name. After you do this once in the code (with the POST...) it is not necessary to do it the second time (with the REQUEST...)

I removed the "REQUEST" code, and retried to the mailing portion of the form. The data still sends to the database, which is a required function I need. However, When it sends the email to me it sends just a blank email.

UberDragon
11-15-2006, 08:58 PM
I removed the "REQUEST" code, and retried to the mailing portion of the form. The data still sends to the database, which is a required function I need. However, When it sends the email to me it sends just a blank email.

Your $message variable (used in the mail function) is empty. You need to fill it with what you want to see in the body of the email message. IE:



$to = "Webmaster@cwsenterprise.com";
$subject = "Testing Code";

$message .= "\r\rInfo Name: ".$InfoName;
$message .= "\r\rInfo Phone: ".$InfoPhone;
// Keep adding all the variables you defined earlier
// ..
// ..
$headers = "From: $InfoEmail";
$sent = mail($to, $subject, $message, $headers) ;


So you can take out all the second variable assignements (the ones using $_REQUEST) and replace those lines with lines that fill the $message variable as shown in the code snippet above.

~Uber

ez4me2c3d
11-15-2006, 09:35 PM
I removed the "REQUEST" code, and retried to the mailing portion of the form. The data still sends to the database, which is a required function I need. However, When it sends the email to me it sends just a blank email.

you posted

$sent = mail($to, $subject, $message, $headers) ;
Yet you never set the $message variable to anything.

EDIT: Dude above me posted it first, I wasn't quick enough

USMarineNCO
11-15-2006, 10:49 PM
you posted

$sent = mail($to, $subject, $message, $headers) ;
Yet you never set the $message variable to anything.

EDIT: Dude above me posted it first, I wasn't quick enough

Guess it doesn't matter who it came from....I appreciate it all the same!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum