My problem is somewhat the same as this post but the accepted answer on that did not work for me. I had also tried to use e.preventDefault()
referencing from
this post but I still faced the same problem.
My form comes from a php file which are populated from the result set of a query
<?php
require "conn.php";
$load_questions = $conn->query("Select * from sqlearn_questions");
$questions = '';
while($row = $load_questions->fetch_assoc()){
$questions .= '
<form class = "add-question-form" >
<table class = "question_item">
<tr>
<td colspan = "3">
<p id = "qitem_header">From: <input id = "qitem_header" type = "" name = "user_id" value = "' . $row["USER_ID"] . '"></p>
</td>
</tr>
<tr>
<td id = "td_inst"><input type = "" name = "instruction" value = "' . $row["INSTRUCTION"] . '"</td>
<td><input id = "qitem" type = "" name = "question" value = "' . $row["QUESTION"] . '"</td>
<td><input id = "qitem" type = "" name = "answer" value = "' . $row["ANSWER"] . '"</td>
</tr>
<tr>
<td></td><td></td>
<td id = "td_btn_holder"><button id = "#qitem_post">Post</button></td>
</tr>
</table>
</form>';
}
echo $questions;
?>
Here's my js script
$("#qitem_post").click(function(e){
e.preventDefault();
var data = $(".add-question-form").serialize();
$.post("add-question.php", data)
.done(function(msg){ alert(msg); });
});
and here's my html content:
<html>
<title>SQLearn</title>
<link rel = "stylesheet" type = "text/css" href = "style/ui_layout.css" />
<script src = "javascript/jquery-3.2.0.js"></script>
<script src = "javascript/script.js"></script>
<div class = "container">
<img id = "header_img" src = "images/sqlearn_header.png" width = "200" height = "100" />
<div class = "login_layout">
<input class = "login_inputs" id = "aid" type = "text" /><br />
<input class = "login_inputs" id = "password" type = "password" /><br />
<button id = "login_btn">Sign In</button>
<p id = "login_response"></p>
</div>
<nav>
<ul id = "nav_list">
<li>Admin</li>
<li>Questions</li>
<li>Reviews</li>
<li>Reports</li>
</ul>
</nav>
<div class = "review_questions">
<!-- The content of this div will be populated from javascript -->
</div>
</div>
My theory is the problem is caused by my php file where I echoed the form. Hope someone could explain to me my error. Thanks !