我有很多麻烦将Ajax,Jquery,Javascript实现到循环中。 目前我有一个"酒吧评级系统"我想在do-while循环中使用并重复每个记录。但是我无法弄清楚的是错误的。
与此问题相关的代码部分:
<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formevalcoment")) {
$insertSQL = sprintf("INSERT INTO tblevaluacion (idUser, idComent, intEvaluacion, dateFecha, intActivo) VALUES (%s,%s,%s,%s,%s)",
GetSQLValueString($_POST['idUser'], "int"),
GetSQLValueString($_POST['idComent'], "int"),
GetSQLValueString($_POST['intEvaluacion'], "int"),
GetSQLValueString($_POST['dateFecha'], "timestamp"),
GetSQLValueString($_POST['intActivo'], "int"));
mysql_select_db($database_conexionproject, $conexionproject);
$Result1 = mysql_query($insertSQL, $conexionproject) or die(mysql_error());
$insertGoTo = "";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<?php
if ($totalRows_DatosComentarios > 0) { // Show if recordset not empty
do {
<p class="eval_coment">Update your current evaluation</p>
<form id="<?php echo 'form-evalcoment'.$m ?>" name="<?php echo 'form-evalcoment'.$m ?>" class="eval-form">
<select class="bars-reversed" id="<?php echo 'bars-reversed'.$m; ?>" name="intEvaluacion" style="display:none" data-number="<?php echo $m; ?>">
<option value="-2" style="color:red" <?php if (!(strcmp(-2, htmlentities(ObtenerEvalUsuario($row_DatosComentarios['idComent']), ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>>Muy en desacuerdo (--)</option>
<option value="-1" style="color:red" <?php if (!(strcmp(-1, htmlentities(ObtenerEvalUsuario($row_DatosComentarios['idComent']), ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>>En desacuerdo (-)</option>
<option value="0" <?php if (!(strcmp(0, htmlentities(ObtenerEvalUsuario($row_DatosComentarios['idComent']), ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>>No aporta (0)</option>
<option value="1" style="color:blue" <?php if (!(strcmp(1, htmlentities(ObtenerEvalUsuario($row_DatosComentarios['idComent']), ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>>Deacuerdo (+)</option>
<option value="2" style="color:blue" <?php if (!(strcmp(2, htmlentities(ObtenerEvalUsuario($row_DatosComentarios['idComent']), ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>>Muy deacuerdo (++)</option>
</select>
<input type="hidden" name="dateFecha" value="now()" />
<input type="hidden" name="intActivo" value="1" />
<input type="hidden" name="MM_insert" value="formevalcoment" />
<input type="hidden" name="idComent_persopoli" value="<?php echo $row_DatosComentarios_CaractPoli['idComent_persopoli']; ?>" />
<input type="hidden" name="idUser" value="<?php echo $_SESSION['MM_IdUser']; ?>" />
</form>
<?php
$m++;
} while ($row_DatosComentarios = mysql_fetch_assoc($DatosComentarios));
在身体标签结束之前:
<script src="assets/jquery.barrating.js"></script>
<script type="text/javascript">
$(function() {
$('.bars-reversed').barrating({
theme: 'bars-reversed'
});
});
</script>
<script>
$('.eval-form').submit(function(e){
var url = "<?php echo $editFormAction; ?>";
var data = $(this).serialize();
$.ajax({
type: "POST",
url: url,
data: data,
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault();
});
$('.bars-reversed').click(function () {
var formNumber = $(this).attr('data-number');
$("#form-evalcoment" + formNumber).submit();
});
</script>
非常感谢您的时间和帮助。
答案 0 :(得分:0)
您目前正在尝试通过单击选择元素执行此操作。您应该尝试改变:
function submitForm(data) {
var url = "<?php echo $editFormAction; ?>";
$.ajax({
type: "POST",
url: url,
data: data,
success: function(data)
{
alert(data); // show response from the php script.
}
});
};
$('.bars-reversed').on('change', function () {
var formNumber = $(this).attr('data-number');
var data = $("#form-evalcoment" + formNumber).serialize();
submitForm(data);
});
单击元素后,单击会立即提交表单,而不是在进行更改时,即我假设您希望提交表单。