I am completely new to javascript/jquery, and would appreciate any help.
I am having trouble with the function $.post
because I am using radio in the form. I need to use the value of the chosen radio in a different file, so that I can process what should be outputted, and then I want to output something in place of where the form is.
Here is the form with type radio input:
<div id='poll'> <form name='poll_form' id='poll_form'> <INPUT TYPE="radio" name='poll' value ='poll1'/>Option1<br/> <INPUT TYPE="radio" name='poll' value='poll2' />Option2<br/> <INPUT TYPE="radio" name='poll' value='poll3'/>Option3<br/> <INPUT TYPE="radio" name='poll' value='poll4'/>Option4</br> <INPUT TYPE='button' value='Submit Vote' onClick="vote();" /> </form> </div>
Here is the javascript/jquery to define the “vote();” function:
<head> <script type = "text/javascript" src="jquery.js"></script> <script type = "text/javascript"> function vote() { $.post('file.php',$('input:radio[name=poll]:checked').val(), function(output){ $("#poll").html(output).show(); }); }; </script> </head>
Is $('input:radio[name=poll]:checked').val()
the correct thing to use? And if so, how do I retrieve the value of $('input:radio[name=poll]:checked').val()
in file.php?
Advertisement
Answer
To post values you would have to declare a post-variable and assign your value to it, i.e.
$.post('file.php',{ poll: $('input:radio[name='poll']:checked').val() }, function() { $("#poll").html(output).show(); });
In your PHP file you can access the value via
$_POST['poll']