How do i do in jquery, to check if there's anything in isset SESSION user_message, each 1 seconds, and if there is then display #box else dont..
thanks
PHP file (session.php):
<?php if( isset($_SESSION) ){echo 1;}?>
JS file:
function checkSession(){
$.ajax({url: "session.php", success: function(data){
if( data == 1){
$('#box').show();
}else{
$('#box').hide();
}
}});
}
setInterval('checkSession()',1000);
This is untested, but it should do what you're looking for. Note that, typically, AJAX request expect XML or JSON, so you can reformat the session.php file the way you want.
Use PHP to echo the jQuery command:
if (isset($_SESSION['user_message'])) {
echo '<script> $("#box").… </script>';
}
This will only echo the following if $_SESSION['user_message'] is set:
<script> $("#box").… </script>
Simple as this:
<?php
if (isset($_SESSION['user_message']))
{
?>
<script>
$('#box').show(2000);
</script>
<?php
}
?>
Or you can use fadeIn or fadeOut with specified time.
<?php if (isset($_SESSION['user_message'])) { ?>
<script type="text/javasscript">
$("#box").show(1000);
</script>
<?php } ?>
Related
How do I load a script if a PHP condition is true?
For example, I got an external script called script.js and I wanna load this if it's on the product page of WooCommerce.
I've tried the following but it doesn't work because it prints the code on the page:
<?php
add_action( 'template_redirect', 'check_product_page' );
function check_product_page() {
if(is_product()) {
include ('script.js');
}
}
?>
If I write the script inside a PHP like the below, it causes a fatal error:
<?php
echo
'<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
?>
Try this:
<?php
if($condition == true){
echo '<script type="text/javascript" src="script.js"></script>';
}
?>
or:
<?php if($condition == true): ?>
<script type="text/javascript" src="script.js"></script>
<?php endif; ?>
Actually this works on my side, checkout maybe you have another issue:
echo '<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>';
It's because the single quotes are inside your string. You should actually do it the way which other people have shown, but if you need it to be an inline script you can do it like this:
<?php
//some php can go here..
?>
<script type="text/JavaScript">
window.addEventListener("load", function() {
//some code here...
})
</script>
<?php //more php can go here... ?>
I want to back my form upload page after success to upload.
alert is run well, but window location is not run.
Here my code. Thanks for help
if ($uploadKey)
{
?>
<script>
alert("Upload succesfull");
window.location ('= form_upload.php');
</script>
<?php
}
else{
?>
<script>
alert("Upload failed");
window.location('=form_upload.php');
</script>
<?php
}
Did you cared to see the syntax? It should be
window.location = "PATH_TO_REDIRECT";
Also, you are using PHP, you should be using header() instead of window.location
if ($uploadKey) {
header('Location: URL_HERE');
exit;
}
You have written wrong window.location syntax.
Please write as below:
window.location = "form_upload.php";
Instead of :
window.location ('= form_upload.php');
I have an ajax function that post to an PHP file.
Now since I'm using WordPress I can use the get_url function so I don't need to hard code the entire URL.
The WordPress function is an PHP so I'm trying to use PHP inside the ajax post. But it wont do the trick.
Any ideas ? and is it possible ?
This is what I have.
$(document).ready(function(){
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
I have also tried the php echo inside quotes like this.
$.post(' "<?php echo get_template_directory_uri(); ?>" /send.php' ....
ether way I get this path
http://mysite.com/%27%3C?php%20echo%20get_template_directory_uri();%20?%3E%27/send.php&email=&message=&name=&sent=1
Javascript to PHP = nope you cant embed javascript to php, only php can embed html and javascripts.
the better way to do it is to create a .php file and insert the javascript there...
Example: js.php
<?php
function doSubmit() {
?>
<script>
$('#submit').click(function(){
$.post('<?php echo get_template_directory_uri(); ?>/send.php', $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
</script>
<?php
}
?>
call the js.php using "include(js.php);" and call the functions inside another php
Inside your index.php
<?php
include('js.php');
?>
<html>
<head><script><?php doSubmit();?></script></head>
<body>
</body>
</html>
I am writing some jquery to call an ajax script every 2 seconds to get the result and update the page. I am mostly a backend programmer and could use some help on this.
This is the code I have now:
<script language="javascript">
function downloadProgress(id) {
$("#" + id + "").load("index.php?_controller=download&_action=getDownloadProgressAjax",
{
downloadId: id
}
);
setTimeout(downloadProgress(id), 2000);
}
</script>
<?php
foreach ($downloads as $dl) {
?>
<div id="<?php echo $dl["download_id"]; ?>">
<script language="javascript">
downloadProgress(<?php echo $dl["download_id"]; ?>);
</script>
</div>
<?php
}
?>
This does not work. What am I doing wrong or would you suggest another approach?
Thanks
I think that you are confusing your PHP script by giving it both query string variables (sent as GET) and data (which is probably getting sent as POST). Try this:
$("#" + id).load("index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id }
since you are using jquery, you can use the $.ajax function when the page is ready.
$(function () {
function function downloadProgress(id) {
$.ajax({
url: "index.php?_controller=download&_action=getDownloadProgressAjax&downloadId="+id
})
setTimeout(function () {
if (downloadnotcomplete){ // this way your script stops at some pont.
downloadProgress(id);
}
},2000);
}
});
You will attach the downloadProgress(id) function to your download button or anything else, to trigger the function the first time.
The problem you are having is that you have to provide a parameterless function and not a function call to setTimeout. Also, I would do it a little bit different and use setInterval instead of setTimeout as it relays your intention better in the code. Here is how I would do it:
<script language="javascript">
$(function() {
setInterval(downloadHandler, 2000);
});
function downloadHandler() {
// I'm not sure where the id is coming from you will probably need to put a
// class on your div's so that you can select them.
$(".MyDivClass").each(function() {
var id = $(this).attr("id");
downloadProgress(id);
});
}
function downloadProgress(id) {
$("#" + id + "").load(
"index.php?_controller=download&_action=getDownloadProgressAjax",
{ downloadId: id }
);
</script>
and then on your div:
<?php
foreach ($downloads as $dl) {
?>
<div id="<?php echo $dl["download_id"]; ?>" class="MyDivClass"/>
<?php
}
?>
Hope this helps.
Can we use the<?php ?> tag in javascript? If yes then my next question is; can we use session in this tag? Example:
success:function(data)
{
if(data)
{
<?php
if(isset($_SESSION['UserId']))
{ ?>
window.location.href="coll_delivery_det.php";
}
else
{
?> window.location.href="courier.php"; <?php
}
} ?>
}
If I understand what your looking to do you would want to use php to echo out your javascript commands.
success:function(data)
{
if(data)
{
<?php
if(isset($_SESSION['UserId']))
{
echo "window.location.href=\"coll_delivery_det.php\";";
}
else
{
echo "window.location.href=\"courier.php\";";
}
} ?>
}
Yes. But only if the page is executed as actual PHP page.
If you use PHP code through your javascript or HTML I suggest using templatish statements, like so:
<?php if ($someVariable) : ?>
var i = 0;
<?php else : ?>
var i = 2;
<?php endif; ?>
It'll be much more clear what statements are closed. Instead of the following:
<?php if ($someVariable) { ?>
var i = 0;
<?php } else { ?>
var i = 2;
<?php } ?>
In fact, you can't use PHP tags in JavaScript.
You can only generate either whole JS code or only some data for it using PHP.
The specific solutions posted here address your current situation, I'd just like to touch on the reasoning behind them.
Javascript logic is executed in your browser.
PHP logic is executed on the server.
Embedding conditional PHP statements directly in javascript won't do what you want, but you can use PHP to generate the javascript your browser needs to execute.
Yes as long as you are doing this within a file that will be executed as PHP but your code is syntactically incorrect from what I can see. Try this instead:
success:function(data) {
if(data) {
<?php if(isset($_SESSION['UserId'])) { ?>
window.location.href="coll_delivery_det.php";
<?php } else { ?>
window.location.href="courier.php";
<?php } ?>
}
}
It is worth noting that you cannot go the other way. Javascript variables cannot be used in PHP code as by the time the users browser executes the Javascript the PHP execution cycle is terminated. The only way to pass it back this way would be to make an Ajax request.
Also the PHP will only be run once each page load so using your code if $_SESSION['UserId'] is set then the users browser would just see:
success:function(data) {
if(data) {
window.location.href="coll_delivery_det.php";
}
}
Otherwise if it is not set it will just be rendered from PHP as:
success:function(data) {
if(data) {
window.location.href="courier.php";
}
}
In this way javascript is generated from your PHP code.
Yes, php can generate anything: html, css and JavaScript as well. So you can do something like that on your .php page:
function() {
<?php if($data) { ?>
window.location.href="coll_delivery_det.php";
<?php } else { ?>
window.location.href="courier.php";
<?php } ?>
}
However you need to remember that PHP is generating JavaScript as any other text, so you can't use Javascript variables in PHP script. eg. something like that will not work:
function test(){
var r=1;
<?php if ($r==1){ ?>
alert('r = 1');
<?php }?>
}
If you're using apache you can create a .htaccess file in your javascript directory with the following contents:
AddHandler php-cgi .js
This will make your .js files run as php, but retain its original extension.
the easiest way is to create a page that generates a dynamic javascript and includes the header for the javascript.
mysite.com/js.php
<?php header("Content-type: application/x-javascript");?>
success:function(data) {
if(data) {
<?php if(isset($_SESSION['UserId'])) { ?>
window.location.href="coll_delivery_det.php";
<?php } else { ?>
window.location.href="courier.php";
<?php } ?>
}
}
but you probably dont want to do that.. browsers save the included javascript files on cache, and you could have some problems with that..
the best way to proceed is to have your js files separated and and pass some parameter to the functions or classes using a <script> tag inside your <header>