I have a form that is echoed out from the database, but the issue is that when I try to submit, only the first echoed form submits and the rest doesn't. Below is my code.
editquestion.phh
<thead> <tr> <th style="width: 5%;">S/N</th> <th style="width: 20%;">QUESTION</th> <th style="width: 40%;">ANSWER</th> <th style="width: 30%;">KEYWORDS</th> <th style="width: 5%;">SAVE/UPDATE</th> </tr> </thead> <tbody> <?php $sql = $db->prepare("SELECT * FROM questions"); $result = $sql->execute(); while ($row = $result->fetchArray(SQLITE3_ASSOC)) { $quiz_id = $row['quiz_id']; $question = $row['question']; $answer = $row['answer']; $keywords = $row['keywords']; echo '<form action="updatequestion.php" method="post" enctype="multipart/form-data"> <tr> <td><input style="width: 50%" type="text" name="cid" id="cid" value="'.$quiz_id.'"></td> <td><input type="text" name="question" id="question" value="'.$question.'"></td> <td><input type="text" name="answer" id="answer" value="'.$answer.'"></td> <td><input type="text" name="keywords" id="keywords" value="'.$keywords.'"></td> <td><input type="submit" name="qupdate" class="qupdate" value="Update"></td> </tr> </form>'; } ?> </tbody> </table> qupdate.js
$(document).ready(function() { $('.qupdate').click(function() { question = $('#question').val(); answer = $('#answer').val(); keywords = $('#keywords').val(); id = $('#cid').val(); $.ajax({ type: "POST", url: "updatequestion.php", data: "cid="+id+"&question="+question+"&answer="+answer+"&keywords="+keywords, success: function(html){ if(html = "true") { $('.qupdate').css("opacity", "1"); } else { alert("not successful"); } }, beforeSend: function(){ $('.qupdate').css("opacity", "0.5"); } }); return false; }); }); Just added the code for updatequestion.php.
<?php session_start(); require_once("db.php"); $db = new MyDB(); if (isset($_POST['question']) || isset($_POST['answer']) || isset($_POST['cid'])) { $id = strip_tags(@$_POST['cid']); $cname = strip_tags(@$_POST['question']); $cunit = strip_tags(@$_POST['answer']); $keywords = strip_tags(@$_POST['keywords']); if (empty($cname) || empty($cunit)) { echo "fill"; } else { $sql = $db->prepare("UPDATE questions SET question = ?, answer = ?, keywords = ? WHERE quiz_id = ?"); $sql->bindParam(1, $cname, SQLITE3_TEXT); $sql->bindParam(2, $cunit, SQLITE3_TEXT); $sql->bindParam(3, $keywords, SQLITE3_TEXT); $sql->bindParam(4, $id, SQLITE3_INTEGER); $result = $sql->execute(); if ($result) { echo "true"; } else { echo "false"; } } } ?> But the ajax seems to only work for the first echoed data and doesn't seem to submit the rest. How do I solve this?
Thanks in advance.