0

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.

ageoff
  • 2,798
  • 2
  • 24
  • 39

1 Answers1

3

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

Community
  • 1
  • 1
Rob W
  • 9,134
  • 1
  • 30
  • 50
  • I understand that, and I have been looking through example code and everything. My problem is I am not sure how to pass a variable (like my id) when it is not a form that has been posted. If that makes sense? – ageoff Jul 01 '13 at 18:43
  • At that point in time, then, you would probably want to generate an ID or UUID and store it in a session or insert a blank row (a "reserved" row) that can be filled later. Make sense? – Rob W Jul 01 '13 at 18:44
  • I am still a bit confused, I edited my code with an attemt I made. – ageoff Jul 01 '13 at 19:43
  • Check FireBug's net console to see what's going on. :) – Rob W Jul 01 '13 at 19:46
  • That is a pretty cool tool. I think my problems have moved beyond this question now (now getting into syntax issues/errors), so I am going to move onto them. Thanks for you help! – ageoff Jul 01 '13 at 20:06