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!
mysql_real_escape_string
to prevent SQL injection, or better, use parametrized statements ofmysqli
orPDO
.