HTML form / PHP email help

Associate
Joined
22 Aug 2010
Posts
2,212
Location
In My Command Center
ok i have a form on the website im working on, there is only one form with all the available options but depending on what option is chosen depends on what elements are shown. I use JS to hide and show the relivant elements.

so on my web page there are three buttons

1) my boiler needs fixing 2) my boiler needs a service 3) contact us

so if the first option is chosen only <tr id="message"> is hidden.
if the second option is chosen both <tr id="message"> and <tr id="message1"> are hidden.
if the third option is chosen then <tr id="fuel"><tr id="bmgas"><tr id="bmlpg"><tr id="bmoil"><tr id="sevice">
<tr id="message1"> are hidden.

Code:
                <div id="form1">
                	<form name="fix" method="post" action="http://localhost/PHP/fixscript.php">
						<table width="800" border="0" cellspacing="10" cellpadding="5">
  						<tr>
    						<td width="230" valign="top" align="right">First name: </td>
                			<td width="503" valign="top" align="left">
                            <input type="text" name="fName" id="fName" size="50" maxlength="20" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">Surname: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="lName" id="lName" size="50" maxlength="20" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">Contact phone number: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="phone" id="phone" size="50" maxlength="20" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">Email: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="email" id="email" size="50" maxlength="75" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">House name or number: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="num" id="num" size="50" maxlength="75" /></td>
  						</tr>
           				<tr>
    						<td width="230" valign="top" align="right">First line of your address: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="address1" id="address1" size="50" maxlength="100" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">Second line of your address: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="address2" id="address2" size="50" maxlength="100" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">Town/City: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="city" id="city" size="50" maxlength="50" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">County: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="county" id="county" size="50" maxlength="50" /></td>
  						</tr>
            			<tr>
    						<td width="230" valign="top" align="right">Post code: </td>
    						<td width="503" valign="top" align="left">
                            <input type="text" name="postCode" id="postCode" size="50" maxlength="10" /></td>
  						</tr>
            			<tr id="fuel">
                			<td width="230" valign="top" align="right">Fuel Type: </td>
                			<td width="503" valign="top" align="left">
                				<input type="radio" name="fuelType" id="fuel1" value="Natural Gas" onclick="showOptions(this);" />
                 					<label for="fuel1">Natural Gas</label>
              					<input type="radio" name="fuelType" id="fuel2" value="LPG" onclick="showOptions(this);" />
                 					<label for="fuel2">LPG</label>
              					<input type="radio" name="fuelType" id="fuel3" value="Oil" onclick="showOptions(this);" />
                 					<label for="fuel3">Oil</label>
            				</td>
            			</tr>
            			<tr id="bmgas">
    						<td width="230" valign="top" align="right">Boiler manufacturer: </td>
    						<td width="503" valign="top" align="left">
                    			<select name="gas" id="gas" >
                 					<option value="0">Natural Gas</option>
                 					<option value="Alpha Boilers">Alpha Boilers</option>
                 					<option value="Ariston">Ariston</option>
                 					<option value="Baxi">Baxi</option>
                 					<option value="Biasi">Biasi</option>
                 					<option value="Broag">Broag</option>
                 					<option value="Ferroli">Ferroli</option>
                 					<option value="Glowworm">Glowworm</option>
                 					<option value="Halstead Heating">Halstead Heating</option>
                 					<option value="Ideal Boilers">Ideal Boilers</option>
                 					<option value="Ikon">Ikon</option>
                 					<option value="Jaguar">Jaguar</option>
                 					<option value="Johnson &amp; Starlay">Johnson &amp; Starlay</option>
                 					<option value="Keston Boilers">Keston Boilers</option>
                					<option value="Potterton">Potterton</option>
                 					<option value="Range Powermax">Range Powermax</option>
                 					<option value="Raven Heat">Raven Heat</option>
                 					<option value="Vaillant">Vaillant</option>
                 					<option value="Viessmann">Viessmann</option>
                 					<option value="Vokera">Vokera</option>
                 					<option value="Worcester">Worcester</option>
                 					<option value="Unknown">Unknown</option>
                 					<option value="Not Listed">Not Listed</option>
              					</select>
                			</td>
  						</tr>
            			<tr id="bmlpg">
    						<td width="230" valign="top" align="right">Boiler manufacturer: </td>
    						<td width="503" valign="top" align="left">
                    			<select name="lpg" id="lpg">
                 					<option value="0">LPG</option>
                 					<option value="Alpha Boilers">Alpha Boilers</option>
                 					<option value="Baxi">Baxi</option>
                 					<option value="Keston Boilers">Keston Boilers</option>
                 					<option value="Potterton">Potterton</option>
                 					<option value="Vaillant">Vaillant</option>
                 					<option value="Viessmann">Viessmann</option>
                 					<option value="Worcester">Worcester</option>
                 					<option value="Unknown">Unknown</option>
                 					<option value="Not Listed">Not Listed</option>
              					</select>
                			</td>
  						</tr>
            			<tr id="bmoil">
    						<td width="230" valign="top" align="right">Boiler manufacturer: </td>
    						<td width="503" valign="top" align="left">
                   				<select name="oil" id="oil">
                 					<option value="0">Oil</option>
                 					<option value="Buderas">Buderas</option>
                 					<option value="Danesmoor">Danesmoor</option>
                 					<option value="Firebird">Firebird</option>
                 					<option value="Grant">Grant</option>
                 					<option value="HRM">HRM</option>
                 					<option value="Nuway Oil">Nuway Oil</option>
                 					<option value="Thermeco">Thermeco</option>
                 					<option value="Trianco">Trianco</option>
                 					<option value="Worcester">Worcester</option>
                 					<option value="Unknown">Unknown</option>
                 					<option value="Not Listed">Not Listed</option>
              					</select>
                			</td>
  						</tr>
            			<tr id="sevice">
    						<td width="230" valign="top" align="right">Last Service: </td>
    						<td width="503" valign="top" align="left">
                    			<select name="lastServ" id="lastServ">
                 					<option value="0">Last Service</option>
                 					<option value="6 months">Less than 6 months ago</option>
                 					<option value="1 year">One year ago</option>
                 					<option value="2 years">Two years ago</option>
                 					<option value="never">Never been serviced</option>
                 					<option value="unknown">unknown</option>
               					</select>
                			</td>
  						</tr>
                        <tr id="message">
    						<td class="form" width="230" valign="top" align="right">Your message: </td>
    						<td width="503" valign="top" align="left">
                    			<textarea name="msg" id="msg" cols="40" rows="10"></textarea>
                			</td>
  						</tr>
            			<tr id="message1">
    						<td width="230" valign="top" align="right">Brief Description of fault: </td>
    						<td width="503" valign="top" align="left">
                    			<textarea name="msg1" id="msg1" cols="40" rows="10"></textarea>
                			</td>
  						</tr>
            			<tr>
    						<td width="230" valign="top"> </td>
    						<td width="503" valign="top" align="left">
                    			<input type="button" name="return" id="return" class="button" value="Return"  onclick="location.reload;" />&nbsp;&nbsp;&nbsp;&nbsp;
                				<input type="reset" name="reset" class="button2" value="Reset" onclick="hide(bmgas, bmlpg, bmoil);" />&nbsp;&nbsp;&nbsp;&nbsp;
                    			<input type="submit" name="submit" class="button3" value="Submit" onsubmit="checkForm();" />
                			</td>
  						</tr>
					</table>
				</form>

