5
I’m trying to send a data to a PHP page with AJAX, but I can’t figure out how to make it work, I’ll explain what I have so far!
The dropdown menu has the following code:
<div class="btn-group">
<button class="btn btn-danger btn-lg dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
BOTÂO <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" id="status">
<li data="1"><a href="#">valor 1</a></li>
<li data="2"><a href="#">valor 2</a></li>
</ul>
</div>
And AJAX is like this:
$(document).on('click','#status li',function(){
$.ajax({
url: 'exemple.php',
type: 'POST',
data: {
'valor': $('#data').val()
},
});
});
Is it possible to send the value of a dropdown menu made in Bootstrap? How can I get the value on the exemple.php page? I think the AJAX part is wrong, but it’s not working!
I removed the '' quotes, but it didn’t work anyway, the value of the dropdown-menu is still not passed to the PHP page, the variable remains Undefined index. Any more suggestions?
– Luis
Luis, I just tested this code on the JS Bin online console and the request goes correctly to the php file. Could you post the snippet of the PHP code that receives this request? So we can get a better sense.
– ECJ
Actually I am trying to receive two variables on the PHP page, they may be conflicting? I can post the AJAX code of both if it helps! <? php $a = $_POST['name']; $b = $_POST['value']; ?>
– Luis
Conflict no, but the undefined index error may be coming from the other variable. Have you checked this? You can avoid this type of error by testing the variable with "isset()" before using it.
– ECJ
If you use distinct ajax codes for variables, obviously requests occur at different times. So, when you submit one of the variables, you will not be sending the other and you will always have the error occurring in any of the requests. No doubt you need to control this with "isset()" to process only the part of code corresponding to the variable you are sending at any given time.
– ECJ
Even with isset() in PHP it is no mistake, I think the whole problem is in the way AJAX is written, has how to take a look at the code and show me how I can aggregate the two AJAX requests in 1 only, for when to click the button being to PHP it send the two variables at the same time? http://jsfiddle.net/Luis/qxckcwmo/4/
– Luis