I have a form which sends its form fields to a remote locations through a javascript file which involves remote API.
I want to capture a single form field through PHP before it is sent to remote API.
My form inside a PHP file and the code to capture email field
<form id="mycustomform" method="post" action="" role="form">
<div class="row">
<div class="form-group col-lg-4">
<label for="Email-input">Email</label>
<input type="email" class="form-control" id="Email_Address-input" placeholder="Your Email" name="vemail" parsley-type="email" required>
</div>
<div id="submit" class="form-group col-lg-12">
<input type="hidden" name="submit" value="contact">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
<?php
if(isset($_POST['submit'])){
$to = "myself@email.com";
$subject = "New Entry";
$message = $_POST['vemail'];
mail($to,$subject,$message);
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://www.mywebsite.com/javascript.js"></script>
javascript.js
$("#mycustomform").submit(function(event) {
var lead = {};
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
lead['Email'] = $('#Email_Address-input').val();
$.post("http://remoteserver.com/Token", {grant_type: 'password', username: 'myusername', password: 'mypassword'})
.done(function(data) {
$.ajax({
url: 'http://remoteserver.com/api/',
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(lead),
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + data.access_token)
}
}).done(function(data) {
window.location = "http://www.mywebsite.com/thank-you.html";
}).fail(function(jqXHR, textStatus, errorThrown) {
});
}).fail(function(jqXHR, textStatus, errorThrown) {
});
event.preventDefault();
});
The problem is that once the javascript is involved, the field is not captured and an email is not sent. As soon as I remove the javascript reference, it captures the form field and sends an email without problem.
Can someone please help how would I achieve this?
Basically I want to send the email field to myself via mail()
function and then have the PHP file to process the field forms and send them to remote API.