2

I am using an anchor tag to run the JS function confirm(), and if the user clicks okay it adds "deltopic=id", and use the $_GET method to get 'deltopic' to delete that specific item, but it seems to not be finding the $_GET['deltopic']

<script language="JavaScript" type="text>
  function deltopic(title, tid) {
    if(confirm("Are you sure you want to delete '" + title + "'")){
      window.location.href = "?viewtopic.php&deltopic=" + tid;
    }
  }
</script>

<?php
if(isset($_GET['deltopic'])){
if($_GET['deltopic'] !=='1'){
    $query = "DELETE FROM `bkg`.`bkg_topics` WHERE `bkg_topics`.`topic_id` = :topicid";
    $query_params = array(':topicid' => $_GET['deltopic']);
    try{

        $stmt = $db->prepare($query);
        $result = $stmt->execute($query_params);

        header('Location: index.php?forums&action=deleted');
        exit;
    } catch(PDOException $e) {
        $error[] = "An error has occured. Please try again later.";
    }

}
echo "deltopic is set";
}

I added the last echo just to see if its checking if deltopic isset, or if it was an error in my SQL that I was just not seeing. However I do not see "deltopic is set".

I am not sure what I am doing wrong and/or what I am forgetting. I have code similar to this, that does work, and double checked it closely.

EDIT: I saw the error I was doing in my 'window.location.href' string, where I was adding .php to the end of ?viewtopic, making it ?viewtopic.php. Removing the .php fixed my issue as well.

4
  • 2
    Try window.location.href = "viewtopic.php?deltopic=" + tid; Commented Feb 14, 2016 at 1:43
  • Although, ajax through jquery would be a lot better Commented Feb 14, 2016 at 1:44
  • I am not too familiar with using ajax & mysql databases. I changed it to window.location.href = "viewtopic.php?deltopic=" +id;, ran into some other problems but figured it out ^.^ Thanks. Commented Feb 14, 2016 at 1:50
  • If you want to try AJAX, please peak at jQuery implementation: api.jquery.com/jquery.ajax Otherwise, please look at my answer below. Commented Feb 14, 2016 at 1:52

1 Answer 1

1
window.location.href = "?viewtopic.php&deltopic=" + tid;

will result in URL where "viewtopic.php" is a part of URL QUERY. Perhaps you want it in the PATH:

window.location.href = "viewtopic.php?deltopic=" + tid;
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh I actually may see the error I may have done. In my index.php I use the $_GET method to retrieve what information to display on the page, depending on what $_GET is set. When I had ?viewtopic.php&deltopic="+ tid; I didn't mean for the ".php" part to be in there.
Awesome! We do not see the PHP file name in your question, so I have presumed it's "viewtopic.php". Thank you.
Looking forward, I suggest you use $_REQUEST** instead of **$_GET to cover both "post" and "get": will be handy if you switch to ajax.

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.