Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a PHP variable which I am trying to pass into a javascript function. Using pure PHP I am to echo the desired string output, but when I shove it into a variable and output it in javascript it doesn't work.
It seems javascript doesn't seem to see anything at all.
Here's the code:
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
var myVar = <?php echo $b; ?>;
alert(myVar);
</script>
Whilst this is just a test piece of code I'm trying to make work, the results of which I am trying to make work with something like this:
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
$(".imgWindow").backstretch("<?php echo $b; ?>");
</script>
There's clearly some underlining principal of PHP and Javascript I must be missing.
Enlighten me please. Help appreciated.
<?php $a = get_post_thumbnail_id( $post -> ID ); ?>
<?php $img = wp_get_attachment_image_src( $a ); ?>
<?php $b = $img[0]; ?>
<script type="text/javascript">
var myVar = '<?php echo $b; ?>';
alert(myVar);
</script>
You have to wrap the value in quotes for non-integer values.
Alternatively, assign like this:
var myVar = '<?=$b;?>';
if it's a string "image name" use quotes
var myVar = '<?php echo $b; ?>';
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
two php variable in php code..
$areaid = filter_var($_GET["aid"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$areaname = filter_var($_GET["aname"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$(document).ready(function() {
$("#submit_btn").click(function() {
var uareaname = <?php echo ($areaname) ?>;
var uareaid = <?php echo ($areaid) ?>;
post_data = {'userArea':userareaname,'userAreaid':uareaid}; Not able to post this data
$.post('review_me.php', post_data, function(response){
Okay replace these lines:
var uareaname = <?php echo ($areaname) ?>;
var uareaid = <?php echo ($areaid) ?>;
With these line:
var uareaname = '<?php echo ($areaname) ?>';
var uareaid = '<?php echo ($areaid) ?>';
String values must be in quotes. Otherwise it's considered as variables.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
how do you create an if else statement that contains an include statement?
In ASP you need to have the double quotes but I am not sure how to do it in PHP.
I believe the issue lies with this:
<?php include 'i_main-nav-wohl.php' ?>
I Tried the following:
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include ''i_main-nav-wohl.php'' ?>';
} else {
echo '<?php include ''i_main-nav-wohl.php'' ?>';
}
?>
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include "'i_main-nav-wohl.php'" ?>';
} else {
echo '<?php include "'i_main-nav-wohl.php'" ?>';
}
?>
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
echo '<?php include 'i_main-nav-wohl.php' ?>';
} else {
echo '<?php include 'i_main-nav-wohl.php' ?>';
}
?>
Echoing in php means that it displays in the browser and does not implement itself. Just set the include directly in the if statement instead of echoing it.
You dont need a second php tag within your if-else-statement. Otherhwise your PHP output will result in a PHP document containing the content of your if or else branch.
Thus, if you want to produce conditional HTML or CSS use echo. Otherwhise just write your PHP commands without additional php tags.
You can do that like that :
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/occupational/wohl/") === 0) {
include 'i_main-nav-wohl.php';
} else {
include 'i_main-nav-wohl.php';
}
?>
Or even better :
<?php
$url = $_SERVER["REQUEST_URI"];
$fileToInclude = strpos($url, "/occupational/wohl/") === 0 ? 'i_main-nav-wohl.php' : 'i_main-nav-wohl.php';
include($fileToInclude);
?>
BTW you are including the same file in both cases.
You shouldn't echo php tag in php code .
If you want your php code to be executed by your server,
Instead of this code :
echo '<?php include ''i_main-nav-wohl.php'' ?>';
just do this :
include 'i_main-nav-wohl.php';
I have following script printed from PHP . If some one has a single quote in description it shows javascript error missing ; as it thinks string terminated .
print "<script type=\"text/javascript\">\n
var Obj = new Array();\n
Obj.title = '{$_REQUEST['title']}';
Obj.description = '{$_REQUEST['description']}';
</script>";
Form does a post to this page and title and description comes from textbox.Also I am unable to put double quotes around {$_REQUEST['title']} as it shows syntax error . How can I handle this ?
a more clean (and secure) way to do it (imo):
<?php
//code here
$title = addslashes(strip_tags($_REQUEST['title']));
$description = addslashes(strip_tags($_REQUEST['description']));
?>
<script type="text/javascript">
var Obj = new Array();
Obj.title = '<?php echo $title?>';
Obj.description = '<?php echo $description?>';
</script>
You also need to be careful with things like line breaks. JavaScript strings can't span over multiple lines. json_encode is the way to go. (Adding this as new answer because of code example.)
<?php
$_REQUEST = array(
'title' => 'That\'s cool',
'description' => 'That\'s "hot"
& not cool</script>'
);
?>
<script type="text/javascript">
var Obj = new Array();
Obj.title = <?php echo json_encode($_REQUEST['title'], JSON_HEX_TAG); ?>;
Obj.description = <?php echo json_encode($_REQUEST['description'], JSON_HEX_TAG); ?>;
alert(Obj.title + "\n" + Obj.description);
</script>
Edit (2016-Nov-15): Adds JSON_HEX_TAG parameter to json_encode calls. I hope this solves all issues when writing data into JavaScript within <script> elements. There are some rather annoying corner cases.
Use the string concatenation operator:
http://php.net/manual/en/language.operators.string.php
print "<script type=\"text/javascript\">\n
var Obj = new Array();\n
Obj.title = '".$_REQUEST['title']."';
Obj.description = '".$_REQUEST['description']."';
</script>";
I try to do something pretty strait... getting php value to javascript
here is the page
here is the code..
<script language="javascript">
<?php $imagepath = $_REQUEST["path"]; ?>
var whatisthepath = <?php $imagepath; ?>
alert (whatisthepath);
</script>
ALWAYS getting UNDEFINE.... why ?
--
Final wordking optimized code :
alert ("<?php echo $_REQUEST["path"]; ?>");
Missing: quotes around the var, php output to js var, semicolon after var setting:
<script language="javascript">
<?php $imagepath = $_REQUEST["path"]; ?>
var whatisthepath = "<?php echo $imagepath; ?>";
alert (whatisthepath);
</script>
All strings in JavaScript need to be surrounded with quotes. For example:
var whatisthepath = "<?php $imagepath; ?>";
The other issue is that you are not actually printing the string. All the above line of code will result in is an empty set of quotes. The correct way to do it would be to echo the imagepath
var whatisthepath = "<?php echo $imagepath; ?>"
For exactly this purpose, PHP offers the shorthand notation of <?= ... ?>. To output to value of the variable $imagepath, you can use <?= $imagepath ?>. For this to work, the short_open_path ini variable must be set to true. This may not be the default of your web server.
So, this turns the code into
<?php
ini_set('short_open_tag', TRUE);
$imagepath = SOME_VALUE;
?>
<script language="javascript">
var whatisthepath = "<?= imagepath ?>";
alert(whatisthepath);
</script>
Changing the ini value may not be convenient if it's just about a few variables, but if it happens more often in the code, I tend to find it make things more readable.
You forgot to add an echo or print statement in front of your $imagepath
<script language="javascript">
<?php $imagepath = $_REQUEST["path"]; ?>
var whatisthepath = <?php echo $imagepath; ?>
alert (whatisthepath);
</script>
PHP is a serversided language while javascript is a client side language. Which means you have to threat it the same way you would threat HTML.
For example:
<div><?php echo $content; ?></div>
Hopefully this will give you a better understanding...
You can use json_encode() to ensure that the variable is properly escaped and quoted etc. for javascript. e.g.:
<?php $imagepath = $_REQUEST["path"]; ?>
<script language="javascript">
var whatisthepath = <?php echo json_encode($imagepath); ?> ;
alert (whatisthepath);
</script>
You should use to ensure that the variable has correct JS syntax.
<script language="javascript">
<?php $imagepath = $_REQUEST["path"]; ?>
var whatisthepath = <?php echo json_encode($imagepath); ?>;
alert (whatisthepath);
</script>
Working Example:
This is almost identical to code I use in another places on my page but fails here for some reason.
<?php
//$p = "test";
?>
<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = "test";
</script>
Failing example: (alert still works)
<?php
$p = "test of the var";
?>
<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = '<?php $p; ?>';
</script>
Try
'<?php echo $p; ?>';
or
'<?= $p ?>';
Debugging 101: Start checking all variable values.
alert(parent);
alert(parent.document);
alert(parent.document.getElementById('posts'));
as well as the value rendered by: '<?php $p; ?>'
Make sure your 'posts' object (I guess it is DIV or SPAN) loads before you fill it using javascript.
You're trying to generate javascript with php, here I use a simple echo:
<?php
$p = "test of the var";
echo"
<div id='posts'></div>
<script type='text/javascript'>
var posts = document.getElementById('posts');
posts.innerHTML = '$p';
</script>
";
?>
Note the $p and that the div is printed before the javascript!
You are not outputting the variable data is why it isn't working. You need to echo or print the variable $p.
In your example the $p is being evaluated, not printed.
To print it you should use print, echo, or the syntax <\?=$p;?>. without the \