How to use php to output this html code?
The html code is this.
Piano Programs
but I want to use php to show this code
$program_name = "piano_programs";
echo "Piano Programs";
but.....doesn't work, any idea ,thanks
Backslashes to escape the quotes:
echo "Piano Programs";
You need to add slashes before onclick's two double quotes and move your single quote to the right side of .php.
echo "Piano Programs";
I like to curly-bracket my variables so that they stand out in my editor and so that they don't get mixed up with the text that immediately follows.
Related
I have an echo in my php page which has a javascript in it.
it looks like this:
echo '<ons-list-item onclick="fn.load('home.html')" tappable>';
I need to escape the single quotes on fn.load('home.html')
so i did:
echo '<ons-list-item onclick="fn.load(/'home.html'/)" tappable>';
But that doesn't really work..
could someone please advise on this?
Thanks in advance.
Try using double quotes around "home.html".
Or you need to escape ", so it won't be interpreted as end of string. Use \ to escape the quotes as you said.
I have some HTML. In my HTML, there is a form that uses a PHP script. When I submit this form, it sends me to a blank page. I have tried this, as suggested in a similar question, but it doesn't work because I have HTML events that call functions with arguments, so both " and ' are already used... I am new to PHP, so, is a there a solution to this that I do not know of?
Use the backslash character \ to escape your quotes like so \". The backslash causes PHP to treat the quote as a regular character. :]
Example:
<?php echo "Hello World"; ?>
outputs Hello World
<?php echo "\"Hello World\""; ?> outputs "Hello World"
Escape your quotes.
echo "<h3>First</h3>"
\" is an escaped quote. When a quote is escaped, the environment treats it as an ordinary character, not as a quote. \' Single quotes can also be escaped if need be.
(Not the best example since I'm not also using singles, but you get the point.)
This question already has answers here:
single quote inside double quote in php
(3 answers)
Closed 9 years ago.
The below outputs
href="javascript:showBed(" a114:1')'
when I want it on the form
href="javascript:showBed('A114:1')"
in order to get javascript to work. I had a look at this site but coudn't get it to work so I gave up. Perhaps you could give me a hint on how the corrent syntax would be?
echo("<a href='javascript:showBed('" . $row['Bed'] ."')' target='main' class='larmlink'>link</a>");
Thanks =)
Your output is not what it would output, but it is how it would be interpreted (HINT: don't look at a parsed DOM tree, look at the source).
echo("<a href='javascript:showBed('" . $row['Bed'] ."')' ...
==>
echo("<a href=\"javascript:showBed('" . $row['Bed'] ."')\" ...
You really should be using the more standard double quotes around HTML element properties. As such, it is probably best to use single quotes in PHP. I would suggest this:
echo('link');
To print the double-quote character, you can escape it by doing \"
echo("<a href=\"javascript:showBed('" . $row['bed'] ."')\" target='main' class='larmlink'>link</a>");
Live demo
When you want to output variable data to JavaScript, it is good to use json_encode() so that all special characters are escaped automatically. The htmlspecialchars() escapes any values for use in the HTML attribute value.
echo '<a href="',
htmlspecialchars('javascript:showBed(' . json_encode($row['Bed']) . ')'),
'" target="main" class="larmlink">link</a>';
Note that I use single quotes for PHP string literals so that PHP doesn't have to search through my string for a variable to replace. You don't have to do this, but I recommend it.
I like to use sprintf (or printf, but sprintf is easier to refactor) for long strings like this so it's easy to see the template:
echo sprintf("<a href='javascript:showBed(\"%s\")' target='main' class='larmlink'>link</a>", $row['Bed']);
I'd also consider using addslashes on the $row['Bed'] variable in case it has quotes in it.
Using the heredoc syntax often makes code with mixed quotes easier to understand:
echo <<<EOD
link
EOD;
As others mentioned, if the value of your $row['Bed'] might contain single or double quotes, you have to escape it with addslashes.
You can use the heredoc syntax to avoid to escape anything:
echo <<<LOD
link
LOD;
Notice that if your variables contains some quotes you must use the addslashes function or str_replace before.
Another good practive is to separate systematically all the html content from php code:
<a href="javascript:showBed('<?php
echo $row['Bed'];
?>')" target="main" class="larmlink">link</a>
try this one:
echo("<a href='javascript:showBed(\"" . $row['Bed'] ."\")' target='main' class='larmlink'>link</a>");
PHP is echoing JavaScript (I'm using the jQuery library) something like this:
echo 'var users = $("#add").val().split("\n");';
However, the \n is creating a line break in what the echoed script looks like, and therefore breaking the JavaScript. Is there a way to circumvent this?
Many thanks!
The \n is an escape sequence meaning newline. Backslashes are the beginning of escape sequences, to output a backslash then write \\. So you want \\n. Other useful escape sequences include the quote: use \" to put a quote into the string instead of ending the string.
echo "var users = $(\"#add\").val().split(\"\\n\");";
Not sure If you looking for this
echo "<script>alert('Line1\\\\nThis still in Line1')</script>";
I am using a bit of JavaScript like this:
echo '<a href="javascript:playSong'."('$row[artist]','$row[title]','$row[sourcefile]')".'">';
My problem is that sometimes my $row[artist] and $row[title] variables contain double qoutes.
When this happens it breaks the javascript:playSong(); function call.
For example if the line was output like this:
<a href="javascript:playSong('Danny Elfman','Beetlejuice Theme (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')">
Everything would be fine.
But sometimes the function will look like this:
<a href="javascript:playSong('Danny Elfman','Beetlejuice "Theme" (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')">
Which would then cause my site to think the command ends at the double quote before "Theme" and thus cause it to fail.
Is there a way I should be properly quoting my javascript so it treats double quotes inside the function as text and no the end of the function.
I am using addslashes() and have tried various other encodings but nothing like that seems to work.
The best solution here is to stop using href="javascript:… and start using unobtrusive JavaScript and progressive enhancement.
If you do want to continue down this route, then you need to remember that you are dealing with three different languages and generating one from the other in a chain.
Start with the JavaScript. Then make it work with the HTML. Then make it work with the PHP.
javascript:playSong('Danny Elfman','Beetlejuice "Theme" (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')
There are no syntax errors here. You just have double quotes in a string.
href="javascript:playSong('Danny Elfman','Beetlejuice "Theme" (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')"
Now you hava nested the JavaScript in an HTML attribute which is delimited with double quotes. This means that the double quotes in the JS are now a problem as it terminates the attribute value half way through the script.
Deal with these quotes in the usual way for HTML. Replace them with an HTML entity: "
href="javascript:playSong('Danny Elfman','Beetlejuice "Theme" (Kamei Halloween Edit)','2009-10-31-10-52-01.4521.data','28330')"
Then we get to the PHP:
echo '<a href="javascript:playSong'."('$row[artist]','$row[title]','$row[sourcefile]')".'">';
Dealing with quotes inside quotes inside quotes is a pain. So don't try.
href="javascript:playSong('<?php echo $row[artist] ?>','<?php echo $row[title] ?>','<?php echo $row[sourcefile]; ?>')"
You seem to have lost an argument between examples there.
Follow the normal rules for dealing with inserting content into HTML with PHP: htmlspecialchars
href="javascript:playSong('<?php echo htmlspecialchars($row[artist]); ?>','<?php echo htmlspecialchars($row[title]); ?>','<?php echo htmlspecialchars($row[sourcefile]); ?>')"
Escaping ONLY with backslash won't help you because you also need to escape them in html, try using ' for single quotes and " for double quotes in your embedded js functions.
This should do what you need.
Sinan.
Edit: Oh. You need to escape it in html. Try htmlspecialchars: