Jump to content

echo javascript inside anchor element

Go to solution Solved by Barand,

Recommended Posts

Hey, sorry guys I know this is a easy one but I've been at it all day and can't seem to figure it out.

I want to include this:

onclick="return confirm('Are you sure?')"

Inside this line of code:

<td> <?php echo("<a href='delete.php?user_id=" . $d_row["FormId"] . "'>Delete</a>"); ?></td>

I am deleting a record, I have all the SQL, it's all fine, but just this annoying little problem. I have tried many things , including escaping the double quotes but I have got no where. 

I have tried the following but none of them work:

<td> <?php echo("<a href='delete.php?user_id=" . $d_row["FormId"] . "' onclick="return confirm('Are you sure?')">Delete</a>"); ?></td>

  <td> <?php echo("<a href='delete.php?user_id=" . $d_row["FormId"] . "onclick=\"return confirm('Are you sure?')\"'>Delete</a>"); ?></td>

<td> <?php echo("<a href='delete.php?user_id=" . $d_row["FormId"] . " \"onclick=\"return confirm('Are you sure?')\"'>Delete</a>"); ?></td>


Link to comment
Share on other sites

So - what IS happening?  Are you getting an error message?  Or does the JS never show up?

Here is what I converted it to:

$formid = $d_row['FormId'];
echo "<td><a href='delete.php?user_id=$formid' onclick=\"return confirm('Are you sure?')\">Delete</a></td>";

Couldn't make it work with the reference to the array element so I moved it to a simple variable.

Link to comment
Share on other sites

  • Solution
Posted (edited)

Curly braces should do the trick

$d_row['FormId'] = '98765';

echo "<td><a href='delete.php?user_id={$d_row['FormId']}' onclick=\"return confirm('Are you sure?')\">Delete</a></td>";


<td><a href="delete.php?user_id=98765" onclick="return confirm('Are you sure?')">Delete</a></td>


Edited by Barand
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.