how to post while loop radio button values using Ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sree23
    New Member
    • Jul 2012
    • 1

    #1

    how to post while loop radio button values using Ajax

    i am fetching records from mysql database am using while loop for displaying in side while loop am using radio button for each record am using ajax for posting each radio button value
    but it is not working properly

    sql query is
    Code:
    <?php
    //db connection
     $query1=mysql_query("select * from questions ");
    	while($res1=mysql_fetch_array($query1))
          {
        
     ?>
    	Q)<?php echo $res1['id'];?>
    
    <b>
    <?php echo $res1['question'];?>
    </b>
    
    
     <table>
      <tr>
    	<td><input type="radio" name="ans<?php echo $res1['id'];?>" id="ans1" value="A"></td>
    	<td><strong>A</strong></td>
    	<td><?php echo $res1['answer1'];?></td>
      </tr>
      <input type="hidden" name="id" id="id" value="<?php echo $res1['id'];?>">
      <tr>
    	<td ><input type="radio" name="ans<?php echo $res1['id'];?>" id="ans2" value="B"></td>
    	<td><strong>B</strong></td>
    	<td><?php echo $res1['answer2'];?></td>
      </tr>
      <tr>
        <td><input type="radio" name="ans<?php echo $res1['id'];?>" id="ans3" value="C"></td>
        <td><strong>C</strong></td>
        <td><?php echo $res1['answer3'];?></td>
      </tr>
     
    </table>
    <?php
    }
    ?>
    =============== ===============
    for posting radio button values am using following ajax code but it is not working


    Code:
    $.ajax({
            type: "POST",
    		url: "count.php",
            dataType: "json",
            data: "id="+id+"&ans1="+ans1,
            cache:false,
             success: 
              function(data){
    			
    			if(id==100)
    			{
    				window.location = 'score.php';
    				opener.Reload();
    			}
    			else {
    			document.getElementById("statediv").innerHTML = "";
                $("#statediv").append(data);
    		    $("#form_message").html(data.message).css({'background-color' : data.bg_color}).fadeIn('slow');
               }
              }
            
            });
    
          return false;
    
        });
      
    
    });

    am new to ajax please solve this
    thanks in advance
    Last edited by gits; Jul 24 '12, 08:16 AM. Reason: added code tags
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You have to be more descriptive about what's wrong. Just saying it's not working properly doesn't help at all. What's not working properly? What is it doing that it's not supposed to? What's it not doing that it's supposed to? Are you getting any error messages? Do you have a screenshot of what it looks like when it's wrong? Do you have a screenshot of what it's supposed to look like? What browser are you testing on?

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      Ok first of all some basic problems with the html code you are streaming out in the code block on top.

      1) use proper indents so it is easy to see what belongs in which blocks. Use the tab key instead of the space bar to create your indents.

      2) your code did not stream out properly because you created a hidden element that is not in the table, right between row tags <tr> </tr>. Note the changes I made to your code. A) document header, body and form nodes. B) next I moved the table and end table tags outside your php while loop. No need to create a new table each time, just add more rows. C) I moved the question and hidden element inside the first row and cell. I also spanned the first row over two cells because that is what you have in the following rows with the A, B and C choices. D) notice I used th instead of td. That automatically bolds your text in the <th> </th> cell.

      3) aside from this cosmetic stuff you have some problems in naming your radio buttons. A) for each question you want the same "name" that will group/cluster those 3 choices so when the user clicks on one choice the checked button will be moved to that choice. Each time you loop to a new question you need a new name (this you have correct). Next you need a unique "id" for each radio button. No two buttons can have the same id (this you do not have correct - notice how I added your $res1['id'] to each button's id). You will need that when you finally get to loading your ajax info. To turn on a radio button you will have to reference it by the "id" and use the word "checked".

      You can copy and run this sample code:

      Code:
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
                  <title>Test Form</title>
              </head>
              <body>
                  <form>
                      <table>
          <?php
      		/*
      		-------------------------------------------------
              //db connection
              $query1=mysql_query("select * from questions ");
      
      		//demo questions instead of connection to data base
      		-------------------------------------------------
      		*/
      		$questions = array(
      			1 => array('id' =>'1','question'=>'Birds','answer1'=>'fly','answer2'=>'walk','answer3'=>'both'),
      			2 => array('id' =>'2','question'=>'Dogs have','answer1'=>'four legs','answer2'=>'three legs','answer3'=>'two legs'),
      			3 => array('id' =>'3','question'=>'Wht sounds do cats make','answer1'=>'bark','answer2'=>'meow','answer3'=>'chirp'),
      		);
      
      
      		/*
      		-------------------------------------------------
      		//for demo instead of looping the query
              while($res1=mysql_fetch_array($query1))
      		//we just loop the array
      		-------------------------------------------------
      		*/
      		foreach ($questions as &$res1)
      		{
           
          ?>
                          <tr style="text-align:left;">
                              <th colspan='2'>
                                  <br />
      							<input type="hidden" name="id" id="id" value="<?php echo $res1['id'];?>" />
                                  <?php echo $res1['id'];?>) <?php echo $res1['question'];?>
                              </th>
                          </tr>
                          <tr>
                              <td style="text-align:right;"><strong>A</strong><input type="radio" name="ans<?php echo $res1['id'];?>" id="ans1<?php echo $res1['id'];?>" value="A" /></td>
                              <td><?php echo $res1['answer1'];?></td>
                          </tr>
                          <tr>
                              <td style="text-align:right;"><strong>B</strong><input type="radio" name="ans<?php echo $res1['id'];?>" id="ans2<?php echo $res1['id'];?>" value="B" /></td>
                              <td><?php echo $res1['answer2'];?></td>
                          </tr>
                          <tr>
                              <td style="text-align:right;"><strong>C</strong><input type="radio" name="ans<?php echo $res1['id'];?>" id="ans3<?php echo $res1['id'];?>" value="C" /></td>
                              <td><?php echo $res1['answer3'];?></td>
                          </tr>
          <?php
              }
          ?>
                      </table>
                  </form>
              </body>
          </html>
      I have not addressed your ajax code because I am not sure what you are trying to do here. You usually use ajax to load new information into a html page that is already displayed on the user's browser. So the user would take some action that sends a request to the web server. The server then sends information back to the browser. Some javaScript in the html page parses the ajax data and updates the form. So you have some thinking to do on that part. Therefore you will have to have some ajax code run by the javaScript on the html form in the browser and you will have to have to ajax code running on the server. What is it you want to do?

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        Ok I get what you want to do with the ajax code. You want the questions submitted, scored and the results re-displayed.

        Do you want that scoring to occur with a submit button when all the questions have been answered?

        Or do you want to send each question to the server when the user click on one of the radio buttons?

        For an example of how to include the necessary ajax code go to this link http://www.w3schools.com/php/php_ajax_database.asp

        Comment

        Working...