i have the validation handled by JS so once the form is submitted i want my PHP to collect the data and send an email.

Code:
<?php

	// VALUES FROM THE FORM

	$first		= $_POST['fName'];
	$last		= $_POST['lName'];
	$phone		= $_POST['phone'];
	$email		= $_POST['email'];
	$number		= $_POST['num'];
	$address1	= $_POST['address1'];
	$address2	= $_POST['address2'];
	$city		= $_POST['city'];
	$county		= $_POST['county'];
	$postCode	= $_POST['postCode'];
	$fuelType	= $_POST['fuelType'];
	$fuelGas	= $_POST['gas'];
	$fuelLpg	= $_POST['lpg'];
	$fuelOil	= $_POST['oil'];
	$lastServ	= $_POST['lastServ'];
	$msg		= $_POST['msg'];
	$msg1		= $_POST['msg1'];
	
	// CREATE THE EMAIL

	$headers	= "Content-Type: text/plain; charset=iso-8859-1\n";

	$headers	= "From: $first $last <$email>\n";

	$recipient	= "[email protected]";

	$subject	= "Message from blah.com";

	$message	= wordwrap($message, $fName, $lName, $phone, $number, $address1, $address2, $city, $county, $postCode, $fuelType, $fuelGas, $fuelLpg, $fuelOil, $lastServ, $msg, $msg1, 1024);



	// SEND THE EMAIL TO YOU

	mail($recipient, $subject, $message, $headers);



	// REDIRECT TO THE THANKS PAGE

	header("location: thanks.php");

?>

i recieve an email but it is blank? can any of you see what i need to change, many thanks for your help :)
 
if any one is interested i solved my puzzle -

