I've got a real problem, I want to make an alert using PHP to call JavaScript, but my PHP is inside a textarea, is there anyway to echo out the script outside the textarea without moving the PHP code?
<textarea>
<?php
echo "<script language='javascript'>alert('Hello!')</script>"
?>
</textarea>
No, not really. Once you're inside a <textarea>, ANY text, including html tags, will be considered part of the textarea's "to be edited text", up until the first </textarea>. You will have to output your script tag outside of the textarea, which means moving your PHP code.
<?php
echo "<script type="text/javascript">alert('Hello!');</script>"
?>
<textarea></textarea>
This function and the alert you can't write inside the textarea
Related
Normally when we use:
<textarea><?php echo 'ok'; ?></textarea>
We get <?php echo 'ok'; ?> displayed inside the textarea. But on my website I get the ok inside the textarea. So the php code gets executed instead the php code is being showed.
Anybody knows how this can happen? To be clear, I don't want it. I want the code to be displayed.
If you're running a .php file on a PHP-enabled web server... Use the HTML entities for the text you want (specifically the < and >, though potentially more):
<textarea><?php echo 'ok'; ?></textarea>
Or you can treat it as a string server-side and HTML-encode it with PHP:
<textarea><?php echo htmlentities("<?php echo 'ok'; ?>"); ?></textarea>
Alternatively, if you don't want to use PHP at all, then simply don't use it. Serve an .html file and/or disable PHP on the server:
<textarea><?php echo 'ok'; ?></textarea>
I am trying to hide our mailing address on our website, until someone cliks a button to "load" the address. I am doing it like follows:
Homepage.php:
<button onclick="test()"> Click </button>
<div> </div>
<script>
function test(){
$.ajax({url:"address.php", success:function(result){
$("div").text(result);}
})
}
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
Address.php:
<?php
function php_func(){
echo '<span><?php echo $address; ?></span>';
}
php_func();
?>
This works in echoing the text onto homepage.php, but it's not loading the PHP function. Just showing the function as text as seen here:
I tried $("div").write(result);} and it won't even load.
$address is already defined elsewhere. Any tips?
You're trying to write code which outputs code which outputs the address. Why? You're already in the context of outputting something from the PHP code:
echo "something...";
If what you want to output is the value of $address then just output that:
echo "<span>$address</span>";
I suspect the reason you did it that way is because you're expecting the currently loaded page to parse and execute that PHP code. This is a fundamental misunderstanding of how these technologies work. The PHP code for that page executed once, on the server, and delivered the resulting HTML/CSS/JavaScript to the client.
The AJAX operation is making a new, separate request to another PHP resource which will execute on the server and output back to the client. In this case it's just outputting a string value, which the client-side JavaScript code will then write to an element on the page:
$("div").text(result);
(This is a good opportunity for you to use your browser's debugging tools and observe the AJAX request/result in the network tab, to see what's actually being sent/received. At no point should actual PHP code be visible to the browser. All of that is executed on the server.)
The reason this is important is because, if this is the case, then you are likely misunderstanding where $address is defined. If it's defined in the PHP script which rendered the page you're looking at, that doesn't mean it's defined in address.php. If the code you're showing us for address.php is the entirety of that page then $address is not defined.
So you'll need to define $address on that page.
After having said all of that... You might find it much easier not to involve AJAX for this at all in the first place. Just output the address to the page but style the <span> to not be visible. Then when the user clicks the button, make it visible. No need for the complexity of an entirely new HTTP request:
$('button').click(function () {
$('span').show();
});
span {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Click</button>
<span>this is the address</span>
You don't use <?php echo inside strings; that's only used when you're in a section of the script that's outputting literal text, not executing PHP code.
If you're in PHP code doing echo, you use variable substitution or concatenation.
<?php
function php_func(){
echo "<span>$address</span>";
}
php_func();
?>
You'll need additional code to set the $address variable; I assume you just left that out for simplification in the question.
<?php
function php_func(){
echo '<span>' . $address .'</span>';
}
php_func();
?>
this should work, u can't use 'echo' and inside echo open 'php' tag to use again.... more another 'echo'
Why does this not work? This is the first thing in the body:
<?php
if(isset($_POST['submit'])){
echo "<script>$('.classToShow').show();</script>";
}else{
echo "<script>$('.classToShow').show();</script>";
}
?>
classToShow is a simple div in the body. It won't show up and its not depending on the boolean condition, it must be the code...
While this works:
<?php
if(isset($_POST['submit'])){
echo "<script>alert('works');</script>";
}else{
echo "<script>alert('works');</script>";
}
?>
So the simple JavaScript works, but the jQuery doesn't... Why is this?
This is your problem:
This is the first thing in the body
At that point the element with the class of classToShow does not exist yet, so nothing happens. You should wait for the DOM to be ready before you run that code.
On the other hand, if you just want to show something when a POST request was made, you can add it directly using php and you don't need jQuery to do that afterwards.
A common solution would be to show it directly using php and then use javascript to hide the message after a certain timeout.
You can use $(document).ready() and inside that write the code
On a page, I have an HTML form with a <select> tag. This is dynamically filled with php... using content with spaces.
<select text="52 state street" value="53 state street">
On another page, I create the links to it.
<a href="example.com/page/?var=53%20state%20street">
Is there something I can replace the %20's with that will fix this issue?
Note Using WordPress and a plugin called "gravity forms". ?var dynamically selects an option through the plugin I am using in WordPress. I don't think my issue is with WordPress or the plugin, but rather a lack of php knowledge. I don't have access to the html form code aside from filling it with variables (it is called in via shortcode).
You can try the urldecode function. http://php.net/manual/en/function.urldecode.php
you can also use trim function like this
<?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str," ");
?>
Output like this HelloWorld
I have a variable in which a complete html is saved.
$body= $myhtmlpage;
<a onclick="openWin(' <?php echo htmlspecialchars(json_encode($body)) ?>');" href="javascript:void(0);"> Click </a>
and i have this javascript function which display the text in new window.
<script type="text/javascript">
function openWin( str )
{
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(str+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
</script>
When there is simple text in my body, it works fine. but if there is some html then it does not display, I am new to javascript. please tell me how can i prepare my HTML that it should be passed to Javascript html. i tried htmlspecialchars(json_encode($body))
functions but still having problem.
Uncaught SyntaxError: Unexpected identifier
You will have a long battle trying to get a lot of HTML to work as a string variable in Javascript. It would be far better for you to put that markup in a hidden block (like a DIV) in your markup and then just get the contents of that markup and show it in your window.
This has the added advantage of allowing your hidden markup to be validated. It is very hard to debug a lot of html markup stuffed into a string variable, but when included in the DOM as real markup it makes your life much easier.
UPDATE: Adding some sample code:
<div id="my_hidden_content" style="display:none;">
<?php echo $body; ?>
</div>
<a onclick="openWin('my_hidden_content');" href="javascript:void(0);"> Click </a>
Now the javascript:
function openWin( contentId )
{
var contentContainer = document.getElementById(contentId);
var content = contentContainer.innerHTML;
myWindow=window.open('','','width=400,height=400');
myWindow.document.write(content+"<p>This is 'myWindow'</p>");
myWindow.focus();
}
Firstly, you do not need to use json_encode(), that is just confusing the situation.
Secondly, the problem will be that your HTML contains quotes. This will result in a syntax error in the HTML you output, since htmlspecialchars() does not escape quotes.
Use htmlentities() with the ENT_QUOTES flag instead. So change the line to this:
<a onclick="openWin('<?php echo htmlentities($body, ENT_QUOTES) ?>');" href="javascript:void(0);">Click</a>
Thirdly (although it should probably be firstly since it is the most important point) your approach to this is all wrong. If you're opening a new window, you should have it load a page from the server and generate the HTML when the window is opened.
You can try html code inside php code, instead php code inside html.
for e.g.
<?php
echo "<a onclick='openWin(\" ".htmlspecialchars(json_encode($body))." \")'>Click</a>";
?>
That's because, HTML isn't JSON. For that simply use: htmlspecialchars($body)