0

I have a function that sends a post request to a php website. I get 2 different behaviors by simply changing the capitalization of a variable. The variable in question is the 'action' variable and either being set to "deleteIndexMain" or "deleteIndexmain" If the action variable is set to "deleteIndexmain" I get the popup displaying the html that the php returns. If I set the variable to "deleteIndexMain" I get no popup. (meaning it appears to be a javascript issue?

Here is the java script code:

function deleteMe(v,r)
            {
                if(confirm("Are you sure"))
                {
                    var xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function() 
                    {
                        if(xhttp.readyState == 4 && xhttp.status == 200)
                        {
                            alert(xhttp.responseText);
                            document.getElementById("indexmaintable").deleteRow(r);
                        }
                    };
                    xhttp.open("POST", "includes/control.php", true);
                    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    xhttp.send("action=deleteIndexMain&file="+v);
                }

            }

and here is the php code:

<?php
    //Todo make sure  to authenticate!

    session_start();
    require_once("config.php");


    function deleteIndexMain($file)
    {
        unlink($file);
        $query = 'DELETE FROM indexmain WHERE linklocation="'.$file.'"';
        $db->query($query);
    }

    print_r($_POST);
    if(isset($_POST) && $_POST['action'] == "deleteIndexMain")
    {
        echo 'Deleting '.$_POST['file'];
        deleteIndexMain($_POST['file']);
    }



?>
Matthew
  • 3,886
  • 7
  • 47
  • 84
  • 1
    You have "deleteIndexMain" in both the JS and PHP. When you change the capitalization, are you changing it in both files? If not, I think that's your answer. – Jacob Brazeal May 14 '16 at 21:59
  • No alert box probably indicates a PHP error, because the AJAX request is not returning with 200. Can you check to see if you get a 500 de in your Network Timeline in Chrome/Safari? – Jacob Brazeal May 14 '16 at 22:02
  • 1
    Ids, variables and values such as `deleteIndexMain` in `$_POST['action'] == "deleteIndexMain"` are case-sensitive/unique; *end of story*. I.e.: `$dog` and `$Dog` are two different animals altogether, as are `#cat` and `#Cat`. This applies to most or all programming/coding languages. – Funk Forty Niner May 14 '16 at 22:03
  • possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner May 14 '16 at 22:04
  • Functions however are not case-sensitive, not in PHP anyway, but in JS they are. *"JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters. The while keyword, for example, must be typed "while", not "While" or "WHILE"."* – Funk Forty Niner May 14 '16 at 22:09
  • Turns out it was a PHP issue. The variable $db was not defined in the scope of the function. – Matthew May 15 '16 at 03:58

1 Answers1

1

String comparison with == is case-sensitive. If you want to perform a case-insensitive comparison, you can use strcasecmp():

if(isset($_POST) && strcasecmp($_POST['action'], "deleteIndexMain") == 0)

Notice that strcasecmp doesn't return a boolean, it returns a number that indicates whether the first string is less than, equal to, or greater than the second. So you have to use == 0 to test if the strings are equal.

Or you can convert everything to a single case with strtolower() before comparing normally.

if(isset($_POST) && strtolower($_POST['action']) == "deleteindexmain")
Barmar
  • 741,623
  • 53
  • 500
  • 612