简体   繁体   中英

how to send multiple radio button values via ajax to php

I have multiple radio buttons differentiated by their name, here is my script

<?php
            if(mysqli_num_rows($result)>0){
                while($row = $result->fetch_assoc()){ ?>
                    <tr>
                        <td><?php echo $row['fullname'];?></td>
                        <td><?php echo $row['email'];?></td>
                        <td><?php echo $row['class'];?></td>
                        <td><input type="radio" value="present" name="<?php echo($row['id']); ?>" checked></td>
                        <td><input type="radio" value="absent" name="<?php echo($row['id']); ?>"></td>
                    </tr>
                <?php }
            }
            ?>

I want to pass their value to a php script, how I can pass each radio group value, Say if mysql query returned 10 rows then there are 10 radio button groups and I need to send all values of radio buttons. And my ajax call is

$("#submitAttendance").click(function(){
                $.ajax({
                    url: 'teacher.php',
                    method: 'post',
                    data:
                });
            }); 

You can use $(form_selector).serialize() that serializes all the values of the form and send to your server as an intended input.

$("#submitAttendance").click(function(){
    $.ajax({
        url: 'teacher.php',
        method: 'post',
        data: $(form_selector).serialize()
    });
});

Also, make sure you write this code in:

$(form_selector).submit()

Rather than attaching an event handler to the submit button.

So, a typical output of your form, say like this:

<form action="">
  <div>
    <label><input type="radio" name="student-1" id="" value="present"> Present</label>
    <label><input type="radio" name="student-1" id="" value="absent"> Absent</label>
  </div>
  <div>
    <label><input type="radio" name="student-2" id="" value="present"> Present</label>
    <label><input type="radio" name="student-2" id="" value="absent"> Absent</label>
  </div>
  <div>
    <label><input type="radio" name="student-3" id="" value="present"> Present</label>
    <label><input type="radio" name="student-3" id="" value="absent"> Absent</label>
  </div>
  <div>
    <label><input type="radio" name="student-4" id="" value="present"> Present</label>
    <label><input type="radio" name="student-4" id="" value="absent"> Absent</label>
  </div>
  <div>
    <label><input type="radio" name="student-5" id="" value="present"> Present</label>
    <label><input type="radio" name="student-5" id="" value="absent"> Absent</label>
  </div>
</form>

Would be:

"student-1=absent&student-2=present&student-3=absent&student-4=present&student-5=absent"

Is this what you are looking for? And you can get this using PHP using:

$_POST["student-1"]  // absent
$_POST["student-2"]  // present
$_POST["student-3"]  // absent
$_POST["student-4"]  // present
$_POST["student-5"]  // absent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM