I am setting up a form submission and having some troubles getting the Ajax to connect to the PHP and send the form info to the desired email address. Not an expert at this whole thing as its the first time I attempt AJAX to submit a form information.
Any input on what is going wrong is appreciated.
Thanks in advance
THIS CODE IS NOW ALL WORKING I have updated the question with the new code.
FORM:
<form name="contactform" id="contactsubmit" action="form_action.php">
<h2>contact form</h2>
<hr>
<label class="Tgrey">name</label>
<input type="text" name="name" id="name" required>
<label class="Tgrey">email</label>
<input type="email" name="email" id="email" required>
<label class="Tgrey">Telephone</label>
<input type="tel" name="telephone" id="telephone" required>
<br>
<br>
<label class="Tgrey">Telephone</label>
<br>
<textarea name="message" rows="10" cols="50" id="message" required></textarea>
<br>
<input type="submit" value="send">
</form>
JQUERY/AJAX
$("form").submit(function(e)
{
event.preventDefault();
$.ajax(
{
'url' : $(this).attr('action'),
'type' : "POST",
'data' : $(this).serialize(),
success:function(success)
{alert("HEY")},
});
});
PHP
if(isset($_POST['name'], $_POST['email'], $_POST['telephone'], $_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$message = $_POST['message'];
$to = "name@email.com";
$subject = "Message from $name via Web:";
$txt = "This message has come website: \n
$message \n
Contact details:\n
Name: $name, Email: $email, Telephone: $telephone";
$header = "From Website";
mail($to,$subject,$txt,$headers);
}