I am trying to use a Flash detection script to assess whether Flash Plugin is enabled in the user browser so that a different page loads. The Flash detection script is as follows, using jquery 1.8.2 and jquery.jqplugin 1.0.2
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="jquery.jqplugin.1.0.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#withflash").hide();
$("#noflash").hide();
if ($.browser.flash == true)
$("#withflash").show ();
else
$("#noflash").show ();
});
</script>
<div id="withflash">Flash Supported</div>
<div id="noflash">Flash Not Supported</div>
I get the display that "Flash Supported" if Flash Plugin is present.. I need to capture the value whether flash plugin value is true in a php variable $hasFlashSupport as below:
<?php
echo " $hasFlashSupport";
exit;
?>
I am aware that PHP is server based and Javascript is client based.. Hence Ajax would be a nice option to capture the javascript variable to my php variable. I am totally ignorant about Ajax syntax and how to achieve it.
Request the experts here to help me out with the code on how this can be achieved...
Thanking all of you in advance..
You can do that using DOMDocument object that have a getElementById method.
Try this:
if true
<div id="withflash">
<?php echo '$hasFlashSupport'; ?>
</div>
else
<div id="noflash">
<?php echo '$hasFlashSupport'; ?>
</div>
Related
I have a problam that I dynalically add a class to div through php, and then checking if id "checkLang" has spacific class, but it desn't work :(
Maybe someone knows the problem?
My html:
<div id="checkLang" class ="<?php echo $valoda ?>"><div>
and that div I check with
if ($('#checkLang').hasClass('RU')){
<?php include("lan/ru.php"); ?>
};
if ($('#checkLang').hasClass('LV')){
<?php include("lan/lv.php"); ?>
};
I don't know why, but both ifs include language php file.
But maybe the reason is because this script is in php file with header <?php header('Content-type: text/javascript'); ?> I attach file like javascript file in index page like <script type="text/javascript" src="php/FormuValidacija.php" /></script>
I tried
if($_GET['lang']=="latviesu"){
include("php/lan/lv.php");
}
else($_GET['lang']=="krievu"){
include("php/lan/ru.php");
}
But doesn't work as well :(
P.S. Sorry if this is stupid question, i'm new whit this stuff, but willing to learn! :)
Looks like your script is not added in a dom ready handler like
jQuery(function($){
alert($('#checkLang').length)//it should alert 1 not 0
if ($('#checkLang').hasClass('RU')){
<?php include("lan/ru.php"); ?>
};
})
Current language:
<div id="checkLang" class ="<?php echo $valoda ?>"><div>
Including your localization files:
<div class="ru" style="display:none;">
<?php include("lan/ru.php"); ?>
</div>
<div class="lv" style="display:none;">
<?php include("lan/lv.php"); ?>
</div>
Getting current language and showing corresponding div
<script type="text/javascript">
$(document).ready(function(){
var currLng = $('#checkLang').attr('class'); // get current language, i.e class of #checkLang
$('div.' + currLng).show(); // show div with corresponding class, i.e lang
});
</script>
However, its better to get language at server side and not to load unused files to client(i.e HTML)
P.S un labāk visus variablus, script failus etc saukt anglū valodā :)
Given my jsfiddle example here, you can clearly see that javascript is able to check that the given class is set on the div.
http://jsfiddle.net/n5e9a/
Both of the php files will be rendered no matter what, because the if check here is done on the client side javascript. if you wish to only render data from the php files once the check has been done on the client side. Then you have to fetch the data from the client side instead of serving it from the server side.
One way of doing this is through ajax. for example. you could create a php script that returns html contents based on a query, something like:
checklang.php?lang=RU
and in the javascript code you would have a request set up like this:
$.get('checklang.php?lang=RU', function(data) { //switch RU with a js variable so you can change between RU and LV programatically
$('#some-content-div').html(data);
});
it includes your content because its generated server side already, so when the html site is transfered the php script is already rendnered while the javascript is handled client side, you would have need to adapt your try
you see this would work
<div id="checkLang" class ="<?php echo $valoda ?>"><div>
<div class="ru" style="display:none;">
<?php include("lan/ru.php"); ?>
</div>
<div class="lv" style="display:none;">
<?php include("lan/lv.php"); ?>
</div>
$(document).ready(function()
{
if ($('#checkLang').hasClass('RU')){
$('.ru').show();
}
if ($('#checkLang').hasClass('LV')){
$('.lv').show();
};
});
example here:
http://jsfiddle.net/yVpf4/
tough it would rendner all language content; you should consider doing the language selection within the php code to avoid extra traffic unless you want to switch language via javascript only without any server interaction.
regards jan
also as Arun P Johny stated
...your script is not added in a dom ready handler...
regards jan
so I have this in my HTML file
<script type="text/javascript" src="load.js"> </script>
<script type="text/javascript" src="load2.php"> </script>
Saw somewhere you could call an external php file using the js script tag and whatever js in the php file would be rendered first.
But my problem is, I can't assign no JS variable from load.js to a php variable in load2.php but, however, I could alert using echo "alert(js variable);"; So that tells me it finds the variable and I could use it, but I can't assign it to a php variable :/ any help please?
When working between JavaScript and php you must remember one is client side (JavaScript) and the other server side (php). Therefore php can insert values into javascript before it is sent to the client and then rendered (processed). Although the reverse cannot happen as the "scope" of JavaScript is on the clients computer.
The way around this is to use AJAX requests from the client back to the server sending back the data / variable values you need.
JQuery provides an easy to use library with AJAX functionality.
Please see http://api.jquery.com/jQuery.ajax/
PHP is server-side, JS is client side. This means the PHP will run far before the JS ever gets assigned. The only way a PHP file is going to get a JS variable is if you post it using Ajax
One option would be to dynamically load the PHP-generated script and pass some GET parameters to the PHP. This would still require ajax, but could be done cleanly with jQuery. If you go with jQuery, this is the documentation for getScript.
EDIT
So, your HTML file should look like this:
<!-- your first javascript file -->
<script type="text/javascript" src="load.js"></script>
<!-- load jQuery library -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript>
// get the variable from your first javascript file
// via a getter function or global variable
var x = getVarFromFirstFile();
// this will load your second 'javascript' (generated by php) file
// and will pass the variable 'x' as a GET parameter to the php
$.getScript("load2.php?x="+x);
</script>
Then the php in load2.php could access the variable x (originally from load.js) like this:
<?php
$x = $_GET['x'];
...
I am trying to share data on same .php page. I receive some data on a page via POST method and I want to use it on javascript on that page during page load. But somehow javascript is showing the value undefined. What's the reason and how do I fix it?
<?php
echo "<label id='origin' style='visibility:hidden;'>".$_POST["startStation"]."</label>";
?>
<script type="text/javascript">
alert(document.getElementById('origin').value);
</script>
Because label does not have a .value. To access that data you need to use .innerHTML.
<?php echo "<label id='origin' style='visibility:hidden;'>".$_POST["startStation"]."</label>"; ?>
<script type="text/javascript"> alert(document.getElementById('origin').innerHTML); </script>
I am developing a small drawing tool which collects some data from the user before starting the tool. I'm using Symfony2 - the drawing tool is pure javascript.
So once the user reaches the page where the tool loads, javascript needs to know about some of the data the user has entered previously (for example height & width of the canvas), which have been sent to the controller by forms. I have no problem using the variables in the templates, but can't access them with the javascript files.
Is there a way to set javascript variables in a template?
I've included the javascript file in the template like so (which works):
<script type="text/javascript" src="<?php echo $view['assets']->getUrl('bundles/mybundle/js/myjs.js') ?></script>
Just put some script tags in your template and output the variable into them using PHP. It could look similar to this:
<script type="text/javascript">
var canvasHeight = <?php echo $canvasHeight; ?>;
var canvasWidth = <?php echo $canvasWidth; ?>;
</script>
Can't you just print the variable with php in the page? something like
<script type="text/javascript">
var t=<?php echo $var; ?>
</script>
i need to have some php code inside javascript
<script ...>
<?php
echo " ... ";
?>
</script>
but this doesnt work. how can u implement php inside javascript that is in a own file javascript.php?
That doesn't do what you probably think it does. It'll work, but the PHP gets run once, when the page is loaded, not every time the JavaScript function is called.
Just for clarification, this is what will happen
index.php
<script type="text/javascript">
<?php echo "alert('hello!');"; ?>
</script>
output html in browser
<script type="text/javascript">
alert('hello!');
</script>
If that is what you want to do, then you can output all the javascript you like. What you cannot do is execute PHP code in the user's browser.
your can use php to dynamically generate javascript code, but you cannot execute php client side. If you need to execute php you will need to postback or use AJAX
There seems to be a good bit of misunderstanding of the question... Here is what you want to do to generate JS from PHP on the server:
file javascript.js.php
<?php
header('Content-Type: text/javascript');
?>
// javascript code here
function PrintTime()
{
alert("The time is " + <?php echo json_encode(time()); ?>);
}
Now, include it on the HTML page using normal script tags:
<script type="text/javascript" src="/url/to/javascript.js.php"></script>
The server will process the PHP file, and return javascript from it.
You can't run PHP inside a javascript file. Primarily because PHP runs server side and is processed before the client is sent any actual http info. JavaScript is processed by the browser on the client side and is sent as text.
It looks like you want to pass some kind of dynamic info to the JavaScript. You can do this by passing a variable like this:
<?php $variable="its me"; ?>
<script>
alert('<?php print($variable)?>')
</script>
The output passed to the client is:
<script>
alert('its me')
</script>
What are you trying to accomplish and maybe we can help you come up with a better solution?