Take in mind the following; the code below and programs associated at it is runs perfectly.
<script>
window.vorderby = "YEAR"
exibelivrosAJAX();
</script>
but, when I did the modify below gave me the following error: Uncaught ReferenceError: YEAR is not defined
<script>
window.vorderby = <?php echo $_POST['formorderby']; ?>;
exibelivrosAJAX();
</script>
Looking for and reading tons of messages I did the following:
<script type="text/javascript" src="funcoesJS.js">
window.vorderby = <?php echo $_POST['formorderby']; ?>;
exibelivrosAJAX();
</script>
and the error was solved. But, the function exibelivrosAJAX() don't run.
Below you can see the two pieces of code that I think can help you to understand a little better.
1st piece of code in the primary file:
echo " ";
2nd piece of code in another php file:
window.vorderby = ;
exibelivrosAJAX();
Could you help me to understand it? Thanks a lot! Marcos.
You still need the JS quotes:
<script>
window.vorderby = "<?php echo $_POST['formorderby']; ?>";
exibelivrosAJAX();
</script>
In the 2nd example, your code is not executed [and therefore you get no error] because of the src attribute of the script tag.
You forgot to enclose the outputed variable with qoutes
window.vorderby = "<?php echo $_POST['formorderby']; ?>";
Related
I have read the following posts, but they are a little over the top for me. I think I'm trying to do something fairly simple, and would like some guidance.
How to pass variables and data from PHP to JavaScript?
passing PHP variables across javascript windows.open to another PHP page
Posting a php variable to a new window
Here is the case:
I have a php script which is very simple, it calls another script and passes 2 variables:
<?php
echo '<script type="text/javascript" language="javascript">
window.open("http://callpage.com/utils/cdr.php?callernum=123456789&calltime=2017-02-22 16:24:12");
</script>';
?>
Note: This is just a "hardcoded" example.
The next script, takes those numbers and builds file/url variable.
Lets say
$file = /var/www/html/file.wav
What I'm trying to do open a new window to the effect of :
http://newpage.com/$file
I have read and found that I think the best use is Javascript, but I can't seem to get my variable into the Javascript.
Here is what I would like to get working:
<?php
$file = /var/www/html/file.wav
echo '<script type="text/javascript" language="javascript">
window.open("http://newpage.com/$file");
</script>';
?>
A few notes:
I don't want to "redirect" the old page, I want it to stay open, and the remote page isn't on the same domain
(one is a.domain.com and the other is b.domain.com).
I don't care about window sizes, etc, its a wav file that I'm expecting the browser to just play with a simple Browser default interface for Wav.
if I understood correctly what you want, you have to concatenate the string with the variable in order to be replaceed
<?php
$file = '/var/www/html/file.wav';
echo '<script type="text/javascript" language="javascript">
window.open("http://newpage.com/'.$file.'");
</script>';
?>
Use string interpolation with double quotes for the echo statement and single quotes everywhere inside the javascript:
echo "<script type='text/javascript' language='javascript'>
window.open('http://newpage.com/$file');
</script>";
The interpolated PHP variable $file should be correctly interpreted as a string and the value it holds should be displayed in the URI of your javascript.
Check out this easy to understand info about variable interpolation http://phppot.com/php/variable-interpolation-in-php/
Here is my "final" code snippet:
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
$uid = $row['uniqueid'];
foreach(glob($path. "*". $uid. "*") as $file) {
$link = "http://newpage.com$file";
echo "<script type='text/javascript' language='javascript'>
window.open('$link');
</script>";
}
}
I have the following code in a PHP page in ATOM:
<html>
<head>
<?php $testvar = "Test"; ?>
<script>
var test = <?php echo $testvar; ?>;
</script>
</head>
<body>
<p> This is a test. </p>
</body>
</html>
The page is doing exactly as planned on the PHP page but in Atom, everything after the </script> line is highlighting in red as in an error. What's going on here?
Here is a screenshot of the actual code I'm using. The above is an example but also has the same problem.
I have opened a Issue claim on the Atom support but I would like to try here as well to see if it's anything within the code.
Thanks!
Image
This is a bug in your code, not in Atom. Your echo statement produces an unenclosed string:
<script>
var test = Test;
</script>
This results in a JavaScript error ("Uncaught ReferenceError: Test is not defined") because you are telling the interpreter to use a variable named Test, but none exists.
You need to produce a properly enclosed string, which you can do with json_encode():
<script>
var test = <?php echo json_encode($testvar); ?>;
</script>
Based on your comment, you're actually trying to build an array, not just echo a string. You have this code:
var main_categories_array = [<?php echo '"'.implode('","', $main_categories_array ).'"' ?>];
If your array contains any quotation marks, like the simple "Test" example, you will get errors. So, your code should be:
var main_categories_array = <?php echo json_encode($main_categories_array); ?>;
That was an actual bug fixed in 1.13, released yesterday : 10th of January.
Sources :
https://github.com/atom/atom/issues/13532
http://blog.atom.io/2017/01/10/atom-1-13.html
This is a bug with Atom.
Posted here: https://github.com/atom/atom/issues/13532
I keep getting the above error referring to the second line of my code below and have no idea why! With the below part removed my project works ok with no problems but I keep gettig that error with it in, hence it wont run.
<?php
$myVar = $_POST['dropdown'];
?>
<script type="text/javascript">
var myVar = <?php echo '$myVar';?>
console.log(myVar);
</script>
This is probably your issue
var myVar = <?php echo '$myVar';?>
This should work, assuming you did want the single quotes around the PHP variable, and this also contains the javascript end-of-statement ; :
var myVar = <?php echo "'$myVar';";?>
If you did not want the single quotes then this should do :
var myVar = <?php echo $myVar . ';';?>
I have this command inside my javascript to create a cookie with a specific value.
...
document.cookie="superpage=John Doe;secure=true";
...
My goal: I want the content of this cookie inserted via PHP.
So I tried this: In a separate PHP file i declared
$myvalue = "superpage=John Doe;secure=true";
Then I changed the javascript cookie creation to this:
...
document.cookie= '<?php echo $myvalue; ?>';
...
Cookie is then created but with the value <?php echo $myvalue and not the string I defined via PHP. Any help is highly appreciated.
Put your JavaScript on a page with .php extension and your code will work, .i.e:
file.php
<?php
$myvalue = "superpage=John Doe;secure=true";
?>
then, on the same page, outside the php block:
<script>
document.cookie= '<?php echo $myvalue; ?>';
</script>
There seems to be confusion on how to mix php with javascript. The only way to do so would be with javascript inside a php file:
<?php
php code.....
$myvalue = "superpage=John Doe;secure=true";
echo '
<script>
javascript code....
document.cookie= '.$myvalue.'
</script>';
?>
I need to send result from my PHP file to the JavaScript function/file.
I found some of answers like: var x='<?php echo $pathinfo; ?>';
or this:
myHtmlFile.html:
<script type="text/javascript" src="jquery-1.8.1.js"></script>
<script type="text/javascript" src="newEmptyPHP.php"></script>
<script language="javascript">
function test()
{
alert(result);
}
</script>
newEmptyPHP.php:
<?php
$res="it is result";
echo "var result = ".json_encode($res).";";
?>
My question is whether there is way return value from php function and not from php file.
something like this:
<?php
function phpFunction(){
$res="it is result";
return $res;
}
?>
I need simple PHP/JavaScript code without something complex.
I want keep many function in one php file and not many php files.
<?php
function phpFunction(){
$res="it is result";
return $res;
}
?>
<script>
var result = "<?php echo phpFunction() ?>";
</script>
The above method will work. However, if you want more PHP/JS combined scripts, learn AJAX.
The best way to get your data to JavaScript from PHP is to use AJAX using jQuery you have declared.
You can use $.get()
You can call you php script which contains the function with parameters in you url request.
And when a specific parameter is specified in the url, then you can call you fonction and echo whatever you want to the javascript.
Have a good day!
You can open as many php tags as you wish inside your one php document. You can then either make this php document separate from your main document (however this would require making the main document in php and using a "php include" or "php require") or you can simply just open and close php tags as necessary. You do not even have to close the line at the end of the php, for example.
<?php if (a==a) { ?>
<div>
<script>
<?php echo 'do something here'; ?>
</script>
</div>
<?php } ?>
Using the above example can provide some unique results. Just remember that anything you do with php you can just echo out the result and slot it into the middle of your javascript.