im currnetly making a website and im stuck i have a form
<form name="inputForm" onSubmit="return validateForm();" action="#">
<fieldset>
<p>
<label>First Name:</label>
<input type="text" name="first_name"/><br />
<label>Surname:</label>
<input type="text" name="surname"/><br />
<label>Address number:</label>
<input type="text" name="address_number"/><br />
<label>Address name:</label>
<input type="text" name="address_name"/><br />
<label>Suburb:</label>
<input type="text" name="suburb"/><br />
<label>Postcode:</label>
<input type="text" name="postcode"/><br />
<label>State:</label>
<select name="state">
<option value="nsw">NSW
<option value="qld">QLD
<option value="act">ACT
<option value="vic">VIC
<option value="sa">SA
<option value="tas">TAS
<option value="nt">NT
<option value="wa">WA
</SELECT><br />
<label>Email:</label>
<input type="text" name="email"/><br />
<label>Phone Number:</label>
<input type="text" name="phone"/><br />
<input type="submit" name="submit" value="Send form" />
<input type="reset" name="reset" value="reset" />
</p>
</fieldset>
</form>
which is then validated by javascript
function validateForm()
{
var form = document.forms['inputForm'];
var formats =
{
first_name: /^[a-zA-Z]+[\-'\s]?[a-zA-Z]+$/, /*works for a-Z allows - and '*/
surname: /^[a-zA-Z]+[\-'\s]?[a-zA-z]+$/, /*works for a-Z allows - and '*/
postcode: /^\d{4}$/, /*4digit post code australia wide*/
email: /^\w+([\.-]w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/, /*allows all word characters and normal email formats*/
address_number: /^\d[0-9]$/, /*allows any number of digits*/
address_name: /^\w?\s?[a-zA-Z]+(,?\s([a-zA-Z])*)*$/, /*allows numbers space capital letters and other word characters*/
suburb: /^\w?\s?[a-zA-Z]+(,?\s([a-zA-Z])*)*$/, /*allows numbers space capital letters and other word characters*/
phone: /^\d{8}$/, /*8 number phone number*/
};
var elCount = form.elements.length;
for(var i = 0; i<elCount; i++)
{
var field = form.elements[i];
if(field.type == 'text')
{
if(!formats[field.name].test(field.value))
{
alert('invalid '+ field.name.replace('_',' ')+'.'); /*alerts the name of the area not filled right in a pop up box*/
field.focus();
return false;
}
}
}
alert('All fields correct, the form will now submit.')
}
then i need php to gather that information and send it to my email address (this is where im stuck)
<?php
if( isset( $_POST['submit'] ) || isset( $_POST['submit_x']))
{
$to = "myemail@myemail.com";
$subject = 'subject';
$message = 'testing the php mail() function';
$headers = "from: email\r\n";
$body="
Name: $name\n
Email: $email\n
";
echo "the information has been sent we will be in contact with you shortly";
mail($to, $subject, $message, $headers);
}
else
{
echo "The informations has not been sent";
}
?>
now i’ve been scratching my head over this for a couple days looked it up cant find anything im still learning about php and javascript
the javascript and HTML work fine its just the mail function please help
first, add method=”post” to your form, so it looks like this:
Next
to