Newbie here. I would like to ask for a help in creating a basic log file on what values inserted on the field.
Here's my html:
<form id = "form1" name = "form1" method="post">
<div id="fstep_1">
<p>
Your email address:
</p>
<input type="text" name="email" id="email" class="required email">
<label for="email" class="error" style="display: none;">This field is required</label>
</div>
<button type="submit" class="fsubmit">Submit</button>
here's my jquery:
<script>
$(".fsubmit").click(function() {
var emailval = $("#email").val().trim();
$.ajax({
url: '/logfiletracker.php',
type: 'POST',
data: {'data' : {email: emailval,
success: function(data) {},
});
});
</script>
Here's my ajax:
<script>
$(".fsubmit").click(function() {
var emailval = $("#email").val().trim();
$.ajax({
url: '/logfiletracker.php',
type: 'POST',
data: {'data' : {email: emailval,
success: function(data) {},
});
});
</script>
Here's my PHP
<?php
$data = $_POST['data'];
$date = new DateTime();
$datelog = $date->format('d.m.Y h:i:s');
$message = '[' . $datelog . '] - email: ' .$data;
echo($message);
?>
My problem is that it doesn't view any data at all except the date which is looks like this one
[02.02.2017 12:54:11] - email:
And when I tried to add another test, it doesn't increment the data. Is there something lack in my code?
Your answers are appreciated.
$.ajax({
url: '/logfiletracker.php',
type: 'POST',
data: {'data' : emailval},
success: function(data) {},
});
Because your data is not being sent in a proper manner and is not received by php. Modify your Ajax code and it will work
for testing purposes, to know where the problem is (js or php), you can do:
On JavaScript:
$(".fsubmit").click(function() {
var emailval = $("#email").val().trim();
var data = {
'data' : {
email: emailval
}
};
// testing!
console.log(data);
$.ajax({
url: '/logfiletracker.php',
type: 'POST',
data: data,
success: function(data) {}
});
});
On PHP:
<?php
$data = $_POST['data'];
// testing!
echo json_encode($_POST['data']);
exit;
$date = new DateTime();
$datelog = $date->format('d.m.Y h:i:s');
$message = '[' . $datelog . '] - email: ' .$data;
echo($message);
?>