0

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.

11
  • 1. var_dump($_POST); 2. Open dev tools in your browser and check what was sent Commented Jul 5, 2013 at 1:25
  • 1
    Because the same page is not re-loading. I think you want to update the markup using Ajax. Where you have alert statement, use $('.some-item').html(data);. Commented Jul 5, 2013 at 1:29
  • 1
    You're using AJAX to post a value to an intermediate script, which seems to be working. If you have the variable, why not place it where you need it with Javascript? Exactly where are you checking $_POST['case']? Your variable name is different too (case or case_pk). Commented Jul 5, 2013 at 1:30
  • It's not really clear what you mean. You can't access the value in $_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. Commented Jul 5, 2013 at 1:31
  • 1
    @PeterBrowne: You don't have to POST the value specifically to the same page, you can POST it to any page. (Personally I tend to prefer handling AJAX requests as their own resources instead of the same "page" that produces a UI.) In your example you're POSTint it to get_case.php, and doing so successfully. Have you tried running the database query from get_case.php? In the code posted that seems to be the ideal place to do it. Commented Jul 5, 2013 at 2:02

2 Answers 2

1

It's not entirely clear which page you're intending to POST the value to, since the first of the two PHP samples doesn't appear to be used by anything. The second of the two examples, however, seems to be working. You're successfully performing an AJAX POST to another server-side resource (get_case.php) which is successfully receiving the value and echoing back that same value (given the description that the client-side alert() is behaving as expected).

So your server-side functionality in this case can be initiated by get_case.php, and any relevant response can then be sent back to the page and handled by client-side code in the callback function (where you currently have an alert()).

Keep in mind that it's not necessary to POST values to "the current page." You can POST to any server-side resource. Indeed, you may find it preferable to separate your server-side resources between pages which produce a UI (the "current page" in this case) and pages which act more like web services for AJAX calls and return only data (get_case.php in this case). This can make it a lot easier to organize your code without having messy conditionals to determine if you should render a UI or JSON.

So a successful order of operations might go something like this:

  1. User loads page1.php in their browser.
  2. page1.php performs some server-side action and renders a UI.
  3. In the UI there is JavaScript code which makes an AJAX call to page2.php.
  4. page2.php performs some server-side action and renders JSON data.
  5. The JavaScript code receives the JSON data and performs some client-side action.
Sign up to request clarification or add additional context in comments.

Comments

1

I assume that your "post" is something like

<input id="case_pk" name="case_pk" type="hidden" value="">

so in the success you should:

$('#case_pk').value(data);

in order to "reset/change" the value of that hidden input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.