using JQuery and Ajax to post a form
Posted
by hopes
on Stack Overflow
See other posts from Stack Overflow
or by hopes
Published on 2010-06-08T13:09:12Z
Indexed on
2010/06/08
13:12 UTC
Read the original article
Hit count: 224
Hi there,
I want to use ajax to post my form
I want radiobuttons to do this(there is no submit buton)
so clicking a radiobutton will cause a form to be submitted or be post
my php page needs to know the name of the radio button clicked
is my code correct? cuz it doesn't work and I don't know why?
<div id="Q1">
<form id="Question1" method='post'>
<br />
<P> The sky color is..
</P><img border="0" src="images/wonder.png" width="94" height="134"/><br /><br /><br />
<input type="radio" id="option1" name="answer[1]" value="correct!"/> blue
<br />
<input type="radio" id="option1" name="answer[1]" value="false!"/> red
<br />
<input type="radio" id="option1" name="answer[1]" value="false!"/> green
<br />
<input type="radio" id="option1" name="answer[1]" value="false!"/> white
<br />
<br /> </Form>
<!-- <p id="greatp" style="display: none;"> GREAT !!<br /><br /><br /><br /><br /><br /><br /></p> -->
<img border="0" src="images/welldone3.png" width="230" height="182" id="greatp" style="display: none;"/>
<!-- <p id="sorryp" style="display: none;"> Sorry !!<br /><br /><br /><br /><br /><br /><br /></p> -->
<img border="0" src="images/failed3.png" width="230" height="182" id="sorryp" style="display: none;"/>
<img src="images/false2.png"id="myImage1"style="display: none;"/>
<img src="images/correct2.png"id="myImage2"style="display: none;"/>
<!-- <input type= "submit" onclick="Next1()" value="Next"/> -->
<input type="image" src="images/next.png" name="image" width="80" height="30" onclick="Next1()">
</div>
jquery cod:
<script>
$(document).ready(function(){
$("#option1").click(function() {
var answer= $("#option1").value;
var fff= $("#option1").name;
$.ajax({
type: "POST",
url: "page2.php",
data: fff,
success: function(){
if(answer=='correct!')
{
$('#greatp').show();
$('#myImage2').show();
$('#Question1').hide();
}
else
{
$('#sorryp').show();
$('#myImage1').show();
$('#Question1').hide();
}
}
});
return false;
});
});
</script>
© Stack Overflow or respective owner