0

I am echoing a html form with php which has the following lines:

<a class='btn btn-sm btn-danger pull-right' onClick=return confirm('Delete This account?'); href='".url('deleteuser/'.$order->id)."' >Delete</a>

This correctly goes to the url specified. However, the onClick method is never triggered. Any help is much appreciated.

3
  • have you checked the console log? Commented Mar 15, 2014 at 18:54
  • 1
    you didnt surround whatever in the onCLick with quotation marks Commented Mar 15, 2014 at 18:55
  • there's no error shown in the console log Commented Mar 15, 2014 at 19:00

2 Answers 2

3

EDIT: Clarified pure HTML versus echo string.

Put quotes around the onclick attribute value. i.e. in pure HTML:

onClick="return confirm('Delete This account?');"

So if you are outputting this via echo, you need to escape the respective type of quote characters with a backslash.

echo 'onClick="return confirm(\'Delete This account?\');"';

or

echo "onClick=\"return confirm('Delete This account?');\"";
Sign up to request clarification or add additional context in comments.

2 Comments

that works only when it is not echoed from php. this is the full php echo: echo "<tr><td>".$order->username."</td> <td> <a class='btn btn-sm btn-success pull-right' href='".url('manageuser/'.$order->id)."'>Edit</a></td> <td><a class='btn btn-sm btn-danger pull-right' onClick=return confirm('Delete This account?'); href='".url('deleteuser/'.$order->id)."' >Delete</a></td> </tr>";
Good point, Monty did say he was using echo. I updated the answer to be more clear.
2

Upvoted @faintsignal answer because it is correct, your confusion of single quotes is causing, however a much better way to do this is separate the PHP from the HTML:

<?php
    foreach($someItem as $someKey => $order){
?>

<a class="btn btn-sm btn-danger pull-right" onClick="return confirm('Delete This account?');" href="<?php echo url('deleteuser/'.$order->id); ?>">Delete</a>

<?php
    }
?>

Just providing an alternative, and it's generally considered good practice because you have clear separation of duties and its easier to maintain.

EDIT: provided a super generic example of using foreach, obviously you can mod for your setup but it shows you can separate the function and it will run just fine.

5 Comments

this will work well, but my other problem is that the echo is done inside a foreach loop
@MontySwanson you can still use it, i'll mod my answer with an example.
I overlooked the echo aspect of the question and edited my answer to be more clear. Thanks, @PlantTheIdea. I find there are two camps of programmers when it comes to mixing PHP code with direct output, but for programmers also new to HTML it is probably simpler to separate them until they have the basics down.
@faintsignal - quite the opposite. PHP coders that work in a hobbyist environment often echo HTML, whereas PHP coders that operate in an enterprise environment where maintenance by third parties is common consider the separation of duties best practice. often programmers new to PHP don't understand that you can perform that separation, and its easier to simply echo the HTML. but whatever works for you.
@PlantTheIdea I'm not against either style. I'm a switch-hitter, depending on the context. I'm merely suggesting that those new to HTML could benefit from getting acquainted with it in its pure form before moving on to generating it server-side. Then again, I may be biased from learning HTML when most websites were still static.

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.