0

I want to get the value of data-id in my tag and transfer it to a PHP variable.

<a data-toggle="modal" data-id="<?=$adminid;?>" href="#view_contact" class="btn btn-info btn-xs view-admin">View</a>

I already got the value of data-id using the javascript below and placed it into a variable, however its not posting on the page.

$(document).on("click", ".view-admin", function () {
 var adminid = $(this).data('id');
 $.post("index.php", {admin_to_pass: adminid});
});

I tried doing this...

print_r($_POST);

but the array being displayed is empty What am I doing wrong here? Kinda new at this.

Kevin
41.9k12 gold badges57 silver badges72 bronze badges
asked Sep 18, 2014 at 1:18
6
  • $.post() is sending that post data to another page. Are you trying to print the post array on this same page? Or are you trying to print it out on the page that the post data was sent to by javascript? Commented Sep 18, 2014 at 1:22
  • seems no prob on Js, wheres the PHP? same page? Commented Sep 18, 2014 at 1:22
  • You are passing the value adminid to post as admin_to_pass on the PHP side. So to print it do (echo $_POST['admin_to_pass'];) Commented Sep 18, 2014 at 1:25
  • Im trying to print the post array on this same page. But it doesn't seem to be working. Commented Sep 18, 2014 at 1:25
  • Posting a value via AJAX won't magically update the page you're already on. If you want to alter the current document after the POST is successful, add a callback function to do so. Commented Sep 18, 2014 at 1:26

1 Answer 1

1

If you're trying to post on the same page (which is a bad idea anyway), you should have always have an exit/die; on that PHP catch block, and a success function callback on the $.post.

Example:

Simulated JS:

<?php $adminid = 1; ?>
<a data-toggle="modal" data-id="<?=$adminid;?>" href="#view_contact" class="btn btn-info btn-xs view-admin">View</a>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).on("click", ".view-admin", function () {
 var adminid = $(this).data('id');
 $.post("index.php", {admin_to_pass: adminid}, function(response){
 console.log(response);
 });
});
</script>

Here on the PHP (same page):

if($_SERVER['REQUEST_METHOD'] == 'POST') {
 echo json_encode($_POST['admin_to_pass']);
 exit; // this is important
}

Or

if(isset($_POST['admin_to_pass'])) {
 echo json_encode($_POST['admin_to_pass']);
 exit; // this is important
 // you must have this or you'll catch the whole document
}

And since in your current code you don't have one, you need the success callback function to process the response which came from the server:

$.post("index.php", {admin_to_pass: adminid}, function(response){
 ^^^^
 console.log(response); // your response from the server is inside (you can name it any name you want) 
});
answered Sep 18, 2014 at 1:27
Sign up to request clarification or add additional context in comments.

4 Comments

Might want to explain how to see that console.log output. I get the feeling OP is very new to this
My index.php page currently looks exactly like the one you wrote but it is still not echoing any value.
@Nomad heres a live demo hopefully this gives you the idea of the answer
The alert(response) seems to be working but there is no output on the html part. Should I just post the variable to another page?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.