I am using a PHP script, but say I had two radio buttons, right?
How could I actually execute code, such as (main intention | display a messagebox) upon selection of one or the other?
Say I had a radio button named RadioButton1
, Once checked/selected
, a message box
would appear saying RadioButton1 Selected
?
Is this possible through PHP alone? Or do I need to integrate an html page which posts to the PHP page?
I am using a PHP script, but say I had two radio buttons, right?
How could I actually execute code, such as (main intention | display a messagebox) upon selection of one or the other?
Say I had a radio button named RadioButton1
, Once checked/selected
, a message box
would appear saying RadioButton1 Selected
?
Is this possible through PHP alone? Or do I need to integrate an html page which posts to the PHP page?
Share Improve this question edited Nov 28, 2012 at 8:23 Romén Rodríguez-Gil 4032 silver badges17 bronze badges asked Nov 28, 2012 at 7:51 Josh LineJosh Line 6353 gold badges13 silver badges27 bronze badges 2- It's not possible with PHP. Try JavaScript with AJAX – Gerald Schneider Commented Nov 28, 2012 at 7:52
- No, PHP is server-side, while what you want is client-side which is what JavaScript does. – Alvin Wong Commented Nov 28, 2012 at 7:53
5 Answers
Reset to default 3Use Javascript for client side interaction like that. The code below listens for the onchange
event and shows an alert()
.
jsFiddle Demo
<input type="radio" name="myradio" value="RadioButton1" />
<input type="radio" name="myradio" value="RadioButton2" />
<input type="radio" name="myradio" value="RadioButton3" />
<script>
window.onload = function()
{
var radios = document.getElementsByName("myradio");
for(var i=0; i<radios.length; i++)
{
radios[i].onchange = function()
{
if(this.checked)
{
alert(this.value + " selected");
}
}
}
}
</script>
The first 3 lines are the radio buttons HTML. After that we have the <script>
tag which denotes Javascript code. The Javascript is adding some code to the onload
event, which simply means: execute this code when the page is loaded. Next we get all of the radio button elements into an array called radios
- for that we use getElementsByName()
passing the radio button group name which is myradio
. Next we loop through each radio button in the array and assign an onchange
handler, which means: execute this code when each radio button is changed. Within that, we check if the radio button is checked
and if it is, we show the alert, showing the radio button's value which will be RadioButton1
, RadioButton2
, RadioButton3
.
Not possible with just php! Try using Jquery as the easiest was to do this
$(document.body).on('click', '#radio-btn', function(){
$.get( 'file1.php' , function (data) {
//whatever you want to do after fetching the data from a php file
});
});
Selecting a form element is done in the client's browser, while PHP is a server-side language. It is absolutely unaware of what the user clicks until some data is actually sent back to the server, e.g., via a POST request upon submitting a form.
So no, PHP isn't capable of achieving what you are after.
There's an easy way though. JavaScript is executed on the client side, so you can easily attach an event listener to your radio buttons and display a message box if needed.
Using php alone its not possible, but you can do it using Jquery ajax. To do this make a ajax request on click of radio button, and populate the message box with the data ming in response. Let me explain with an example:
<div id='msg_box'>Message will be displayed here</div>
on click of radio button call a function of javascript say ajaxCallForMessage()
<script type="text/javascript">
function ajaxCallForMessage(){
$.ajax({
url: "Url of the page which contain message/?btn_name=xy",
mthod: "GET"
}).done(function ( data ) {
$('#msg_box').append(data);
});
}
</script>
make sure you included jquery.
If I understood your question properly, I would suggest to do it simply via Javascript.
Once the user selects the RadioButton1, the "click" event is triggered in the page. I guess you know that you can capture it adding the onClick
attribute like:
<input type="radio" name="Radio1" value="RadioOption1" onclick="showMessage()"> Option 1
Then all you need to do is to create a Javascript funcion showMessage
that adds some html to the page (maybe a paragraph) with the message you want to display. You can do this in Javascript easily, using for example the jQuery append or html functions.
function showMessage() {
// Example displaying an alert
alert("Message to be displayed here")
}
I would only introduce PHP here if there is really a need to obtain information from the server. In this case what you should be doing is probably a GET / POST from the page to the server (e.g. using AJAX via jQuery get or post method). You will call a PHP script that returns some information that then will be displayed in the page through Javascript.
But if all you need is to display a simple message like "Option 1 selected" you should do it in Javascript without server interaction.
I hope this helps.
Regards, Romén
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744331823a4568924.html
评论列表(0条)