I need to create an ajax processing file, where this javascript is to send the variable myGroupId to, then send it back to my edit.php file where I can then run a query off of the returned file.
I am not experienced with ajax, so I need to know how the processing file should look regarding the javascript variable being converted to a php variable. I know I need to create another folder to send the javascript variable.
I just need to know how to do the ajax processing file (or whatever you want to call it).
Here is my javascript:
<script type="application/javascript">
$(document).on("click", ".open-EditRow", function () {
var myGroupId = $(this).data('id');
$(".modal-body #groupId").val( myGroupId );
// ajax call
var url = "?groupId=" + encodeURIComponent(myGroupId);
$.get(url, function(data){
// do something here
});
});
</script>
I'll call the separate file ajaxProcessing.php. Now if someone could show me how that file should look to convert myGroupId to a php variable, I would be grateful.
When it is sent back to my edit.php file, I should be able to reference it as $_GET['groupId'].
Thank you in advance.
Adapted from the jQuery documentation (http://api.jquery.com/jQuery.get/):
$.get(url, {GroupID: myGroupId})
.done(function(data) {
alert("Data Loaded: " + data);
});
However, if you're passing data to be processed by this script, you should really be using POST:
$.post(url, {GroupID: myGroupId})
.done(function(data) {
alert("Data Loaded: " + data);
});
At any rate, you could get the resulting variable on your ajax processing file by using $variable = $_GET['GroupID']; (or $_POST['GroupID'])
NOTE: To prevent SQL Injection, you must sanitize your $variable, either through mysqli_real_escape string or uses prepared mysqli statements More specifically, don't create a query like "Select * FROM table_name WHERE GroupID= $variable" (I know my syntax is incorrect here, but you get the idea).
Also, make sure you're using mysqli or PDO - mysql is deprecated.