0

I have a php-file with a link in it. When pressing the link a jQuery alert popup shows up asking the user if he would like to proceed.

In the alert popup I want to be able to ask the user if he would like to proceed in different languages depending on what language he have chosed from the begining on my site.

So my code partially looks like this and some of the code is to explain how I think about it. example.php:

<?php
    $lang = $this->lang->line('alert_message');
    echo '<a href="file.php" class="link-alert">Accept</a>';
?>

functions.js:

$("a.link-alert").click(function(event){

    var answer = confirm($lang);

    if(answer){
        // go to destination
        return true;
    }else{
        // cancel
        return false;
    }
});

As seen above $lang is in the .php-file and I want to print it through an alert in functions.js. I preferably dont want to do inline js-code, though this would be messy.

1 Answer 1

1

Add it as a data attribute like this:

echo '<a href="file.php" data-lang="'. $lang .'" class="link-alert">Accept</a>';

And fetch it in the click event like this:

var lang = $(this).data('lang');

As a side note, you could refactor your code a bit:

return confirm(lang);
Sign up to request clarification or add additional context in comments.

Comments

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.