In my PHP file I have a variable $file = "abc". How would I create a button in HTML to run a function in PHP passing in $file as an argument? I saw that I need to do this using jQuery/AJAX. For the sake of simplicity, let's just say I want to echo $file.
HTML/JS
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.js"></script>
<input type="submit" class="button" name="Del" value="Del" />
<script type="text/javascript">
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'ajax.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
console.log(response);
//alert("action performed successfully");
});
});
});
</script>
ajax.php
<?php
if (isset($_POST['action'])) {
Del();
}
function Del($file) {
echo $_POST['action'];
echo $file;
exit;
}
?>
I am unsure of how to modify this to pass in a php argument ($file) through AJAX.