0

I'm trying to display the details of projects in database and and allow admins to them delete them.

Here's what I have so far:

<?php
    include 'dbc.php';
    $query=mysql_query("select * from pro1");
    while($result=mysql_fetch_array($query)){
        echo '<span>'.$result['name'].'</span>'.'<a href="#" id='.$result['pro_id'].' onclick="delet(this.id);">delet</a>'.'<br/>';
    }
?>       
<html>
    <head>
    </head>
    <body>
        <span>test10</span><a href="#" id=10 onclick="delet(this.id);">delet</a>    

        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function(){
            function delete(proid){
                //alert(proid);
                $.post("back.php",{
                    proid:proid
                },function(data){
                    alert(data);
                });
        });//ready func end
        </script>
    </body>
</html>

I'm getting the proid from db... now I need to send the id with ajax. The loop output will be something like this:

<span>test10</span>
 <a href="#" id=10 onclick="delete(this.id);">delete</a>
Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
Manjunath Hegde
  • 404
  • 4
  • 21

3 Answers3

0

Move the delet function out of the document.ready wrapper. It can't be accessed because it's out of scope:

            // no $(document).ready()
            function delet(proid){
                $.post("back.php",{
                    proid:proid
                },function(data){
                    alert(data);
                }); // <-- this was missing
            }
MrCode
  • 63,975
  • 10
  • 90
  • 112
0

Explanation

There is missing one closing bracket for the function.

Note: The function must not be wrapped to be executed on load, otherwise it won't be accessible !


Solution

(JSFiddle)

Javascript/jQuery

    function delet(proid){
        //alert(proid);
        $.post("back.php",{
            proid:proid
        },function(data){
            alert(data);
        });
    } /*This one here */
Community
  • 1
  • 1
Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
0

Perhaps you want like this

JQuery :-

 $(document).ready(function(){
    function delete(proid)
    {
       $.ajax({
           url: 'back.php',
           type: "get",
           data:'id='+ proid,
           success: function (response) {
              alert(data);
           }

        });      
    }
});

PHP:-

<?php
    include 'dbc.php';

    $id=$_GET['id'];
    $sql=mysql_query("DELETE FROM pro1 WHERE pro_id='$id'");

    if($sql)
       echo "Deleted Succesfully<br>";

    else
       echo "Problem to delete :(<br>";

    $query=mysql_query("select * from pro1");
    while($result=mysql_fetch_array($query)){
        echo '<span>'.$result['name'].'</span>'.'<a href="#" id='.$result['pro_id'].' onclick="delet(this.id);">delet</a>'.'<br/>';
    }
?> 
sharif2008
  • 2,716
  • 3
  • 20
  • 34
  • I may be wrong as i am not an expert in PHP or JQUERY. just let me know plz. – sharif2008 Jun 11 '13 at 19:33
  • This code is vulnerable to sql injection attacks. Please check out this question for advice on how to make your php code more secure: http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php – Allan Nienhuis Jun 11 '13 at 19:35
  • @sharif Also note that `mysql` is *deprecated* in latest PHP versions. You'll want to use `mysqli` or `PDO` statements for regular `MySQL` queries. – Jeff Noel Jun 14 '13 at 11:16