我正在html表行中从mysql数据库中获取所有关键字。因此,当我点击每个关键字时,它使用jquery调用keywordBox
id,使用php将kid
添加到mysql数据库中。我使用while循环来获得所有的kid
,但我只得到一个kid
。为什么?当我点击不同的keyword
时,应该会得到不同的kid
以下是我的代码:
echo "<div id='keywordBox'>";
echo '<table border="0" cellpadding="0" cellspacing="0" width="1055">';
$count = 0;
while($result = mysql_fetch_array($query)){
if($count%6==0 && $count!=0)
echo '</tr><tr>';
elseif($count==0) echo '<tr>';
$kid = $result['kid'];
$keywordName = ucfirst($result['keywordName']);
$keyword_short_name = ucfirst($result['keyword_short_name']);
$keyword_full_name = ucfirst($result['keyword_full_name']);
echo "<input type='hidden' id='kid' value='$kid'/>";
echo "<input type='hidden' id='cdid' value='$cdid'/>";
echo "<td width='400'><strong>$keyword_full_name</strong><strong>($keyword_short_name)</strong><br/>$keywordName</td>";
$count++;
}
echo '</table>';
echo "</div";
<script>
$('#keywordBox').click(function(e) {
var kid = $('#kid').val();
var cdid = $('#cdid').val();
e.preventDefault();
$.ajax({
type:"post",
url:"addKeywordProcess.php",
data:{
'kid' : kid,
'cdid' : cdid
},
success:function(res){
$('#result').html(res);
}
});
});
</script>
addKeywordProcess.php代码:
<?php
ob_start();
@session_start();
require_once("../config.php");
if(!isset($_SESSION['front_username']) && isset($_SESSION['front_username']) == "" &&
!isset($_SESSION['front_password']) && isset($_SESSION['front_password']) == "" &&
!isset($_SESSION['user_id']) && isset($_SESSION['user_id']) == "") {
header("Location:login.php");
exit();
}
$cdid = (int) $_POST['cdid']; // Get cdid from keyword page.
$kid = (int) $_POST['kid']; // Get cdid from keyword page.
$check = mysql_query("SELECT cdid, kid FROM userkeywords WHERE kid = '$kid' AND cdid = '$cdid' ");
$num = mysql_num_rows($check);
if($num == 1){
echo "<div class='error'>Keyword already exits to your contact list. cdid = $cdid and kid = $kid </div>";
}else{
$query = mysql_query("INSERT INTO userkeywords (ukid, cdid, kid) VALUES ('', '$cdid', '$kid') ");
if($query){
echo "<div class='success'>Successfully added a new keyowrd to your contact list. </div>";
}else{
echo "<div class='error'>I can't added a new keyword. </div>";
}
}
?>
您的html中有多个重复的id,这是无效的,因为id必须是唯一的。只需更改为类,并相应地更改js:
echo "<input type='hidden' class='kid' value='$kid'/>";
echo "<input type='hidden' class='cdid' value='$cdid'/>";
JS:
<script>
$('#keywordBox tr').click(function(e) {
var kid = $(this).find('.kid').val();
var cdid = $(this).find('.cdid').val();
...
当您将$kid
的值替换为时
echo "<input type='hidden' id='kid' value='$kid'/>";
每次,它都只包含最后一个循环行的值。
与其在单击div时调用Jquery函数,不如在单击每个tr时调用该函数。
while($result = mysql_fetch_array($query)){
if($count%6==0 && $count!=0)
echo '</tr><tr>';
elseif($count==0) echo '<tr>';
$kid = $result['kid'];
$keywordName = ucfirst($result['keywordName']);
$keyword_short_name = ucfirst($result['keyword_short_name']);
$keyword_full_name = ucfirst($result['keyword_full_name']);
echo "<input type='hidden' id='kid' value='$kid'/>";
echo "<input type='hidden' id='cdid' value='$cdid'/>";
echo "<td width='400' onclick='keywordclick($kid,$cdid)'><strong>$keyword_full_name</strong><strong>($keyword_short_name)</strong><br/>$keywordName</td>";
$count++;
}
并更改Jquery函数
$('#keywordBox').click(function(e) {
----
}
至
function keywordclick(kid,cid)
{
$.ajax({
type:"post",
url:"addKeywordProcess.php",
data:{
'kid' : kid,
'cdid' : cid
},
success:function(res){
$('#result').html(res);
}
});
}