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>
Related
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>
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 want to insert the jQuery plugin dropdown login form to make my website more pretty, but I am really confused where and how to insert jQuery into my code. For instance the following is my index.php:
<?php
include ('book_fns.php');
session_start();
do_html_header("Welcome to Store");
echo "<p>Please choose a category:</p>";
// Get categories out of database
$cat_array = get_categories();
// Display as links to cat pages
display_categories($cat_array);
//If logged in as admin, show add, delete, edit cat links.
if(isset($_SESSION['admin_user'])) {
display_button("admin.php", "admin-menu", "Admin Menu");
}
do_html_footer();
?>
And I want to add the jQuery plugin to the top of my index page.
How do I insert this jQuery plugin?
Get it straight from the CDN. Paste this code on your page's HEAD :
<HTML>
<HEAD>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</HEAD>
</HTML>
Then utilize the library just as you would with your usual javascript code just like this one:
<script>
$(document).ready(function() {
alert('Hey! I was called when the document finished loading.');
});
</script>
I think you should echo the tag to your index page like this:
echo "<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>"
Or you include an HTML/PHP file that contains jQuery script in the <head></head> tag
include('headScript.php');
Or you write a function that echo the <script></script> tag above:
echo $this->headScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
protected function headScript($src){
return '<script type="text/javascript" src="$src"></script>';
}
Yes, just treat the JavaScript and jQuery code as you would HTML. The server reads the PHP code and the PHP code tells the server what to send to the browser, and the browser is what reads the jQuery and HTML.
I want to trigger javascript alert using PHP.
Is it possible
I want to use it in head section, for displaying it at load time.
<head>
<?php
$valid="valid";
if(!isset($valid))
echo "<script type=\"text/javascript\"> alert('Hi');</script>";
?>
</head>
EDIT
i want to display javascript alert() at load time after checking existance of session
Of course this is possible.
All you are doing is outputting JavaScript, it's all the same thing.
The only issue is that you are nesting <script> tags, which is an HTML error, so get rid of the tags in the echo string.
<script type="text/javascript">
<?php
$valid="valid";
if(!isset($valid))
echo "alert('Hi');";
?>
</script>
By the way, as i'm sure you already know, this specific code will ALWAYS echo the "alert('Hi');"
hi i need to validate the next page before printing it ...
what i did is i used i frame in the first page and called the page i needed to print
but it fired the query in the first page which should have been fire in the second page after the submission or click of the button ...
so i need to fire the php function after the button click which calls a function in javascript how should i do this?
could anybody help me...
Okay, I am not familiar with your level of PHP knowledge, so I will start with some basics:
PHP is a server-side scripting language. It compiles in real-time when a page is requested. The server processes the HTML and PHP and serves an HTML only page to the browser. You cannot execute PHP code on the client side. There is no way to get PHP code running at the time of a button press without the use of AJAX. You could use AJAZ to make a request to the server at the press of the button and fill the iFrame with the output.
Hope that helps.
so i need to fire the php function after the button click which calls a
function in javascript how should i do
this?
I am not quite clear on why you would need to do this but here it goes...
In the button click handler you want to make an AJAX call. I use jQuery but you can use whatever framework or XMLHttpRequest function you wish.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function ButtonClickHanlder( e ) {
// Prevent the button from doing something it's normal functions(ie submit form if it is a submit)
e.preventDefault();
// Make AJAX Call
$.post("test.php", { call: "callMe" },
function(data){
alert("Data Loaded: " + data);
}
);
}
$(function(){
$('#clicker').click(ButtonClickHanlder);
});
</script>
</head>
<body>
<a id="clicker" href="#">test</a>
</body>
</html>
Reference: http://docs.jquery.com/Ajax
The test.php page
<?php
if(isset($_POST['call']) && $_POST['call'] == 'callMe') {
callMe();
}
function callMe() {
echo "I am a php function. You rang?";
}
?>