0

I'm trying to make an app where if you click on a div, the data-id will be put into a variable, which will give PHP the select parameters to look for. Here's my code to better explain it.

HTML:

<div class="box" data-id="caption"></div>

JQuery:

$('.box').click(function () {
        var caption = $(this).data('id');
});

After Googling I found the best way to do this is through AJAX, which I then proceeded to try:

$.ajax({
    url: 'index.php',
    type: 'GET',
    dataType: 'json',
    data: ({ caption }),
    success: function(data){
        console.log(data);
    }, error: function() {
        console.log("error");
    }
});

However, this doesn't seem to work. If there's a better way to do what I mentioned above, I'm open to new ideas.

EDIT

Here is my PHP code.

if(isset($_GET['caption'])){
    echo $caption;
    $select = "SELECT * FROM pics WHERE text = '".$caption."'";
    }
?>
1
  • 3
    Your data parameter in your ajax call is invalid. It should be something like data: {caption: caption}. Commented Apr 16, 2016 at 5:47

1 Answer 1

2

Look through the api at jQuery.

Your data key should contain a json object -

$.ajax({
    url: 'index.php',
    type: 'GET',
    dataType: 'json',
    data: {caption: caption},
    success: function(data){
        console.log(data);
    }, error: function(error) {
        console.log(error);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

It was just the error from the error: function(), nothing else.
@Vic You will need to modify your question to include the PHP code as well as that's what's causing the error.

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.