hi all,

first i want to say I am new to php.

I'm developing a blog where administrator can get all comments replied by the users.
I can view it in a html table.

But now what I need is dynamically delete , edit them.
i tried this

include '../Classes/DBhandling.php';
  include '../Classes/config.php';
 
  $dob = new Dbhandling($dbHost,$dbUser,$dbName,$dbPass);
  $dob->connectDB();


         $qry="select * from usercomments";
         $result=mysql_query($qry);
             echo '<table  border=1 align="center">'.'<thead>'.'<tr>'.'<th>'."PO_ID".'</th>';
             echo '<th>'."C_ID".'</th>'.'<th>'."USER_NAME".'</th>'.'<td width=300; align="center">'."COMMENT".'</th>';
             echo '<th>'."DATE".'</th>';
             echo '<th>'."ALLOW".'</th>';
             echo '<th>'.'DEL'.'</th>'.'</tr>';
             echo '</thead>';
         while($row=mysql_fetch_array($result)){
            echo '<tbody>';
            echo '<tr>'.'<td align="center" >'.$row['postId'].'</td>';
            echo '<td align="center" >'.$row['commentId'].'</td>';
            echo '<td align="center" >'.$row['name_cm'].'</td>';
            echo '<td align="center" >'.$row['comment_cm'].'</td>';
            echo '<td align="center" >'.$row['dateComment_cm'].'</td>';
            echo '<td align="center" >'.$row['allow_cm'].'</td>';
            $cmId=$row['commentId'];
            echo '<form action="delete.php?cmId='.$cmId.'">';
       
            echo '<td align="center" >'.'<input type="submit" value="Del" name="" />'.'</td>'.'<tr>';
           //  echo "ID".$row['id']."<$> ".$row['title']." ".$row['popst'];
            echo ' </form>';
        //echo '<form action="delete.php?cmId='.$cmId.'">';

//echo "printr".print_r($row);
         }
          echo '</tbody>'.'</table>';
         $dob->closeDB();
        // put your code here

        ?>

but i coudn't get the comment id to delete.
if any how I can get the comment ID then I can manage to delete the comment.
therefore, I think the proplem arise in

echo '<form action="delete.php?cmId='.$cmId.'">';
       
            echo '<td align="center" >'.'<input type="submit" value="Del" name="" />'.'</td>'.'<tr>';
           //  echo "ID".$row['id']."<$> ".$row['title']." ".$row['popst'];
            echo ' </form>';

Can anybody help me to do this . Please
give me a little hint or example code.

I use php, mysql
I think u can understand me.

thx in advance,
menuka.

Are you able to display it

echo '<td align="center" >'.$row['commentId'].'</td>';

If no then check the spelling and case, php array is case sensitive.

Another thing is that you have placed form in the loop, I think it is not required, you can simply use <a></a>\

replace your delete code

echo '<form action="delete.php?cmId='.$cmId.'">';
       
            echo '<td align="center" >'.'<input type="submit" value="Del" name="" />'.'</td>'.'<tr>';
           //  echo "ID".$row['id']."<$> ".$row['title']." ".$row['popst'];
            echo ' </form>';

by following code

$cmId=$row['commentId'];
echo "<td align='center' ><a href='delete.php?cmId=".$cmId."' >Delete</a></td></tr>;";

at line 17 bring tbody tag out of loop

hi,
$cmId=$row;
this works i get properly values for the $cmId;

and correct the <tbody> out of the loop but it doesnt make any difference.
when I press the button I did n't get the id that I want to passe to delete.php.
if i can do it then I am done!!!

have you placed <a></a> as I suggested, what happens when you click on that link?
Does it shows proper url with commentid in broswer statusbar ?

hi I change the form in to <a></a> and now it works. i was trying the form.
Now I can manage this. thx lot. but do u know the reason to not to work form?

I am not sure, because I do not know what do you mean when you say, "It does not works".
I think reason could be multiple forms.

I am not sure, because I do not know what do you mean when you say, "It does not works".
I think reason could be multiple forms.

It doesn't work because you use multiple form, and you don't send the value of each data to file delete.php.

Change code in your delete form:

