I want to echo the javascript from php in a heredoc. But its not working because of dollar sign. Escaped the dollar sign but it's still not working . Could anyone help out regarding this. Thanks. It gives an error
Uncaught ReferenceError: $ is not defined
<?php
echo '<!DOCTYPE html> <html> <head><title> hello world </title> </head> <body>' ;
$testscript=<<<EOT
<script>
\$('input[type=radio][name=gender]').change(function() {
alert(this.value);
});
</script>
EOT;
echo $testscript;
echo '</body></html>' ;
?>
jquery library was not loaded in the script
Related
I was passing an array in php to Javascript using json_encode and ajax. It seems that the only way is to use
echo json_encode($var)
which also prints out the information of $var on the page because of echo. Is there an easy way to hide the output? My code was like the follows,
main.php
<?php
include_once('testing.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="testing.js"></script>
</body>
<html>
testing.php
<?php
$var=array('1','2','3','4','5');
echo json_encode($var);
?>
testing.js
$.ajax({
url : './testing.php',
type : "GET",
dataType : 'json',
success : function (result) {
showstaff(result);
}
});
function showstaff(x){
console.log(x);
}
Running main.php print out the array('1','2','3','4','5') on the page and show the array in the console, but what I need is to just show the array in the console (i.e. hide the results from the page).
You don't need include_once('testing.php') in main.php. When main.php is sent to the browser, it will load testing.js, which will then execute testing.php via the AJAX call.
You can try this:
<script type="text/javascript">
var object = <?php echo json_encode($var); ?>;
</script>
Hope it helps, thanks!
I got no output when I use variable in val() function instead of fixed value. How can I fix this error.
Example: In the below example, when I use variable ($data="Glenn Quagmire"), I get no output.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val($data);
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>
Regards
Just use like this:
$("input:text").val("<?php echo $data; ?>");
Full code
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?php echo $data; ?>");
});
});
</script>
Don't use "$" for variables (that's PHP) in jQuery.
<?php echo 'data="Glenn Quagmire";'; ?>
$("input:text").val(data);
JavaScript running in the browser cannot access variables from a PHP program (which has finished running on another computer before delivering the page to the browser so the variable does't even exist any more).
You have to make PHP output the data to the JavaScript.
You can encode it using json_encode (because JSON is (more or less) a subset of JavaScript literal syntax). This will add quotes as needed and escape any special characters in your data (you don't have any in your example, but this is a safe, general approach that protects you from XSS).
$("input:text").val(<?php echo json_encode($data); ?>);
Use <?= $variable ?> to embed variable values in HTML, e.g.:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?= $data ?>");
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>
A very popular answer in StackOvwerflow suggested that in order to get any php variable within a javascript, simply use php withi angular brackets.
var x= <?php echo "val"; ? >
. However I see that the script given below entirely fails to run If I use this method.
Once I comment out that line script works fine.
<script>
var test=<?php echo "hi"; ?>;
alert(test);
document.title=test;
</script>
Even this simple script is not working.
The page is saved as .php
The page resides in WAMP server and is accessed as localhost/test.php
.
.
EDIT: Full code:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Test</title>
<script>
var test=<?php echo "hi"; ?>;
alert(test);
document.title=test;
</script>
</head>
<body>
<div id="test-canvas" style="float:left;width:100%;height:100%;"></div>
</body>
</html>
I believe your omitted the ' ' in the script
<script>var test = '<?php echo $variable; ?>';</script>
Hope this helps, good luck.
Try this :-
var test = '<?php echo "anything"; ?>';
alert(test);
The result of your php is:
var test=hi;
And that will make a problem because javascript needs quotes too when dealing with strings.
You can do this to solve the issue:
var test=<?php echo "'hi'"; ?>;
<html>
<head>
<script type="text/javasscript">
window.history.forward();
function noback()
{
window.history.forward();
}
</script>
</head>
<body onload="noback();" onpageshow="if(event.persisted) ;" onunload="">
aa
</body>
</html>
This is the code in HTML which forwards you to same page when you click back, but how to include this script in PHP code.
You can for example do something like that:
<?php
Your PHP Code
?>
Your Html code
<?php
Your PHP Code
?>
Your Html code
<?php
Your PHP Code
?>
Or
<?php
Your PHP Code
...
echo "Your HTML code";
...
?>
just change the extension of your file
.html/.htm
to
.php
And put your php code in this .php file in head section
I have a PHP code:
if($billing_total>$limit_to_send){
echo '<script type="text/javascript">
window.onload = function() {
alert("Sorry, you do not have enough credit");
}
</script>';
When I am printing this message, it is being printed at the beginning of the PHP page as below:
<script type="text/javascript">
window.onload = function() {
alert("Sorry, you do not have enough credit");
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-----------------------------------------------------------
This caused the header (logo) of my page in the browser to move down one line.
and the page will look very bad because all the items there will be moved down one line.
I hope it is clear to you. Please any solution ?
===========================================================================
Thanks for All ...
Solution:
$alert_message=<script type="text/javascript">
window.onload = function() {
alert("Sorry, you do not have enough credit");
}
</script>
Printing $alert_message somewhere in the HTML code before the body tag ^_^
Make sure you never output anything before the DTD (doctype declaration).
See this question for more information...
The doctype declaration must be the first element of your html page, it's from what the browser decides how to handle the rest of the html code. Outputting anything before that will probably put your browser in quirks mode so you can't be sure how the browser will render your page.
How to avoid this?
The echo command gets executed as its line is reached, and it seems that the rest of your html code follows after that.
You could either
move the html DTD and header to the top of your php (but sometimes that is not possible) OR
store the error html in a variable, so instead of echo '<script ... do $errorhtml = '<script ... and output that string, if not empty, at a specific place in the head or body generating code of your php.
If you have no control over the original source, you could consider redirecting to an error page with its own html DTD, header and body which you can design as fits you best.
Either append die() into the if codeblock or have your php print the script somewhere in the body or head.
This shows a bad design of your application. I would suggest you change it to something like:
$errors = array();
if($billing_total>$limit_to_send){
$errors[] = 'Sorry, you do not have enough credit';
}
Then on your HTML file, before the <body> tag closes, read your array and display any errors
<?php if(is_array($errors)): ?>
<script type="text/javascript">
<?php foreach($errors as $error): ?>
alert('<?php echo $error; ?>');
<?php endforeach; ?>
</script>
<?php endif; ?>