javascript - Save DIV Data In Database Using Ajax To MySQL (using PHP Mysqli) -
have database below fetching data based on studentregid , studcourse , studentsection (are droupdown table) want send fetched data database using ajax , php on submit button click data radio button selected value , associated student name , 3 selected drop down values.
databse1
schoolid studentregid studentfirstname studcourse studentsection ft001 12kqc31085 abc bcom ft001 12kqc31086 def bcom ft001 12kqc31087 ghi bcom ft001 12kqc31088 jkl bcom
data data base
<div id="myspan4"> </div>
php:
$mysqli=mysqli_connect('localhost','root','root','db'); $standard1 = mysqli_real_escape_string($mysqli,trim($_post["tclass"])); $section1 = mysqli_real_escape_string($mysqli,trim($_post["tsection"])); $schoolid1 = mysqli_real_escape_string($mysqli,trim($_post["tschoolid"])); $query3="select * euser_student studcourse='$standard1' , schoolid='$schoolid1'and studentsection='$section1' order studentfirstname asc"; $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>'; ?>
javascript:
function activity3() { $('#subjects').change(function () { var selection = this.value; //grab value selected if ( selection=='0') { alert("select subject"); } else { httprequest = false; if (window.xmlhttprequest) { // mozilla, safari,... httprequest = new xmlhttprequest(); if (httprequest.overridemimetype) { httprequest.overridemimetype('text/html'); } } else if (window.activexobject) { // ie try { httprequest = new activexobject("msxml2.xmlhttp"); } catch (e) { try { httprequest = new activexobject("microsoft.xmlhttp"); } catch (e) {} } } if (!httprequest) { alert('cannot create xmlhttp instance'); return false; } var url = 'http://localhost:9999/attendence-php/fetch_student.php'; var pmeters = "tschoolid=" + encodeuri( localstorage.getitem("txtpassword1"))+ "&tclass=" + encodeuri(document.getelementbyid("datatosend").innerhtml)+ "&tsection=" + encodeuri(document.getelementbyid("datatosend1").innerhtml); httprequest.open('post',url,true); httprequest.setrequestheader("content-type", "application/x-www-form-urlencoded"); httprequest.setrequestheader("content-length", pmeters.length); httprequest.setrequestheader("connection", "close"); httprequest.send(pmeters); httprequest.onreadystatechange = function() { if(httprequest.readystate == 3) // loading request { $('#img').show(); //<----here } if(httprequest.readystate == 4) // return request { var retval = httprequest.responsetext; document.getelementbyid("myspan4").innerhtml = retval + '<input type="submit">'; } } } }); }
for trying send selected radio button value , name (not working)
on submit data send database
$(document).ready(function() { $('#myspan4').submit(function(e) { e.preventdefault(); $.ajax({ url: 'http://localhost:9999/attendence-php/ajax2.php', data: $(this).serialize(), // reads data ... success: function(data) { $('#message').html(data); } }); }); });
ajax2 php :was using php testing. b changed later associated table , field names
$servername = "localhost"; $username = "root"; $password = "root"; $dbname = "db"; $conn = new mysqli($servername, $username, $password, $dbname); // $_get['present'] , $_get['student'] arrays. $sql = "insert wsmessages (name,msg) values "; foreach($_get['student'] $i=>$student) { $sql .= sprintf("%s ('%s', '%s')" , ($i==0 ? '' : ',') // puts comma between rows (it skips first comma) , mysqli_real_escape_string($conn, trim($_get['student'][$i])) , mysqli_real_escape_string($conn, trim($_get['present'][$i])) ); } echo $sql; if ($conn->query($sql)) { // success }
Comments
Post a Comment