echo "<td align='center'><input type='button' value='Del' id='del".$row['postId']."' onclick='delcomment(".$row['postId'].")' /></td><tr>";

on top your page between head element you create javascript code below:

<script type="text/javascript">
function delcomment(id){
location.href='delete.php?cmId='+id;
}
</script>

Data will capture by the JavaScript by trigger button on each data, and send it to file delete.php with variable cmId.

hi now I want to passe two variables not just one but didn't work and try various way.
what i tried was dynamically delete data here is the javascript of my code,

<script type="text/javascript">

function delcomment(str,str2)
{
if (str=="" && str2=="")
  {
  document.getElementById("contentbox").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("contentbox").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","delete.php?q="+str+"&q2="+str2,true);
xmlhttp.send();
}
</script>

there I try to passe two variables q and q2 to the .

I design the program to get commentID and postId then relven comment delete from the data base. after that print the rest of comments. try to do this with ajax.

here is the the coding in contentbox id

<div id="contentbox">
          <?php     
              include '../../Classes/DBhandling.php';
              include '../../Classes/config.php';
 
  $dob = new DBhandling(dbHost,dbUser,dbName,dbPass);
  $dob->connectDB();


 $qry="select * from usercomments";
         $result=mysql_query($qry);
             echo '<table  border=1 align="center">'.'<thead>'.'<tr>'.'<th>'."PO_ID".'</th>';
             echo '<th>'."C_ID".'</th>'.'<th>'."USER_NAME".'</th>'.'<td width=300; align="center">'."COMMENT".'</th>';
             echo '<th>'."DATE".'</th>';
             echo '<th>'."ALLOW".'</th>';
             echo '<th>'.'DEL'.'</th>'.'</tr>';
             echo '</thead>';
         while($row=mysql_fetch_array($result)){
            echo '<tbody>';
            echo '<tr>'.'<td align="center" >'.$row['postId'].'</td>';
            echo '<td align="center" >'.$row['commentId'].'</td>';
            echo '<td align="center" >'.$row['name_cm'].'</td>';
            echo '<td align="center" >'.$row['comment_cm'].'</td>';
            echo '<td align="center" >'.$row['dateComment_cm'].'</td>';
            echo '<td align="center" >'.$row['allow_cm'].'</td>';
            $cmId=$row['commentId'];
            $poId=$row['postId'];
            echo "<td align='center'><input type='button' value='Del' id='del".$row['postId'].$row['commentId']."' onclick='delcomment(".'$cmId'.','.'$poId'.")'>"."</td>";
            
            echo '</tr>';
        //echo '<form action="delete.php?cmId='.$cmId.'">';

//echo "printr".print_r($row);
           
            
            
            
         }
          echo '</tbody>'.'</table>';
         $dob->closeDB();
        // put your code here

        ?>
               
               
               
           </div>

can u help me to passe two variable to the javascript and from javascript to the delet.php file.???

You mean what is not working.
1) you are not able to delete record now after adding post id in delete.php
2) or you are able to delete record but you are not able to update contentbox
3) or both things not working

both things not working. i try to print the commentId and post id in the delete.php
but I did n't get any value. I think data is not passing.

If I passe one variable then this work propely.
Can I use 2 function to onclick to passe the 2 variable?

echo "<td align='center'><input type='button' value='Del' id='del".$row['postId']."' onclick='delcomment(".$row['postId'].")' /></td><tr>";

another problem is why do we set id='del".$row.
is it necessary to set this ? if yes then can u explain the reason?

Trying to change line 28:

echo "<td align='center'><input type='button' value='Del' id='del".$row['postId'].$row['commentId']."' onclick='delcomment(".$cmId.",".$poId.")'>"."</td>";

Is it the $cmId and $poId all in numeric? If yes, your code will work. You can passes 2 variable or more. It didn't work because of this

onclick='delcomment(".'$cmId'.','.'$poId'.")'

it should be

onclick='delcomment(".$cmId.",".$poId.")'

$cmId and $poId must separeted by concatenation (.) from the string. But you use (,) coma like a variable, you must put coma (,) between the variable and nested it with double quote ("). Hope you understand what i mean. Good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.