Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Aim

I am trying to create a page to delete records from my database.The page would consists of a that populates itself upon page load.Upon selecting a value from the and clicking the submit button, a php page would be called and the results of the php would be loaded into a table below the .I can then click on the delete button that would be beside the values echoed to delete that value from the database

My Form:

<!DOCTYPE HTML>
<html>
<head>
<!--Loads JQuery script-->
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<!--Gets list of item categories on page load-->
<script type="text/javascript">
$(document).ready(function(){
    $("#viewsubcat").load("getcategory.php");
});
</script>
<script type="text/javascript">
$("#viewsubcatsubmit").click(function(){
    var cat=$('#viewsubcat').val();
    $('#deletetable').load('delsubcategory.php?cat='+cat);
});
</script>
</head>
<body>

<form style="width:500px" id="viewsubcategory" name="viewsubcategory" method="post" action="<? php echo $_SERVER['PHP_SELF'] ?>" >
    <div class="inputfield">
        <label for="viewsubcat">Select Category:</label>
        <select style="margin-left:37px" id="viewsubcat" name="viewsubcat"></select>
    </div><br />

    <div class="inputfield">
        <input style="margin-left:250px" type="button" id="viewsubcatsubmit" name="viewsubcatsubmit" value="Search" /></div>
    </div><br />
</form>

<table id="deletetable">
</table>

</body>
</html>

PHP Page:

<?php
    include("cxn.inc");
    $id=$_SESSION['BizID'];
    $cat=$_GET['cat'];
    $viewsubcat=$cxn->prepare("SELECT * FROM `itemcat`,`itemsubcat` 
    WHERE `itemcat`.`CatID`=:cat AND `itemsubcat`.`ItemCat`=:cat AND `itemsubcat`.`BusinessID`=:id");
    $viewsubcat->bindValue(":cat",$cat);
    $viewsubcat->bindValue(":id",$id);
    $viewsubcat->execute();
    //echo"<table border='1'>";
    echo"<tr>";
            echo"<td>";
                echo"Categories";
            echo"</td>";
            echo"<td>";
                echo"SubCategories";
            echo"</td>";
            echo"<td>";
                echo"Action";
            echo"</td>";
        echo"</tr>";
    while($getsubcat=$viewsubcat->fetch(PDO::FETCH_ASSOC))
    {
        $cat=$getsubcat['ItemCat'];
        $subcat=$getsubcat['ItemSubCat'];
        $subcatid=$getsubcat['SubCatID'];
        echo"<tr>";
            echo"<td>";
                echo"$cat";
            echo"</td>";
            echo"<td>";
                echo"$subcat";
            echo"</td>";
            echo"<td>";
                echo"<form id='delsubcategory' name='delsubcategory' method='POST' action='delsubcategory.php'>";
                    echo"<input type='hidden' id='delsubcatid' name='delsubcatid' value='$subcatid' />";
                    echo"<input type='submit' id='delsubcatsubmit' name='delsubcatsubmit' value='Delete' />";
                echo"</form>";
            echo"</td>";
        echo"</tr>";
    }
    //echo"</table>";
?>

Problem:

The HTML Table isn't loading upon pressing the button.The Php page is working when i change the form's action to "delsubcategory.php" and the button type="submit", so the issue lies with the html.

Would appreciate any insights into the matter

share|improve this question
 
Do you get an error? –  Rab Nawaz Aug 26 at 4:59
 
@RabNawaz Dipesh got it. I forgot to wrap my .click in $(function()); –  Ken Aug 26 at 5:13
add comment

1 Answer

up vote 1 down vote accepted

You forget to wrap your button click event handler into $(function(){ });

<script type="text/javascript">
$(function(){
$("#viewsubcatsubmit").click(function(){
    var cat=$('#viewsubcat').val();
    $('#deletetable').load('delsubcategory.php?cat='+cat);
});
});
</script>
share|improve this answer
 
Arghh can't believe i missed that. Amazing how the simplest of mistakes can cause hours of hair pulling –  Ken Aug 26 at 5:01
 
@Ken it happens sometime...but that teach you not to repeat it again....hope problem is solved... –  Dipesh Parmar Aug 26 at 5:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.