【发布时间】:2015-07-20 11:38:30
【问题描述】:
我正在尝试使用 AJAX/JQUERY/PHP(MVC) 将数据发送到数据库;
我有一张包含大量数据的表格;该表包含用于检查可以发送到数据库的行的复选框。
对于查看文件;我试图发送这样的数据:
已编辑
<form action="<?php echo URL ?>etudiants/affectation" method="POST" id="affectStudents" class='form-vertical'>
<div class="span3">
<div class="control-group">
<div class="input-append">
<select name="id_salle" id="id_salle" class='input-medium'>
<option value="0">«Choisir Salle»</option>
<?php foreach($this->fetchSalle AS $key => $value): ?>
<option value="<?php echo $value['id_salle'] ?>">
<?php echo $value['intitule_salle']; ?>
=> C: <?php echo $value['capacite']; ?>
=> M: <?php echo $value['marge']; ?>
</option>
<?php endforeach; ?>
</select>
<button class="btn" type="submit"> Affecter</button>
</div>
</div>
</div>
<div class="box-content nopadding">
<table class="table table-hover table-nomargin table-bordered dataTable dataTable-nosort" data-nosort="0">
<thead>
<tr class='thefilter'>
<th class='with-checkbox'>
<input type="checkbox" name="check_all" id="check_all"></th>
<th>Code Etud.</th>
<th>Filière</th>
<th>Nom et Prénom</th>
<th>CIN</th>
<th>Tel</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php foreach($this->fetchEtudiants AS $value): ?>
<tr>
<td class="with-checkbox">
<input type="checkbox" name="id_etudiants[]" value="<?php echo $value['id_etudiant']; ?>" class="check_id" id="check_id">
</td>
<td><?php echo $value['code_etudiant']; ?></td>
<td><?php echo $value['intitule_filiere']; ?></td>
<td><?php echo $value['nom']; ?>
<?php echo $value['prenom']; ?>
</td>
<td><?php echo $value['cin']; ?></td>
<td><?php echo $value['tel']; ?></td>
<td><?php echo $value['email']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="hidden" name="csem" value="<?php echo @$_GET['csem'] ?>">
<input type="hidden" name="cses" value="<?php echo @$_GET['cses'] ?>">
<input type="hidden" name="cfil" value="<?php echo @$_GET['cfil'] ?>">
<input type="hidden" name="cmod" value="<?php echo @$_GET['cmod'] ?>">
</form>
控制器文件
etudiants.php
class Etudiants extends Controller {
function __construct(){
parent::__construct();
}
public function index(){
$this->view->render('etudiants/index');
}
function affectation(){
$this->model->affectation();
}
}
etudiants_model.php
class Etudiants_Model extends Model
{
public function __construct(){
parent::__construct();
}
public function affectation(){
$students[] = $_POST['id_etudiants'];
$id_salle = $_POST['id_salle'];
for ($i=1; $i <= strlen($students) ; $i++) {
$stmt = $this->db->prepare("INSERT INTO exam_salles (id_student, id_salle) VALUES(?,?)");
$stmt->execute(array($students[$i], $id_salle));
}
if($stmt == TRUE){
echo "good";
}else{
echo "wrong";
}
}
}
对于 JS 文件我使用了这段代码。
$(document).ready(function(){
$("#affectStudents").on('submit', function(e){
e.preventDefault();
var url = $(this).attr('action');
var data = $(this).serialize();
$.post(url, data, function(response) {
if(response == 'good'){
$("#affectedSuccessfully").show();
}else{
$("#affectedError").show();
}
});
});
});
当我点击按钮 SEND 时,它会将我重定向到 /etudiants/affectation,即使我使用的是 preventDefault(); 和不向数据库发送数据。
【问题讨论】:
-
console.log $("#affectStudents").确保在 DOM 准备好之后运行 JS。
-
您发布的 HTML 显示 SEND 是文本字段,而不是按钮,您确定您发布的代码正确吗?
-
我更新了代码,这里只是一个错字
标签: php jquery ajax database model-view-controller