I'm trying to post the value of a dynamic hidden input back to the same page using JQuery $.post with an intermediary php script. The correct value is returned and shown in the alert in the code below, but I can't grab the value in the page via $_POST:
$(document).ready(function() {
$('.case').click(function() {
var caseId = $(this).find('input:hidden').val();
$.post("scripts/get_case.php", { case_pk: caseId },
function(data) {
alert("Respond: " + data);
});
});
And on the same page:
$caseId = $_POST['case_pk'];
echo $caseId;
The intermediary php page (get_case.php):
<?php
$case = $_POST['case_pk'];
echo $case;
?>
The scenario is:
I have div buttons which are dynamically generated based on the number of cases in mysql db in a page vmd.php. Each of these dynamically generated div buttons contains a hidden input with the corresponding value of the case id (case_pk). On clicking one of these buttons, I want to run a mysql query based on the case id in vmd.php. So my understanding is that I need to pass case_pk in the hidden fields via ajax back to the same page (vmd.php) and put that into a php variable that I can use in the query.
var_dump($_POST);2. Open dev tools in your browser and check what was sent$('.some-item').html(data);.caseorcase_pk).$_POST, but you claim that the correct value is returned back from the AJAX call. All that server-side code does is access the value in$_POST, so it seems to be working. And I'm not sure at all what you're trying to do in the first PHP code example. You don't know any form that posts a value to that.get_case.php, and doing so successfully. Have you tried running the database query fromget_case.php? In the code posted that seems to be the ideal place to do it.