I'm not a programmer, so I'm in trouble despite searching the net.
I am using a JS script inserted in WordPress through which I enter a domain and it returns me some data.
The script calls an external PHP file.
However, when I click the form button, it returns me an HTML string which includes the value of the previously entered input field and, instead of the DATA value it processed from the PHP file, the string [object Object].
This is HTML:
<div class="divtoolg">
<form id="form">
<div class="form-group">
<input class="form-control" type="text" id="domain" required placeholder="Enter Input">
</div>
<div class="form-group">
<button class="btn btn-danger btn-block">
Get Data
</button>
</div>
<div id="result"></div>
</form>
</div>
This is the script:
/*jshint esversion: 6 */
let $ = jQuery;
$(document).ready(function(){
$("#form").submit(function(e){
e.preventDefault();
$("#result").html("");
var domain = $("#domain").val();
console.log(domain);
$.ajax({
metod: 'POST',
url:'process.php',
data:{domain:domain},
async: true,
complete:function (data) {
console.log("data ="+data);
$("#result").html(`<h2>The ${domain} is ${data} old </h2>`);
$("#domain").val(domain);
},
error:function () {
$("#domain").val("");
$("#result").html("Error!");
}
});
});
});
This is the PHP file:
<?php
if(isset($_POST['domain']))
{
require('Ageclass.php');
$w=new DomainAge();
echo $w->age($_POST['domain']);
}
?>
Anyone have any ideas? Many thanks.
New JS
/*jshint esversion: 6 */
let $ = jQuery;
$(document).ready(function(){
$("#form").submit(function(e){
e.preventDefault();
$("#result").html("");
var domain = $("#domain").val();
console.log(domain);
$.ajax({
method: 'POST',
url:'../process.php',
data:{domain:domain},
success:function (data) {
console.log(data);
$("#result").html(`<h2>The ${domain} is ${data} old </h2>`);
$("#domain").val("");
},
error:function () {
$("#domain").val("");
$("#result").html("Error!");
}
});
});
});