Hendrik Human
Hendrik Human

Reputation: 135

Correct nesting of apostrophe's/quotation marks in html/php statement

So im trying to make it possible for a user to select a file from a list and then delete the file from the server. I know there are alot of safety concerns that I am neglecting here but I really just need to get a basic version of this to work for now. So following up on some other threads here is the linkby which u select a file to delete:

echo '<a href="delete.php?file=ebooks/'.$name.'" onclick="return confirm(\'Are you sure?\')">Delete</a>';

My problem is that where I ad the variable $name to the link it breaks my nesting of quotation marks, escaping them doesn't work since that just renders it as a string "$name" and not a variable. Can anyone please give me or help me to see what the correct way is to write this statement? thanks

Upvotes: 0

Views: 714

Answers (2)

Patrice Levesque
Patrice Levesque

Reputation: 2114

Wrap $name in urlencode():

echo '<a href="delete.php?file=ebooks/'.urlencode($name).'" onclick="return confirm(\'Are you sure?\')">Delete</a>';

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174977

I like to use Heredoc syntax for complex strings (such as HTML):

echo <<<HTML
<a href="delete.php?file=ebooks/{$name}" onclick="return confirm('Are you sure?')">
    Delete
</a>
HTML;

No need to escape quotes, no need to end the string for variables. Simple and looks good.

Upvotes: 0

Related Questions