1

I am trying to pass a string from a query into a javascript function.

An integer will pass into the function but string will not.

echo "<a href='#' onclick='delete_game({$title});' 
class='btn'>Delete</a>";

<script type='text/javascript'>
function delete_game(title){
    var answer = confirm('Really?');
    if(answer){
        window.location = 'delete.php?id=' + title;
    }
}
</script>

I expected the javascript function to be executed, but instead nothing happens.

7
  • 4
    The string needs to be quoted inside of the function call. delete_game(\"{$title}\") Commented May 23, 2019 at 13:57
  • you shouldn't mix PHP + JS - it's bad practice. Inline js/css is also bad practice. Commented May 23, 2019 at 13:58
  • instead nothing happens I don't think is true. I would expect this to throw a console error. Commented May 23, 2019 at 13:59
  • 1
    @Peter I think that's the preferred - but don't think it's compulsory Commented May 23, 2019 at 13:59
  • @treyBake You're right. A google search told me it does the same thing. Commented May 23, 2019 at 14:06

2 Answers 2

1

Why don't you use ajax for this? As mentioned in comments mix PHP/JS isn't good.

In your HTML, you can do something like

I'm assuming that you are using Blade.

<a href="#" onclick="return deleteGame({$title})">Delete Game</a>

Then in your javascript, you do this using jQuery:

function deleteGame(title){
    var answer = confirm('Really?');
    if(answer){
        $.ajax({
          url : "your-php-file.php",
          type : 'post',
          data : {
               title : title
          }
     })
     .done(function(msg){
          $("#result").html(msg);
     })
     .fail(function(error){
          console.log(error);
     }); 
    }
}

In your PHP you process receiving the data from post $_POST

$title = $_POST['title'];

You can understand better the Ajax function of jQuery here.

Sign up to request clarification or add additional context in comments.

Comments

0

A few things:

I would change window.location to window.location.href
Change your echo to:

echo "<a href=\"#\" onclick=\"delete_game('" . $title . "');\" class=\"btn\">Delete</a>";

Check if $title is set

var_dump($title);

If you'd like to make it a bit cleaner and are prepared to use jQuery:

<a href="#" class="btn delete" data-title="<?= $title ?>">Delete</a>
<script type='text/javascript'>
$(document).on('click', '#delete', function () {
   var title = $(this).data('title');
   var answer = confirm('Really?');
    if (answer){
        window.location.href = 'delete.php?id=' + title;
    }
});
</script>

4 Comments

Thanks for this it works great. One question though: why do we use a \ before the double quote?
@RaviShankerGowda actually delete_game('$title') accepts string value, you can make it as onclick='delete_game('$title'); that would work fine too but when you use " this would break the string so you need to add \" so that It may not break the string
@RaviShankerGowda It's an escape character. Since you open the echo with "`, you cannot use it in your string without escaping it.
@RaviShankerGowda - If you don't want to care about escaping quotes (which makes the code a bit messier), I would recommend simply closing the PHP-tag, write your HTML and then open it again. Something like this: ?> <a href="#"...>...</a> <?php. Then most IDE's can syntax highlight your HTML as well.

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.