0

I don't know why, but the variable $idCount does not increment, it just remains at 1001. I would like to loop through and create nine hyperlinks which all delete a specific record from the database.

<script>


    function passValue() {

        var js_var = document.getElementById("link").getAttribute("value"); 
        alert(js_var);

        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                //document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","data.php?q="+js_var,true);
        xmlhttp.send();

    }

</script>   


<?php   



    $servername = "127.0.0.1";
    $username = "root";
    $password = "";
    $dbname = "test";

    $conn = new mysqli($servername, $username, $password, $dbname);


    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT * FROM customers";

    $rs = mysqli_query($conn, $sql) 
    or die(mysqli_error($conn));


    $idCount = 1001;

    while($rc = mysqli_fetch_assoc($rs)) {

     echo $idCount . '<tr><td><a id="link" href="javascript:passValue()" value="'.$idCount.'")>Delete Record</a></td><td>' . $rc["custID"] . '</td><td>' . $rc["custName"] . '</td><td>' . $rc["custPassword"] . '</td><td>' . $rc["custGender"] . '</tr>';
     $idCount++;

    };      

?>
hohoho236
  • 19
  • 2
  • 1
    fyi, `id`s need to be unique – brombeer Feb 19 '18 at 14:13
  • There is no way this is reproducible – GrumpyCrouton Feb 19 '18 at 14:13
  • Your question doesn't really make sense. Why don't you increment before echo? – Rotimi Feb 19 '18 at 14:13
  • [ID's Must Be Unique](http://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-eleme), specifically because it will cause problems in [JavaScript](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) and CSS when you try to interact with those elements. – Jay Blanchard Feb 19 '18 at 14:13
  • 2
    If you look at the HTML source code for multiple rows you will see that the `idCount` value has increased. So your assumption that it doesn't is wrong. Perhaps now the other comments make more sense? – KIKO Software Feb 19 '18 at 14:17
  • Thank you for the help. It seems the issue is that javascript only gets the first id of the a href link, thus the 'js_var' remains 1001. I am still think if there is any way to pass each value in different links to javascript. – hohoho236 Feb 19 '18 at 15:44

0 Answers0