0

How can I pass id="'.$row["courseid"].'" into the ajax function,

I'm trying data:{'courseid':deleteId}, but is not working any ideas on how to fix this problem.

    <?php


echo "<table width='100%'>";
echo "<tr> 
      <th>Course name</th> 
      <th>Delete</th>
      <th>Edit</th>
      </tr>";
?>
    <?php foreach($rows as $row):  
    echo "<tr>"; 
    echo '<td><a href="#">' . htmlentities($row['coursename'], ENT_QUOTES, 'UTF-8') . '</a></td>';
    echo '<td><button onclick="deleteC(' . $row['courseid'] . ')");"><font color="#e70404"> Delete </font> </button></td>';
    echo '<td><a class="delete" id="'.$row["courseid"].'">Delette</a></td>';
    echo "</tr> ";
    endforeach; 
echo "</table>";

?>

And this is the ajax function which is in the same page

<script type="text/javascript">
function deleteC(deleteId){
   $.ajax({
   type: "GET",
   url: "deleteCourse.php",
   data:{'courseid':deleteId},
   success: function(result){               
       if(result=='correct'){
           window.location='index.php';
       }else {
       window.location='coursesData.php';
       }
   }
});
}
</script>

This is the deleteCourse.php

   <?php 
require("connect.php");
if (isset($_GET['courseid']) && is_numeric($_GET['courseid']))
{ 
    $id = $_GET['courseid'];

    echo"$courseid"; 
    $con=mysqli_connect("localhost","root","","independentstudyclass");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    mysqli_query($con,"DELETE FROM courses WHERE courseid=$id");
    echo "correct";
    mysqli_close($con);
    echo "correct";
}
else 
{
    header ("Location: ../index.php");
}


?> 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3424633
  • 51
  • 2
  • 6
  • `url` property should contain string `'deleteCourse.php'` in your `$.ajax` object. By the way can you tell us how you are passing `courseid` to the ajax handler? – Rahil Wazir Mar 30 '14 at 22:09
  • Thats my main problem I don't know how to pass it, in fact is .$row["courseid"] what I want to pass into ajax – user3424633 Mar 30 '14 at 22:13

1 Answers1

0

Instead of this:

echo '<td><a href="finalphp/deleteCourse.php?id=' . $row['courseid'] . '" onclick="deleteC()");"><font color="#e70404"> Delete </font> </a></td>';

Just pass the id to the JS function as a parameter and use it:

PHP:

echo '<td><a href="finalphp/deleteCourse.php?id=' . $row['courseid'] . '" onclick="deleteC(' . $row['courseid'] . ')");"><font color="#e70404"> Delete </font> </a></td>';

JS

function deleteC(deleteId){
   $.ajax({
   type: "GET",
   url: "deleteCourse.php",
   data:{'courseid':deleteId},
   success: function(result){               
       if(result=='correct'){
           window.location='index.php';
       } else {

       }
   }
});
}
nxu
  • 2,202
  • 1
  • 22
  • 34
  • Thank you for this, now my function is working but it seems that is not reading detecting . $row['courseid'] is there a way to pass this piece of code into ajax – user3424633 Mar 30 '14 at 22:19
  • thank you it was a mistake in my deleteCourse file, now is working. – user3424633 Mar 30 '14 at 22:34
  • I edit my post with the version of code that Im using it seems that is working because it deletes the selected couseId, but how can see refresh only the table without refreshing the entire page. – user3424633 Mar 30 '14 at 22:51
  • Since you use the onclick=.. tag instead of using event listeners (you may consider using them (http://stackoverflow.com/questions/6445207/adding-event-listeners-on-elements-javascript - in this case, event.preventDefault() would be the best way), I'd suggest to remove the href="..." element from the tag as you only want to trigger the JS code and not to navigate to the site. – nxu Mar 30 '14 at 22:56
  • Thank i will look at that. in your opinion did you think is a good idea to refresh a page every 5 second so if a change is made to database the user will available to see it. setInterval(function(){ $('#cousesDatata').load('finalphp/coursesData.php'); },5000); i was thinking on using this line of code to fix the issue of deleting someting. I hope you understand what i saying i learning english too – user3424633 Mar 30 '14 at 23:04