Declare variable in php file that will access in JS/jQuery - php

How can I declare a global variable in a PHP file and access directly in jQuery?
HTML
<script type="text/javascript">
var date_selected = <?php echo $from; ?>;
</script>
JS
$(function() {
alert("dsdsadasdas" + date_selected);
var date = date_selected;
var startDate;
var endDate;
});
Because on my test its not working.

You are missing the quotes
var date_selected = '<?php echo $a; ?>';

Related

Ajax .load parsing php variable

How do I parse a php variable through .load in ajax?
Assuming I want to parse $x through .load so it can be used by the php file being loaded ?
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php", "$x");
</script>
Do you want get the variable $x from a PHP page? If yes the best way is:
on PHP:
<?php
$x = "Hello World!";
print '{"phpvarx" : "'.$x.'"}';
?>
on jQuery Sender page:
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php", function(data){
var myData = $.parseJSON(data);
var varx = myData.phpvarx;
alert(varx);
});
</script>
If you are trying to send the variable $X to a php page use it:
on PHP page:
<?php
$x = $_GET["x"];
echo $x;
?>
on jQuery Sender page:
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php?x=<?php echo $x; ?>");
</script>
you must have the values of $baseUrl and $x. PHP executes on server side and then shows the page.
Probably should work like this:
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php", "<?php echo $x; ?>");
</script>

Use data from view in javascript

I was wondering if it's possible to use data from the view in my javascript code? This is what I have:
View "index.phtml":
<?php $event = $this->event; ?>
<script>
// HERE I WANT TO USE $event
var data = <?=$event?>;
</script>
But that doesn't work!
If it contain a string value:
<script>
var data = '<?php echo $event; ?>';
</script>
If it contain a numeric value:
<script>
var data = <?php echo $event; ?>;
</script>
Also php short tag <?= should be On in php.ini to use it.

Use a PHP variable inside a jquery click()?

I have this,
$(document).ready(function(){
$('#link').click(function(){
var user_login = <?php $base_url; ?>;
window.location = user_login + '/login';
});
});
note that $base_url is global variable. This works in FF but not on Chrome and IE. Thanks.
You need to add quotes around the php tags to designate that the value of $base_url is a string:
$(document).ready(function(){
$('#link').click(function(){
var user_login = "<?php echo $base_url; ?>";
window.location = user_login + '/login';
});
});
That way, when the browser gets this block, it will look something like:
$(document).ready(function(){
$('#link').click(function(){
var user_login = "http://www.example.com"; // after php is executed
window.location = user_login + '/login';
});
});
You need to echo the variable, not just simply have it in a code block. And for safety, to prevent introducing JS syntax errors, you should json-encode the value:
var login = <?php echo json_encode($base_url); ?>;
See below, note the quotes around the PHP echo
$(document).ready(function(){
$('#link').click(function(){
var login = '<?php echo $base_url; ?>';
window.location = user_login + '/login';
});
});
I suppose its a PHP file, you need to surround your variable contents by quotes to javascript
$(document).ready(function(){
$('#link').click(function(){
var login = <?php '\''.$base_url.'\''; ?>;
window.location = user_login + '/login';
});
});
Run this script in a PHP file, not in a .js file, and don't forget tho ECHO your variable and add quotes (in javascript), as said in other answers !

Case, when "document.ready" obstruct

I have php file, in this file I have this code:
<script language="JavaScript" type="text/javascript" src="jquery.js"></script>
<script language="JavaScript">
$(document).ready( function () {
var myvar = <?php echo json_encode($myvar); ?> ;
});
</script>
<script language="JavaScript" type="text/javascript" src="costum.js"> </script>
and in costum.js file I have code:
$(document).ready( function () {
alert(myvar );
});
this not working, error consol returns "myvar is undefined"
if in php file I write this (that is, without "document.ready")
<script language="JavaScript">
var myvar = <?php echo json_encode($myvar); ?> ;
</script>
in costum.js file, code alredy is working. Please tell why this happened?
try with
<script>
var myvar;
$(document).ready( function () {
myvar = <?php echo json_encode($myvar); ?> ;
});
</script>
your variable has to be declared as global (or in other words, in the outer scope) to be viewed from both document.ready functions.
As a side note language attribute is not necessary. Even type is not necessary (if you're using html5 doctype)
Your myvar is in the local scope of the ready-function. Move the var declaration outside to make it global and available to the other script.
However, as you just assign to a variable, you won't need to wait for DOMready anyway. Just use
<script type="text/javascript">
var myvar = <?php echo json_encode($myvar); ?>;
</script>
BTW, the language attribute is deprecated.
local variable inside the function is only visible in the function scope.
when you declare variable in the global scope, then it is the global variable.
You could expose it to global scope by:
$(document).ready( function () {
var myvar = <?php echo json_encode($myvar); ?>;
window['myvar'] = myvar;
});

Can I convert a php $_POST variable into a jquery variable?

I want to know can I convert a php $_POST variable into a jquery variable. I have php variable below:
<?php $postduration = $_POST['durationChosen']; ?>
Can I convert the php variable above into a jquery variable?
Thanks
Yep, of course!
Just write/output your PHP code right into your JavaScript ... block.
Kinda like this:
<script>
var myVar = <? echo $postduration; ?>;
</script>
var durationChosen = "<?php echo $_POST['durationChosen']?>";
<script type="text/javascript">
var postDuration = '<?php echo $_POST['durationChosen']; ?>';
</script>
Just echo it into the Javascript on the page, just like any other dynamic content.
It is simple as...
var myVar = '<? echo $postduration; ?>';
You can convert it into a javascript variable (jquery framework helps with DOM manipulation)
<?php $postduration = $_POST['durationChosen']; ?>
var myVar = '<?php echo $postduration ?>';

Categories