Reputation: 2543
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.
Upvotes: 1
Views: 42
Reputation: 318372
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
});
Upvotes: 3