I want to get the inputs from an html form, and send them to PHP through my JavaScript. Basically I have a very simple html form, and I would like to grab it's inputs and send them to PHP with JavaScript as I'm using AJAX.
The problem is that whenever I fill out the form and I submit it, it just send me an email BUT without the values from the form - just what I already have in the PHP.
HTML Code
<!-- CONTACT FORM START -->
<form id="contact-form" class="fade-up" method="post" action="">
<div class="row">
<!--NAME FIELD-->
<div class="col-md-6">
<input type="text" id="cf-name" class="input__field cf-validate" name="name" placeholder="Name">
</div>
<!--EMAIL FIELD-->
<div class="col-md-6">
<input type="text" id="cf-email" class="input__field cf-validate" name="email" placeholder="Email">
</div>
<!--MESSAGE BOX-->
<div class="col-md-12">
<textarea id="cf-message" class="input__field cf-validate" rows="8" name="message" placeholder="Your Message"></textarea>
</div>
<div class="alert-container col-md-12"></div>
<!--SUBMIT BUTTON-->
<div class="col-md-12">
<button type="submit" id="cf-submit" name="subind" class="main-btn cursor-link">Send as an individual</button>
<button type="submit" id="cf-submit" name="subbiz" class="main-btn cursor-link">Send as a business</button>
</div>
</div>
</form>
<!-- CONTACT FORM END -->
Javascript Code
$(document).ready(function() {
$('form').submit(function (event) {
event.preventDefault()
var name = document.getElementById('cf-name').value
var email = document.getElementById('cf-email').value
var msg = document.getElementById('cf-message').value
//make a post request by AJAX
$.post("mail.php",{name:name,email:email,msg:msg},function(data){
console.log(data)
})
})
})
PHP Code
<?php
$name = $_POST['name'];echo $_POST['name'];
$email = $_POST['email'];echo $_POST['email'];
$message = $_POST['msg'];echo $_POST['msg'];
$subject = "Client Mail - Individual";
$to = "clients@growpile.com";
$headers = "From: ".$email;
$txt = "Name: ".$name." - Individual"."\n"."From: ".$email."\n\n".$message."\n\n".$ind;
mail($to, $subject, $txt, $headers);
?>