Code:
<?php

	// VALUES FROM THE FORM

	$first		= $_REQUEST['fName'];
	$last		= $_REQUEST['lName'];
	$phone		= $_REQUEST['phone'];
	$email		= $_REQUEST['email'];
	$number		= $_REQUEST['num'];
	$address1	= $_REQUEST['address1'];
	$address2	= $_REQUEST['address2'];
	$city		= $_REQUEST['city'];
	$county		= $_REQUEST['county'];
	$postCode	= $_REQUEST['postCode'];
	$fuelType	= $_REQUEST['fuelType'];
	$fuelGas	= $_REQUEST['gas'];
	$fuelLpg	= $_REQUEST['lpg'];
	$fuelOil	= $_REQUEST['oil'];
	$lastServ	= $_REQUEST['lastServ'];
	$msg		= $_REQUEST['msg'];
	$msg1		= $_REQUEST['msg1'];
	
	$fields = array();
	$fields{"fName"} = "First Name";
	$fields{"lName"} = "Last Name";
	$fields{"phone"} = "Phone Number";
	$fields{"email"} = "Email";
	$fields{"num"} = "House Number or Name";
	$fields{"address1"} = "First Line Of Address";
	$fields{"address2"} = "Second Line Of Address";
	$fields{"city"} = "Town Or City";
	$fields{"county"} = "County";
	$fields{"postCode"} = "Post Code";
	$fields{"fuelType"} = "Fuel Type";
	$fields{"gas"} = "Gas Boiler Type";
	$fields{"lpg"} = "LPG Boiler Type";
	$fields{"oil"} = "Oil Boiler Type";
	$fields{"lastServ"} = "last Serviced";
	$fields{"msg"} = "Message";
	$fields{"msg1"} = "Description of fault";
		
	// CREATE THE EMAIL

	$headers	= "Content-Type: text/plain; charset=iso-8859-1\n";

	$headers	= "From: $first $last <$email>\n";

	$recipient	= "[email protected]";

	$subject	= "Message from iFixBoilers.com";

	$message	= "A message has been sent from: \n\n"; foreach($fields as $a => $b){ $message .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

	// SEND THE EMAIL TO YOU

	mail($recipient, $subject, $message, $headers);



	// REDIRECT TO THE THANKS PAGE

	header("location: thanks.php");

?>
 
Just a note on what you've said.
It seems that you're validating using JS, but if I were to use the form and disable JS, could I submit the form? You're either preventing people from submitting the form, or you're allowing them to submit a form that's not been validated.
Also, for future use, the following code will grab all your POST data and put then into variable for you.
PHP:
foreach($_POST as $key => $value) {
		$$key = $value; // Assign the title of the field as the variable name and its value as the variables value
		}
	}

hiya no i have set up the site to work with JS switched off, i will be using php to vaildate the form for everyone without JS on. i dont like using php as it reloads the page every error. i fixed everything by re doing the PHP, but ty for looking :)

Code:
<?php

	// VALUES FROM THE FORM

	$first		= $_POST['fName'];
	$last		= $_POST['lName'];
	$phone		= $_POST['phone'];
	$email		= $_POST['email'];
	$number		= $_POST['num'];
	$address1	= $_POST['address1'];
	$address2	= $_POST['address2'];
	$city		= $_POST['city'];
	$county		= $_POST['county'];
	$postCode	= $_POST['postCode'];
	$fuelType	= $_POST['fuelType'];
	$fuelGas	= $_POST['gas'];
	$fuelLpg	= $_POST['lpg'];
	$fuelOil	= $_POST['oil'];
	$lastServ	= $_POST['lastServ'];
	$msg		= $_POST['msg'];
	$msg1		= $_POST['msg1'];
	
	// CREATE THE EMAIL

	$headers	.= "Content-Type: text/plain; charset=iso-8859-1\r\n";

	$headers	= "From: $first $last\r\n";

	$recipient	= "[email protected]\r\n";

	$subject	= "Message from iFixBoilers.com\r\n";
	
	$message	= <<<EOD
First name: $first
Last name: $last
Contact Number: $phone
Email: $email
House Name Or Number: $number
First Line Of Address: $address1
Second Line Of Address: $address2
Town Or City: $city
County: $county
Post Code: $postCode
Fuel Type: $fuelType
Gas: $fuelGas
LPG: $fuelLpg
Oil: $fuelOil
Last Serviced: $lastServ
Message: $msg
Message: $msg1
EOD;

	// SEND THE EMAIL TO YOU

	$send = mail($recipient, $subject, $message, $headers);

	// REDIRECT TO THE THANKS PAGE

	header("location: thanks.php");

?>
 
Back
Top Bottom