Can I post an ajax value to a same page. And Handle with php $_GET, $_POST, or $_REQUEST

2.6k Views Asked by At

Here I'm attaching the image of my code. I need to post an id to the same page when I clicking a link, and also need to pop-up a bootstrap model window. I'm worked with several methods. The value is available at console and fire bug inspect. But I could not get the id using $_GET,$_POST or $_REQUEST methods. Any Can you help me?

<td><p data-placement="top" data-toggle="tooltip"  title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" id="edit-button" data-id="26" ><span class="fa fa-pencil"></span></button></p></td>

<script>
$('#edit-button').click(function(){
    var eid = $(this).data('id');
    $.ajax
    ({ 
        url: 'managers.php',
        data: {"eid": eid},
        type: 'post',
        success: function(result)
        {
            console.log(eid);
        }
    });
});
</script>

enter image description here

1

There are 1 best solutions below

7
On

It appears that the ajax request succeeds but returns the entire page rather than the specific data you need. To alleviate this check for the request method (POST) and for the existence of the particular variable (eid) and, before sending any data back, clear the output buffer (ob_clean) - proceed with processing and then exit/die.

<?php

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['eid'] ) ){
        ob_clean();

        /* other code presumably to do something with POSTed data */

        /* send some sort of response */
        echo $_POST['eid'];


        exit();
    }

?>

Complete example of returning only required data rather than entire page.

<html>
  <head>
    <script src="//code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    </head>
    <body>
        <p>This HTML data will not be part of the ajax response, only "the value of eid: X"</p>


        <input type='button' id='edit-button' data-id=303 value='Edit' />
        <script>
        $('#edit-button').click(function(){
            var eid = $(this).data('id');
            $.ajax
            ({ 
                url: 'simplejquery.php',/* chenge this to suit your url */
                data: {"eid": eid},
                type: 'post',
                success: function(result)
                {
                    console.log(result);
                    alert(result)
                }
            });
        });
        </script>
    </body>
</html>