Dropdown-menu Bootstrap with PHP

5

I'm trying to send a data to a PHP page with AJAX, but I can not 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 looks 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 this is not being solved!

    
asked by anonymous 31.01.2015 / 04:16

3 answers

2

If you do this (just an example), you can send everything in a request only for page.php:

    <div class="btn-group">
    <button id="drop" class="btn btn-default 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-something="1"><a href="#">valor 1</a></li>
        <li data-something="2"><a href="#">valor 2</a></li>
    </ul>
    <input type='hidden' id='selected_data'>
</div>
<br><br>
<div id='output'></div> 


<div class="modal-body">
<textarea id="area_1" name="area_1" class="form-control" rows="7"></textarea>
<br><br>    
<!-- BUTTON TO SEND THE VALUE AND THE TEXTAREA TO PHP PAGE-->    
<button type="button" id="enviar" class="btn btn-default">SEND TO PHP</button> 
$(document).on('click','#status li',function(){
        $('#selected_data').val($(this).data('something'));
    });

$(document).on('click','#enviar',function(){
        $.ajax({
            url: 'page.php',
            type: 'POST',
            data: {
                    valor: $('#selected_data').val(),
                    plaintext: $('#area_1').val(),                  
                  },

            success: function(resposta){
                $("#area_1").html(resposta);
            }
        }).done(function(data){
           alert("Successfull", data);          
       });
    });
    
31.01.2015 / 19:41
2

Luis, the way to get the value of the attribute, as André Ribeiro has passed, is correct. The error seems to be in the way you are constructing the "option data" of JQuery.ajax. Remove the quotation marks from the "value" index, like this:

$(document).on('click','#status li',function(){
$.ajax({
    url: 'exemple.php',
    type: 'POST',
    data: {valor: $(this).attr('data')}
    });
}); 
    
31.01.2015 / 13:00
1

To get the value of the data attribute from the li of your menu you can use the .attr( attributeName ) function.

In your code it would look like this:

$(document).on('click','#status li',function(){
  $.ajax({
        url: 'exemple.php',
        type: 'POST',
        data: {
               'valor': $(this).attr('data')
              },
        });
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="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>
    
31.01.2015 / 04:31