0

I'm quite new to PHP and Javascript. I am trying to get a variable from a google maps API marker with an option to delete the marker and its information from a MySQL database. No errors are being generated, however the row is not being deleted. I suppose that the problem is with the POST. Below is the code I have related to this matter:

var html = "<b>" + name + "</b> <br/>" + location + "<br/> <br/> <input type='button' value='Get Directions from your Current Position' onclick=getDirections()/> <br> <input type='button' name = 'remove' value='Remove Pointer' onclick=removePointer("+name+")/>";

That is the line where I am calling the removePointer function, passing 'name' as a parameter

function removePointer(name){
    var nameSend = name;
    $.post("index.php", {variableName: nameSend});
    <?php
        $mysql_host = "xxxx";
        $mysql_database = "xxxx";
        $mysql_user = "xxxx";
        $mysql_password = "xxxx";

        $link = mysql_connect($mysql_host, $mysql_user, $mysql_password);
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($mysql_database) or die(mysql_error());

        $removeName = $_POST['variableName'];
        mysql_query(("DELETE FROM markers WHERE Name='" . $removeName . "'"),$link) or die ("Markers Table Error: " . mysql_error());
    ?>
}

That is the removePointer function, where it should get the javascript variable, post it, and a PHP block to get the post and remove the MySQL row accordingly.

Thanks in advance for any help!

2
  • 1
    You are intermixing your PHP in your Javascript. These are two different universes running on two different machines: client + server. You're better off looking for other questions and examples of this type of pattern. Commented Jan 22, 2013 at 16:17
  • And you should use mysql_real_escape_string to prevent SQL injection, or better, use parametrized statements of mysqli or PDO. Commented Jan 22, 2013 at 16:30

2 Answers 2

0

In your case, Javascript is something that will run in the user's browser while PHP is something that will run on the server (which will need to be able to talk to the database server). That is, you cannot embed PHP inside of a Javascript function and expect the PHP to be run - web browsers do not execute PHP. Further, you NEVER want to put any sensitive information into Javascript that will be running in a browser (such as MySQL credentials), because it will be visible to anyone who loads that Javascript.

You will need to create a server-side PHP script that Javascript will communicate with. Javascript could make an AJAX request to the PHP script, POSTing the data you wish the PHP to take action on. In this case, the removePointer() function could post the marker name to the PHP script, which would then remove it from the database.

To make life easier, you might consider using a Javascript library such as jQuery, which can greatly simplify making Ajax requests.

2
  • he is already trying to make an ajax post request, and he is using some library (probably jQuery) Commented Jan 22, 2013 at 16:19
  • Thank you for your suggestions and tip about sensitive information! I will look deeper into it. Thanks again! Commented Jan 22, 2013 at 16:26
0

you cannot post just to your index.php page. create a new one "del.php", put your php code there, and post to that page:

index.php

function removePointer(name){
  var nameSend = name;
  $.post("del.php", {variableName: nameSend}); 
}

del.php

<?php
    $mysql_host = "xxxx";
    $mysql_database = "xxxx";
    $mysql_user = "xxxx";
    $mysql_password = "xxxx";

    $link = mysql_connect($mysql_host, $mysql_user, $mysql_password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db($mysql_database) or die(mysql_error());

    $removeName = $_POST['variableName'];
    mysql_query(("DELETE FROM markers WHERE Name='" . $removeName . "'"),$link) or die ("Markers Table Error: " . mysql_error());
?>

of course im no php-expert, and that is for sure not the optimal way to do ajax requests, but i hope you understand the concept of ajax a little bit better know...

2
  • I think I understand what you are saying. Thank you! Commented Jan 22, 2013 at 16:26
  • This answer has the same SQL injection hole as the question. See my comment there. Commented Jan 22, 2013 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.