php - alternative for document.getElementById [duplicate] - php

This question already has answers here:
PHP quotes inside quotes
(3 answers)
Closed 5 years ago.
hi guys i just want to know if there is an alternative syntax for onclick="document.getElementById('id01').style.display='none'" on php. Beceause its not working even it is inside the echo.tnx
here is my code

First up: document.getElementById is a construct used in JavaScript and has nothing to do with PHP.
The problem you are running into is that you are not escaping the single quotes in your string:
echo '<button onclick="document.getElementById(' <- there is your problem
PHP thinks that the string ends here since you told it so with '.
But the string is in fact not at its end so you need to escape the single quote in order to make it work:
echo '<button onclick="document.getElementById(\'id0\')...';
This goes for all single quotes in your string of course.

Related

How to fix double/single quotes in PHP [duplicate]

This question already has answers here:
How to work with double quotes and single quotes in php [duplicate]
(2 answers)
Closed 3 years ago.
this should be an easy question but im finding it hard to get a working answer, i am using Sendgrid php api and it requires that i do the following code in this layout.
in my code i have:
//this part is the sendgrid part:
$email->addContent(
"text/html", "//i need to put the bottom image code into here"
<?php echo '<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>'; ?>
the end result should look something like:
$email->addContent(
"text/html", "<img src="data:image/png;base64,'.base64_encode($portal['image']).'"width="1024px" height="768px"/>"
i cant figure out how to use the correct quotes so that this code will work since it uses double and single quotes inside of eachother
You have to match the starting and ending quotes for a string. You're starting the first string with ", but then ending it with ' before the concatenation ..
If you want to put double quotes inside the string, use single quotes around it, and vice versa. If you need to include the same kind of quote inside the string as you use two create it, you have to escape the inner one.
$email->addContent(
"text/html", '<img src="data:image/png;base64,'.base64_encode($portal['image']).'" width="1024px" height="768px"/>'

Using html inside double quoted echos [duplicate]

This question already has answers here:
Double quotes within php script echo
(6 answers)
Echo double quotes inside double quotes issue [duplicate]
(2 answers)
PHP quotes inside quotes
(3 answers)
How to use single quote inside an echo which is using single quote
(7 answers)
Closed 3 years ago.
I want to change the colour of some text and it has to be inside double quotation marks as it is a variable. This is the only instance in which it happens so I won't bother with CSS.
echo "$playername ";
I tried
echo "<font color="yellow"> $playername </font>" ;
But it is not working, I assume this is because it does not like double quotes inside double quotes. Any help would be much appreciated.
The necessity of quoted attribute values only comes into play when dealing with values that may contain contain whitespace or characters meaningful to HTML, such as "<". So, in this case, the OP does not need to quote the attribute value; see here.
If one insists on quoting an attribute value, one may use single quotes or else quote double quotes as in below example:
<?php
$playername = "Nobody";
$yellow="\"yellow\"";
echo "<font color=$yellow>$playername</font>";
Note: while the FONT tag is obsolete, it nonetheless works at any rate in my version of Google Chrome (YMMV). Note, the MDN adamantly discourages using the FONT tag:
Do not use this element! Though once normalized in HTML 3.2, it was
deprecated in HTML 4.01, at the same time as all elements related to
styling only, then obsoleted in HTML5.

Replace single quote in a string not working [duplicate]

This question already has answers here:
How to use str_replace to replace single and double quotes
(7 answers)
Closed 4 years ago.
I'm having trouble replacing a single quote in a string, the purpose is to create part of an URL
For example : If I type in "Villeneuve d'ascq" I would want to have :
Villeneuve+d%27ascq", %27 being the ascii equivalent of (')
I tried using str_replace("'", ord("'"), string_name) but it doesn't seem to work
Any help would be appreciated and feel free to ask for any more details
Please try this :
echo 'test';
You can also check this at :
PHP MANUAL

How to write HTML code inside PHP [duplicate]

This question already has answers here:
Is there a better way to write HTML strings in PHP?
(8 answers)
Closed 8 years ago.
My PHP code begins with the ?php tag and ends with the ? tag and my file extension is php. If I want to write part of my PHP code inside HTML, what is the general way of doing it?
For instance:
echo '<strong>' .$element .'</strong><br>';
Do I need to always use single quotes for the HTML tags used inside of a PHP file? Or double quotes or no quotes?
The first thing you need to learn is that PHP doesn't care about HTML.
PHP doesn't understand that it's working with HTML, it just sees text and dutifully sends it to the browser. The browser then makes sense of it as HTML.
So, since <strong> is a string, it must be treated as a string. In other words, as "<strong>", '<strong>' or even a heredoc:
<<<HEREDOC
<strong>
HEREDOC;
(Probably not appropriate in this case :p)

Dynamic Location header PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is PHP not replacing the variable in string?
I would like to have my PHP redirect headers dynamic. Currently I have:
header('Location:message.php?ref=conversation&conv=$conv_id')
But that redirects the user to:
doamin.com/message.php?ref=conversation&conv="$conv_id"
What am I doing wrong?
You are putting a variable in single quotes, change to double quotes.
Change it into this:
header("Location:message.php?ref=conversation&conv=$conv_id");
Or this:
header('Location:message.php?ref=conversation&conv=' . $conv_id);
(I prefer the latter, but that's just a personal thing.)
The thing is that in php, it parses variables inside a string if you use double quotes and doesn't if you use single quotes.
anything in single quote behave like a string
so
header('Location:message.php?ref=conversation&conv=$conv_id') will not work
should be
header("Location:message.php?ref=conversation&conv=$conv_id")
Not much, you should pass the var into the string
header("Location:message.php?ref=conversation&conv=" . $conv_id)
You can do one of these two:
header("Location:message.php?ref=conversation&conv=$conv_id");
OR
header('Location:message.php?ref=conversation&conv='.$conv_id);
you shouldn't place the variable in the single quote '. place it in the double " quote, will work

Categories