ageoff
ageoff

Reputation: 2828

Update database after user action

Ok, so I am relatively new to php, javascript, and jquery. Here is what I have going.

I have a table that is created when the page opens that is filled up using a for from a database query (just a simple select *). The problem right now is the final <td> is a dropdown menu that the use can change (changing the item from viewed to unviewed and back). So, what i want to do is when the user selects either value in the drop down menu, i want to get the second column value (the ID value in my database) of that row and update the viewed status. My problem is I am not sure how to use JQuery to get the ID value into my update statement for the database. Here is what I have so far:

<script language="Javascript">
    //cache the select and span elements
    var table = document.getElementById('leads');
    var mySelect = document.getElementById("mySelect");

    //when it changes
    mySelect.onchange = function() {
        var row = this.parentNode.parentNode;
        //change the tag innerHTML checking the selected value of the select
        if (mySelect == "1") {
            var id = table.rows[row.rowIndex].cells[1].innerHTML;
            //HOW DO I GET THIS ID INTO A SELECT STATEMENT THAT CAN EXECUTED BY A PHP FUNCTION
        }
    }
</script>

EDIT: Here is an attempt so far... Inside my if statement I added the following line:

$.get("updateToNew.php", {lid: id});

and here is the php (you can assume the connection to the database and everything is correct inside this file):

$query = mysql_query("UPDATE myTable SET new=1 WHERE ID=".$_GET['lid']) or die(mysql_error());

This does not throw any errors, but does not update the database. Also, I noticed as I was troubleshooting that the script only runs the first time the drop down menu is changed, any time after that it does nothing.

Upvotes: 0

Views: 105

Answers (1)

Rob W
Rob W

Reputation: 9142

You would have to write an XmlHttpRequest to fulfill this action.

See what suits you best: What is the difference between XMLHttpRequest, jQuery.ajax, jQuery.post, jQuery.get

Upvotes: 3

Related Questions