1

I am trying to get the values of the first 9 clicked buttons and put them into an array named $u_input. I actually found this, but its in javascript.

my html:

 <div>
    <button name="btn1" type="submit" id="btn1" value="1" style="width:50%;height:40%;margin-left: auto;margin-right: auto; float:left" class="ui-btn"></button>
    <button name="btn2" type="submit" id="btn2" value="2" style="width:50%;height:40%;margin-left: auto;margin-right: auto;float:right" class="ui-btn"></button>
    </div> // I have up to 9 buttons, thats only a piece

my javascript:

$(document).ready(function(){
    $('.ui-btn').click(function(){
        var clickBtnValue = $(this).val();
        var ajaxurl = 'escreen.php',
        data =  {'action': clickBtnValue};
        $.post(ajaxurl, data, function (response) {
        });
    });

});

my php:

    if (isset($_POST['action'])) {

$u_input = array($_POST['action'](1), $_POST['action'](2), $_POST['action'](3), $_POST['action'](4));


echo $u_input;  
}
1
  • Change: <button id="btn-1"> value-1 </button> Commented Nov 7, 2015 at 12:47

1 Answer 1

2

Just loop through the buttons with the following code:

jQuery(document).ready(function($){
    var ajaxurl = 'escreen.php',
    $values = [];
    $('button').each(function(){
        $values.push($(this).val());
    });
    $.ajax({
        url: ajaxurl,
        type: 'post',
        data: {
            buttons: $values,
        },
        success: function (result) {
            console.log('Yay it worked');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log('Something went wrong');
        }
    });
});

An even nicer way to do it would be to set a name for each property in your array:

jQuery(document).ready(function($){
    var ajaxurl = 'escreen.php',
    $values = [];
    $('button').each(function(){
        $values[$(this).attr('name')] = $(this).val();
    });
    $.ajax({
        url: ajaxurl,
        type: 'post',
        data: {
            buttons: $values,
        },
        success: function (result) {
            console.log('Yay it worked');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.log('Something went wrong');
        }
    });
});

The array $values should contain the following:

[button1: "value1", button2: "value2", button3: "value3"]

So in your PHP file you can retreive it by using:

$_POST['buttons']['button1']

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer! I think I take the first code snippet, because I have 9 different buttons and I need only a array with numbers. :)
Ok that's fine, but then you should not us the $_POST['buttons']['button1'], but just $_POST['buttons']
How can I echo the resulting array in php? :D
You can use echo $_POST['buttons'][0] if i am right if you use option 1, or just do an var_dump($_POST['buttons']) to see what is going on. Or do an foreach($_POST['buttons'] as $k => $v){ var_dump($v); }

Your Answer

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.