1

It might be a simple thing, however I spent way too much time unsuccessfuly trying to make it work.

Basically I have the following Bootstrap buttons:

<div class="bs-example">
    <div class="btn-group" data-toggle="buttons">
        <label title="This will display" class="btn btn-lg btn-info">
        <input type="radio" name="toDo" id="toDo" value="enableSSH"> Enable SSH
    </label>
    <label class="btn btn-lg btn-info">
        <input type="radio" name="toDo" id="toDo" value="migrateDB"> Migrate Database
    </label>
</div>

I have the following button lower in the page that I need to use with the above selected value:

<button id="submit" class="btn btn-default" type="button">Go!</button>

When the button is clicked, it triggers the following jQuery function:

$("#submit").click(function(){
    var buttonValue = $('#toDo').val();
    var phpfile = "submit.php";
    var data =  {'action': buttonValue};
    $.post(phpfile, data, function (response) {
        alert("Data has been submitted successfully);
    });
});

This should pass the result of the selected Radio Button to this PHP script:

<?php
if (isset($_POST['action'])) {
    switch ($_POST['action']) {
        case 'enableSSH':
            alert("enableSSH has been selected");
            break;
        case 'migrateDB':
            alert("migrateDB has been selected");
            break;
    }
}
?>

The alert that the data has been submitted from the jQuery function appears properly, however the alerts from the PHP script confirming that it has been passed are not appearing.

Sorry for the lengthy post. Please help.

1
  • 1
    I suspect your PHP logs are filled with errors from this alert() function that doesn't exist... Commented Jun 25, 2015 at 16:03

1 Answer 1

3

There is no alert in PHP, it's a serverside language that has nowhere to alert.

The solution is to return something to the ajax call instead

<?php

    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'enableSSH':
                echo "enableSSH has been selected";
                break;
            case 'migrateDB':
                echo "migrateDB has been selected";
                break;
        }
    }

?>

and you'd get it as the response

$.post(phpfile, data, function (response) {
    alert("Data has been submitted successfully");
    alert(reponse); // alert it here instead
});
Sign up to request clarification or add additional context in comments.

3 Comments

Oh, you are actually absolutely correct. I set it up in the way that you suggested, and it's returning the echo alert properly (after I fixed the little typo in the above.
Really sorry for being a noob, however it seems to always think that the button selected is the first one, even when I select the second one. I was under the impression that Radio Buttons return a value only when pressed. Not sure why it would do this :/
Actually I answered my second question - my understanding above has been incorrect and the way to use the select button is by using the following format in jQuery: var buttonValue = $('[name="toDo"]:checked').val(); :checked indicates which one is currently selected. Thank you very much for the help and I marked your answer as correct. :)

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.