gpt4 book ai didi

php - 使用jquery无法获取php循环中的所有id

转载 作者:行者123 更新时间:2023-11-29 07:54:06 24 4
gpt4 key购买 nike

我从 html 表行中的 mysql 数据库获取所有关键字。因此,当我单击每个关键字时,它会使用 jquery 调用 keywordBoxid 来使用 php 将 kid 添加到 mysql 数据库。我使用 while 循环来获取所有 kid,但我只获取一个 kid。为什么 ?当我点击不同的关键字

时,应该得到不同的 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();

...

关于php - 使用jquery无法获取php循环中的所有id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25605297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com