Shibbir
Shibbir

Reputation: 257

Can't get all id in php which loop using jquery

I'm getting all keywords from mysql database in html table row. So When I click on each keyword it's calling keywordBoxid using jquery to add kid to mysql database using php. I used while loop to get all kid but I just get only one kid. why ? It's should be get different kid when i click on different keyword

Following is my code:

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 Code:

<?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>";  
    }


}
?>

Upvotes: 0

Views: 412

Answers (2)

Steve
Steve

Reputation: 20469

You have multiple duplicate ids in your html, which is not valid, as ids must be unique. Simply change to classes, and alter your js accordingly:

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();         

    ...

Upvotes: 2

Jenz
Jenz

Reputation: 8369

As you are replacing value of $kid to

echo "<input type='hidden' id='kid' value='$kid'/>";  

each time, it will contain only the last looped row's value.

Instead of calling the Jquery function on clicking the div, call that function on clicking each tr's.

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++;  
    }

and change the Jquery function

$('#keywordBox').click(function(e) {
----
}

to

function keywordclick(kid,cid)
{
$.ajax({
        type:"post",
        url:"addKeywordProcess.php",
        data:{
            'kid' : kid,
            'cdid' : cid
        },
        success:function(res){
            $('#result').html(res);         
            }
        }); 
}

Upvotes: 0

Related Questions