tags:

views:

62

answers:

1

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>
+1  A: 

First off, an ID on a tag can only exist for one tag, it's a unique identifier. That would be your first problem jQuery will grab the first element it finds using $("#someId").

Change that to a class and that should help. ie. $(".someClass") or don't uses classes at all and do something like $("input:radio").click...

For the data, you're pretty close, but I think that won't quite work as you need to send the data as a key/value pair. You can achieve this using a query string or just a straight up json object. Something like:

var theData = {
 name: $(this).attr("name"),
 value: $(this).val()
};

$.ajax({
  data: theData
  ...
});

it's the this that you're looking for. this is set to whatever radio button is clicked, right now you're hardcoding again to the first radio button with ID #option1 which is wrong.

Also, consider looking at the $.get and $.post functions as their slightly simpler than the $.ajax. They're also more terse so it leads to less code to achieve the same thing.

brad
thank u brad what about data?how to give page2.php the thing it needs? in this case how to give it the name of the radiobutton? is what I did correct?
Check my edit...
brad