I already have everything pre-defined in the script. What I want to do is when I click on SUBMIT it will do the work without refreshing the page. Currently it refreshes the page and shows "Working". What I am trying to do is something like this:
Here is my code:
<?php
$url="http://google.com";
if(isset($_POST["submit"])){
$ch = curl_init("$url");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200==$retcode) {
echo "working";
} else {
echo "failed";
}
}
?>
<form action='ping.php' method='post'><input type="submit" name="submit"></form>
<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript">
var $url = '<?php echo $url ?>';
$('#submit').click(function(e){
e.preventDefault();
var $this = $(this);
$.get($url,function(data){
$this.text('Success');
});
});
</script>
EDIT:
The script in the above example shows an error:
[The request] has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
so I was going to do it with PHP cURL. If anyone knows any solution, please help.