drdawt9210 2015-06-19 07:57
浏览 25
已采纳

选择单选按钮选择的值并在另一个div中显示或向前转回另一个PHP

Have a table as below from MySQL Database 1. data is send from the user i.e "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>
  • 写回答

1条回答 默认 最新

  • dowjgrm6787 2015-06-19 10:53
    关注

    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>';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站