Reputation: 2425
I want to echo the post data i get using AJAX-JQUERY.
I have a dropdown in that dropdown i show some values from database. Now the item i select frmo dropdown is sent as POST to my controller.
If i open my network tab in browser, i can see the POST data i get using my AJAX script.
But if i try to echo it shows me nothing on browser.
I am using this script:
<script type="text/javascript">
$( ".specialLink" ).click(function() {
var site = this.id;
var url= "<?php echo base_url('customer/dashboard/index') ?>";
//get value for throw to controller
$.ajax({
type: "POST", //send with post
url: "<?php echo base_url('customer/dashboard/index') ?>",
data: {site:site},
success:function(data){
},
});
});
The DROPDOWN:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Your Sites <span class="caret"></span></a>
<ul class="dropdown-menu">
<?php
foreach($sites as $site)
{
echo "<li class='specialLink' id='".$site->site_key."'><a href='#'>".$site->site_key."</a></li>";
}
?>
</ul>
</li>
THE PHP SCRIPT
var_dump($_REQUEST);
var_dump($_POST);
Upvotes: 1
Views: 1716
Reputation: 944538
But if i try to echo it shows me nothing on browser
That is because you are making the request using Ajax instead of just loading it in the main window.
If you want to load the content in the main window, then just submit a regular form. If you must use JavaScript, then you can generate the form using DOM methods, add it to the page, then call its submit()
method.
(That said, you seem to be trying to simulate links, which would make a GET request more appropriate than a POST request and allow you to just use a regular link (or set location.href = ...
if you must use JS, which you really shouldn't need to for this).
If you want to use Ajax then you need to change this:
success:function(data){ },
… so that you actually do something with the value of data
(such as add it to the DOM of the current page).
Upvotes: 3
Reputation: 472
Please send data in this form in ajax call
data : {"site" : site};
key should be the string and then it's value in js variable.
Upvotes: 0