当我尝试使用ajax点击更新数据到我的数据库而不刷新页面时,我的ajax代码出现问题。我试图让用户单击从数据库中获取的每个数据中的按钮。但问题是每次我点击每个数据时它只占用第一行数据。你能帮我看看我的代码有什么问题吗? 这里是Ajax
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(function () {
$('.vote').on('submit', function (e) {
var vote = $(this).attr("data-id");
e.preventDefault();
$.ajax({
type: 'post',
url: 'file-includes/social-interaction/vote_action.php',
data: {vote : vote},
success: function (data) {
alertify.log(data, "", 0);
return false;
}
});
});
});
</script>
Php Here
<?php
session_start();
error_reporting(0);
require '../../core/database/connect.php';
require '../../core/functions/general.php';
require '../../core/functions/users.php';
//mysql_query("SET NAMES utf8");
$current_file = explode('/', $_SERVER['SCRIPT_NAME']);
$current_file = end($current_file);
if (logged_in() == true) {
$session_user_id = $_SESSION['user_id'];
$user_data = user_data($session_user_id, 'user_id', 'username', 'password', 'first_name', 'last_name', 'email', 'password_recover','type','allow_email', 'profile', 'quote','dob','status','current_occupation','location','skill','about_user','joined_date','phone','ban','fbpname','fbpurl','fb','fbn','website','whatsapp','profile_view');
$user_id = $user_data['user_id'];
if (user_active($user_data['username']) == false) {
session_destroy();
header('Location: index');
exit();
}
if ($current_file !== 'change_password.php' && $current_file !=='logout.php' && $user_data['password_recover'] == 1) {
header('Location: change_password?force');
exit();
}
}
$errors = array();
?>
<?php
protect_page();
if($_POST){
$question_id = $_POST['question_id'];
$answer_id = $_POST['answer_id'];
$rate = $_POST['score_input'];
$user_id = $user_data['user_id'];
$sql = "SELECT * FROM vote_action WHERE answer_id = ".$answer_id." AND user_id=".$user_id."";
$result = mysql_query($sql);
if(mysql_num_rows($result)!=0)
{
echo 'You\'ve already voted for this answer';
//mysql_query("DELETE FROM `vote_action` WHERE `answer_id` = '$answer_id' AND `user_id` = '$user_id'");
}else{
mysql_query("INSERT INTO `vote_action` (`vote_id`, `question_id`, `user_id`, `rate_score`, `answer_id`) VALUES (NULL, '$question_id', '$user_id', '$rate', '$answer_id');");
echo 'You\'ve voted for this answer the answer and give the user '.$rate.' Score(s)';
}
}
?>
html文件在这里
<form class="vote" data-id="<?php echo 'vote-'.$answer_id?>">
<input type="hidden" data-id="<?php echo 'vote-'.$answer_id?>" value="<?php echo $question_id_get;?>" name="question_id_asnwer" class="vote">
<input type="hidden" data-id="<?php echo 'vote-'.$answer_id?>" value="<?php echo $answer_id;?>" name="answer_id" class="vote">
<!-- Small button group -->
<select id="first-disabled" name="score_input" data-id="<?php echo 'vote-'.$answer_id?>" class="selectpicker vote" data-hide-disabled="true" data-live-search="true" data-width="55%">
<optgroup disabled="disabled" label="disabled">
<option>Hidden</option>
</optgroup>
<optgroup label="SCORE">
<option data-icon="glyphicon-heart">1</option>
<option data-icon="glyphicon-heart">2</option>
<option data-icon="glyphicon-heart">3</option>
<option data-icon="glyphicon-heart">4</option>
<option data-icon="glyphicon-heart">5</option>
</optgroup>
</select>
<button type="submit" name="vote" class="btn-text" data-vote="<?php echo $answer_id; ?>"><i class="glyphicon glyphicon-menu-up"></i></button> <span>25 </span>
</form>
答案 0 :(得分:0)
这是怎么回事。我刚刚发现它。如果有人遇到同样的问题,你可以看到这个。谢谢... 这是我新的ajax代码
<script type="text/javascript">
$(function () {
$('.form').on('submit', function (e) {
e.preventDefault();
var form_data = $(this).closest(".form").serialize();
//alert(form_data); //I used this to see how my ajax get data from form but I don't want it to be show to user so I commented it out
$.ajax({
type: 'GET',
url: 'file-includes/social-interaction/vote_action.php',
data: form_data,
success: function (data) {
console.log(data)
alertify.log(data, "", 0);
}
});
});
//event.preventDefault();
});
</script>
这是我发布数据的新工作资料
<?php
protect_page();
if($_GET){
$question_id = $_GET['question_id'];
$answer_id = $_GET['answer_id'];
$rate = $_GET['score_input'];
$user_id = $user_data['user_id'];
$sql = "SELECT * FROM vote_action WHERE answer_id = '".$answer_id."' AND user_id='".$user_id."'";
$result = mysql_query($sql);
if(mysql_num_rows($result)!=0)
{
echo 'You\'ve already voted for this answer';
//mysql_query("DELETE FROM `vote_action` WHERE `answer_id` = '$answer_id' AND `user_id` = '$user_id'");
}else{
mysql_query("INSERT INTO `vote_action` (`vote_id`, `question_id`, `answer_id`, `user_id`, `rate_score`) VALUES (NULL, '$question_id', '$answer_id', '$user_id', '$rate')");
echo 'You\'ve voted for this answer the answer and give the user '.$rate.' Score(s)';
}
}
?>
最后这是我的工作形式:
<form action="" class="form">
<input type="hidden" name="question_id" value="<?php echo $question_id;?>" />
<input type="hidden" name="answer_id" value="<?php echo $answer_id;?>" />
<select id="first-disabled" name="score_input" class="selectpicker" data-hide-disabled="true" data-live-search="true" data-width="width:10%;">
<optgroup disabled="disabled" label="disabled">
<option>Give Score</option>
</optgroup>
<optgroup label="Useless Answer">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
</optgroup>
<optgroup label="Good Answer">
<option>4</option>
<option>5</option>
<option>6</option>
</optgroup>
<optgroup label="Helpful Answer">
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</optgroup>
</select>
<input class="submit" type="submit" name="submit" value="submit" class="glyphicon glyphicon-ok icon-color-blue">
</form>