Miles M.
Miles M.

Reputation: 4169

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.

Upvotes: 2

Views: 9272

Answers (2)

N Rohler
N Rohler

Reputation: 4615

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

confirm("Are you sure you want to delete this " + type + " ?");

Upvotes: 9

gion_13
gion_13

Reputation: 41533

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;
    }
}

Upvotes: 3

Related Questions