简体   繁体   中英

Select value of Radio button Selected and display in another div or forward back another PHP

Have a table as below from MySQL Database 1. data is send from the user ie "SchoolID" "StudCourse" "StudentSection" via ajax call to PHP 2. php gives a result which is username and three radio buttons Present, absent and Leave against that student 3. what i need is to capture the radio button data and send back to another PHP with that clicked radio button and the name or display in another div is also fine.

Data base

  SchoolID   StudentRegID    StudentFirstName StudCourse   StudentSection 
    FT001     12KQC31085        ABC             BCOM             A
    FT001     12KQC31086        DEF             BCOM             A        
    FT001     12KQC31087        GHI             BCOM             A  
    FT001     12KQC31088        JKL             BCOM             A  


$mysqli=mysqli_connect('localhost','Uname','Pass','Database');

       //data from ajax
$standard1 = trim($_POST["tclass"]);
$section1 = trim($_POST["tsection"]);
$SchoolID1 = trim($_POST["tschoolid"]);

$query3="SELECT * FROM euser_student  WHERE  StudCourse='$standard1' and SchoolID='$SchoolID1'and StudentSection='$section1' order by StudentFirstName   ASC";
$data3=mysqli_query($mysqli,$query3)or die(mysqli_error());

while($row=mysqli_fetch_array($data3)){
    $dat3 = $row['StudentFirstName'];


    // data to ajax to display data in a div
    echo "<table><tr><td>".$dat3."</td><td><input name='".$dat3."' type='radio' value='Present'>Present</td><td><input name='".$dat3."' type='radio' value='Absent'>Absent</td><td><input name='".$dat3."' type='radio' value='Leave'>Leave</td></tr></table>";
}

Final Out Put

                ABC         O Present   O Absent    O Leave
                DEF         O Present   O Absent    O Leave
                GHI         O Present   O Absent    O Leave
                JKL         O Present   O Absent    O Leave


<input type="submit" id="submit_data" > </input>

First, two things.

  • Escape !!! Don't ever use user data in a query without escaping. (I use sprintf to make queries, you don't have to)
  • POST & GET: Post means the user adds data to the website, like a comment. I think this is a pure GET thing

I think this looks like what you want.

index.php

<form id="my_form" method="get" action="ajax1.php">
  <input name="tclass" value="BCOM">
  <input name="tschoolid" value="FT001">
  <input name="tsection" value="A">
  <input type="submit" value="GO">
</form>
<hr>
<form id="my_radios"></form>
<hr>
<div id="message"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
  // client selects the school, section, class
  $('#my_form').submit(function(e) {
    // prevent the form submitting and leaving the webpage
    e.preventDefault();
    // ajax call
    $.ajax({
      url: 'ajax1.php',
      data: $(this).serialize(),    // reads the data ...
      success: function(data) {
        $('#my_radios').html(data + '<input type="submit">');
      }
    });
  });
  // radio buttons
  $('#my_radios').submit(function(e) {
    e.preventDefault();
    $.ajax({
      url: 'ajax2.php',
      data: $(this).serialize(),    // reads the data ...
      success: function(data) {
        $('#message').html(data);
      }
    });
  });
});
</script>

ajax1.php

<?php
/*
CREATE TABLE IF NOT EXISTS euser_student (
  SchoolID varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  StudentRegID varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  StudentFirstName varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  StudCourse varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  StudentSection varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

INSERT INTO euser_student (SchoolID, StudentRegID, StudentFirstName, StudCourse, StudentSection) VALUES
('FT001', '12KQC31085', 'ABC', 'BCOM', 'A'),
('FT001', '12KQC31086', 'DEF', 'BCOM', 'A'),
('FT001', '12KQC31087', 'GHI', 'BCOM', 'A'),
('FT001', '12KQC31088', 'JKL', 'BCOM', 'A')
*/

$mysqli=mysqli_connect('localhost', 'root', '', 'stackoverflow');  // put your own settings here
$query3=sprintf("
  SELECT StudentFirstName FROM euser_student 
  WHERE StudCourse='%s' and SchoolID='%s' AND StudentSection='%s' 
  ORDER BY StudentFirstName ASC"
    , mysql_real_escape_string(trim($_GET["tclass"]))  // don't ever ever use user data in a query without escaping (or casting to number), not even in test phase.  There is absolutely no excuse
    , mysql_real_escape_string(trim($_GET["tschoolid"]))
    , mysql_real_escape_string(trim($_GET["tsection"]))
);
$res3=mysqli_query($mysqli, $query3);
echo '<table border="1">';
for($i=0; $row=mysqli_fetch_assoc($res3); $i++) {
  $dat3 = $row['StudentFirstName'];
  // data to ajax to display data in a div
  // we put the student's name in a hidden input
  echo "<tr>
    <td>" . $dat3 . " <input type='hidden' name='student[" . $i . "]' value='" . $dat3 . "'></td>
    <td><input name='present[" . $i . "]' type='radio' value='Present'>Present</td>
    <td><input name='present[" . $i . "]' type='radio' value='Absent'>Absent</td>
    <td><input name='present[" . $i . "]' type='radio' value='Leave'>Leave</td>
  </tr>";
}
echo '</table>';
?>

ajax2.php

<?php
// $_GET['present'] and $_GET['student'] are arrays.
foreach($_GET['student'] as $i=>$student) {
  $sql = sprintf(
    "INSERT INTO student_present (studentID, present) VALUES ('%s', '%s');"
      , mysql_real_escape_string(trim($_GET['student'][$i]))
      , mysql_real_escape_string(trim($_GET['present'][$i]))
  );
  // do what ever you need to do with this.  I just display the sql query
  echo $sql . '<br>';
}
?>

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