0

Using google maps, I have events saving to a database using mysqli. These events are then displayed as markers on the map and when clicked the relevant data is displayed in an info box (Name, date, etc). I want the option to delete an event event by deleting a row from the DB when the Remove (remove-event) button is clicked. The button is contained in the data displayed with the javascript:

var eventContent = $('<div class="event-info">' + '<h4 class="event-name">' +     point.name + '</h4><hr>' +
        '<span><h5>Date: </h5>' +
        '<p class="event-date">' + point.edate + '</p></span>' +
        '<p class="event-description">'+point.description+'</p>' +
        '</span><button id="remove-event" name="remove-event" class="remove-event btn btn-danger btn-sm" onclick="tidy_maps.delete()" title="Remove Event">Remove Event</button>'+
        '</div>');

    // Display Event details on marker click
    google.maps.event.addListener(event_markers[i], "click", function () {
        infowindow.setContent(eventContent[0]);
        infowindow.open(map, event_markers[i]);

The script that sends it to the php (removedata.php):

tidy_maps.delete = function() {
    $.ajax({
        type:'POST',
        url:'removedata.php',
        success:function(data) {
            if(data) {
                alert("Are you sure?");
            }
            else {
                alert("ERROR!!!!");
            }
        }
    });
}

The removedata.php is:

$con = mysqli_connect("localhost", "root", "password", "gmaps1");
if (!$con) {
die("Can not connect: " .mysql_error());
}

$sql = "DELETE FROM events WHERE id = 'id' ";

$query = mysqli_query($con, $sql);

if(mysqli_affected_rows($con)) {
    echo "Record deleted successfully";
}

mysqli_close($con);

As it is, it does not delete the row in the DB, but when i change the line:

$sql = "DELETE FROM events WHERE id = 'id' ";

to a specific ID No. Example:

$sql = "DELETE FROM events WHERE id = '5' ";

And i run the removedata.php in the browser, it deletes the row with ID=5 from the DB. There seems to be no errors when the console when clicking the remove button so it must be sending to PHP script ok.

I would like when the Remove button is clicked that it asks are you sure and then it deletes that specific Row form the DB.

6
  • You forgot the $ sign in the id. "DELETE FROM events WHERE id = $id "; Commented Mar 24, 2015 at 22:41
  • Assign the row id to a variable ... $id = 5; $sql = "DELETE FROM events WHERE id = '$id' "; Commented Mar 24, 2015 at 22:41
  • @Brian no need for quotes around $id. Commented Mar 24, 2015 at 22:47
  • Where do you pass the id? success:function() is called if the request was successful - you need to show a confirm-box before you make the request. Commented Mar 24, 2015 at 22:51
  • I don't see any ID or Data passing from client to server? So how can it be delete? Commented Mar 25, 2015 at 14:03

4 Answers 4

1

As far as I can tell you don't pass the ID of the row to be deleted. You can send data two ways, either as a url parameter, or post it using the data tag:

$.ajax({
    type:'POST',
    url:'removedata.php',
    data: {id : 5}
});

Access the ID in removedata.php:

$id = intval($_POST["id"]);
$sql = "DELETE FROM events WHERE id = " . $id;
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried accessing ID in removedata.php but it just gives an error of: "Notice: Undefined index: id in/......./ on line 12"
Please show the code that you have tried. You have to make the request, if you go to removedata.php manually the ID will not be sent.
0

WHERE id = 'id' you need to remove the '' and add the $ symbol if you want id to be a variable.

2 Comments

This doesnt quiet solve it. It still remains in the DB. Should I be passing "id" in the "tidy_maps.delete = function()"??
You could pass it using your ajax request with the help of the data tag. api.jquery.com/jquery.ajax
0

Ok I've played around a little and amended the JS slightly:

tidy_maps.delete = function() {
var confirm_remove = confirm("Do You Want to Remove This Event?")
if(confirm_remove) {
    $.ajax({
        type:'POST',
        url:'removedata.php',
    });
    window.location = "http://www.google.com/";
}
else {
    alert("ERROR!!!!");
}
}

So when Confirm is YES, i threw in a redirect to Google just to see what happens. When YES is clicked in the confirm box, it redirects the page to Google but does not delete the row from the DB

4 Comments

No, just looking at it now again. It seems to be going to the removedata.php script but that itself does not delete the intended row. I'm just looking at another way, by sending the lat and lon of the map marker and using them instead of id
Well, do you pass the id to removedata.php?
type: 'POST', url: 'removedata.php', data: { id: <the_id_you_want_to_remove> }, success: function() { ... }
the thing is, the ID being sent will be that of whatever marker is being accessed. So instead of using a numerical value like'5', i need it to be a variable, so should the ID be sent like: "data: {id: $id}" ?
0

Try this

var id = 5;
var request = $.ajax({
  url:'removedata.php',
  type: "POST",
  data: "id="+id,
  success: function(data){
     console.log(data);
  }
});

get post value in removedata.php

 //get post value
 $id = intval($_POST["id"]);
 $sql = "DELETE FROM events WHERE id = " . $id;

1 Comment

No because that will be sending the ID as 5 and deleting only that markers row. I want ID to be a variable so it deletes what ever event marker that is selected.

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.