javascript - Select value of Radio button Selected and display in another div or forward back another PHP -
have table below mysql database 1. data send user i.e "schoolid" "studcourse" "studentsection" via ajax call php 2. php gives result username , 3 radio buttons present, absent , leave against student 3. need capture radio button data , send php clicked radio button , name or display in div fine.
data base
schoolid studentregid studentfirstname studcourse studentsection ft001 12kqc31085 abc bcom ft001 12kqc31086 def bcom ft001 12kqc31087 ghi bcom ft001 12kqc31088 jkl bcom $mysqli=mysqli_connect('localhost','uname','pass','database'); //data ajax $standard1 = trim($_post["tclass"]); $section1 = trim($_post["tsection"]); $schoolid1 = trim($_post["tschoolid"]); $query3="select * euser_student studcourse='$standard1' , schoolid='$schoolid1'and studentsection='$section1' order studentfirstname asc"; $data3=mysqli_query($mysqli,$query3)or die(mysqli_error()); while($row=mysqli_fetch_array($data3)){ $dat3 = $row['studentfirstname']; // data ajax display data in 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, 2 things.
- escape !!! don't ever use user data in query without escaping. (i use sprintf make queries, don't have to)
- post & get: post means user adds data website, comment. think pure thing
i think looks 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 school, section, class $('#my_form').submit(function(e) { // prevent form submitting , leaving webpage e.preventdefault(); // ajax call $.ajax({ url: 'ajax1.php', data: $(this).serialize(), // reads 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 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 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 own settings here $query3=sprintf(" select studentfirstname euser_student studcourse='%s' , schoolid='%s' , studentsection='%s' order studentfirstname asc" , mysql_real_escape_string(trim($_get["tclass"])) // don't ever ever use user data in query without escaping (or casting number), not in test phase. there 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 ajax display data in div // put student's name in 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'] , $_get['student'] arrays. foreach($_get['student'] $i=>$student) { $sql = sprintf( "insert student_present (studentid, present) values ('%s', '%s');" , mysql_real_escape_string(trim($_get['student'][$i])) , mysql_real_escape_string(trim($_get['present'][$i])) ); // ever need this. display sql query echo $sql . '<br>'; } ?>
Comments
Post a Comment