Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter in a Javascript confirm function?

I have something like that in my code:

<?php foreach($clients as $client): ?>
    <tr class="tableContent">
        <td onclick="location.href='<?php echo site_url('clients/edit/'.$client->id ) ?>'"><?php echo $client->id ?></td>
        <td>
            <a class='Right btn btn-danger' onClick="ConfirmMessage('client', <?php $client->id ?>,'clients')">
                <i class="icon-remove-sign icon-white"></i>
            </a>
        </td>
    </tr>
<?php endforeach  ?>

that's actually the view. So when the user click on the delete button (thr one with the btn-danger class) I'd like him to confirm his choice with a javascript confirmation box message. You can find that script in the header

function ConfirmMessage(type, id, types) {
    if (confirm("Are you sure you want to delete this ",type," ?")) { // Clic sur OK
       document.location.href='<?php echo site_url(); ?>',types,'/delete/',id;
    }
}

So here is my question:

I would like the $type to be replaced by a paramenter (like client, article, post .. ) that I'll pass to the function. And i would like to get the $client->id parameter as well. I'm bad in javascript and as you already have guess, It is obviously not working at all.

like image 278
Miles M. Avatar asked Nov 30 '25 02:11

Miles M.


2 Answers

Concatenate strings in JavaScript with + (versus . in PHP):

confirm("Are you sure you want to delete this " + type + " ?");
like image 118
N Rohler Avatar answered Dec 02 '25 14:12

N Rohler


confirm only takes one argument (a string) which is the message that is shown to the user.
If you want to insert a variable into that message, you shoul concatenate the strings using the + operator :

function ConfirmMessage(type, id, types) {
    if (confirm("Are you sure you want to delete this " + type + " ?")) { // Clic sur OK
       document.location.href='<?php echo site_url(); ?>' + types + '/delete/' + id;
    }
}
like image 34
gion_13 Avatar answered Dec 02 '25 16:12

gion_13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!