This question already has answers here:
Best way to pass parameters to jQuery's .load()
(2 answers)
Closed 10 months ago.
i would like to send a variable on the next page this is what i have at the moment
<? php
//some code
$ID = $_GET['ID'];
echo "<div id='show_here'></div>";
<script type ="text/javascript">
$(document).ready(function(){
setInterval(function(){
$('#show_here').load('fetch.php')
}, 3000);
});
</script>
so i would like to send the variable $ID in to the next page i assume it will be something like this .load(fetch.php?=id=$id') but it doesn't work can some one please help me out here
i think the first think is to define the variable id in the jQuery part but i do not know how to go about it please help
If I were you, I'd clean that code up properly before anything else.
<? php
//some code
$ID = $_GET['ID'];
?>
<div id='show_here'></div>
<script type ="text/javascript">
$(document).ready(function(){
setInterval(function(){
$('#show_here').load('fetch.php?id=<?php echo $ID; ?>') // we put the ID here
}, 3000);
});
</script>
In your fetch.php file, you'll need to access the $ID via $_GET again.
I'm sticking within the scope of your question here, but something to note is that you best sanitize that information. Especially if you're going to use it in a database query!
you can use
.load('fetch.php?id=<?php echo $id ?>');
or
.load('fetch.php' , {id : '<?php echo $id ?>'}) ;
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>";
}
}
This question already has answers here:
Pass a return value from php to js
(1 answer)
How to access php session in javascript file?
(4 answers)
Closed 6 years ago.
I'm trying to create an image uploading website. In this, when a user logs in, I set a $_SESSION['id'] variable in php. Now, I wish to check if $_SESSION['id'] is set in my javascript file (general.js) in order to carry out certain functions. How should I go about doing it?
<?php $session_value=(isset($_SESSION['id']))?$_SESSION['id']:''; ?>
<html>
<head>
<script type="text/javascript">
var myvar='<?php echo $session_value;?>';
</script>
<script type="text/javascript" src="general.js"></script>
</head>
<body>
</body>
</html>
In above code snippet I have initialized global variable myvar with value stored into php session variable into script file before general.js file is referenced in script.It will be accessible in general.js file
A simple example please try this
<?php
session_start();
$_SESSION['id'] = 12;
?>
<script>
alert(<?php echo $_SESSION['id']; ?>);
</script>
You can even use php variable and array as variable in js like below
<?php
$id= $_SESSION['id'];
$data= array('one', 'two', 'three');
?>
<script type="text/javascript">
var idr = '<?php echo $id; ?>';
var datar = <?php echo json_encode($data); ?>;
</script>
Here PHP is server-Side execution and JavaScript is Client side execution. and $_SESSION is a server-side construct.So probably you can not access that directly using JavaScript You would need to store that variable in $_COOKIE to be able to access it client-side.
You can get that Session using PHP, and then use that for the JavaScript like this
<?php
$variablephp = $_SESSION['category'];
?>
<script>
var variablejs = "<?php echo $variablephp; ?>" ;
alert("category = " + variablejs);
</script>
Here you are getting Session like echo $variablephp using PHP, and the assign that value to JavaScript variable.
The best way is to echo the session ID in a javascript variable.
<script>
var sessionId = "<?php echo $_SESSION['id']; ?>";
// Your javascript code goes here
</script>
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>';
?>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I've been trying to find out the response for this because I think it's not difficult to do, but I cannot find it.
I basically want to "recall" a mysql query that works. I have an HTML button and a span so I call a function in javascript which has a php code with the Mysql query and the result that is store in my span, works fine, but... Why I cannot click the button again to get the update results from Mysql table if I changed it ?
The syntax is something like ( in a single php file for test):
<script type="text/javascript">
function test(){
<?php include 'dbconnect.php';
$query = 'SELECT * FROM data';
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) {
$data = $data . "row[name]<br>";
}
mysql_close();
?>
testspan.innerHTML = <?php echo $data ?>';
}
</script>
<HTML><BODY>
<input type='button' onclick='test();'/><br>
<span id="testspan"></span
</BODY></HTML>
Resuming: when I click the first time the data appears but not the second time onwards so the Mysql query can be call just first time? anything related with the variables ?
Php is ServerSide... JS is ClientSide... you can't do code php in code JS you must use AJAX :https://api.jquery.com/jQuery.ajax/
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.