Javascript updating variable - javascript

I have used some javascript with some php. The jquery changes a php variable like it should... i echoed the new php variable and it shows, however my javascript variables don't change
<?php
$image ='<div id="myid"></div>';
?>
<script type="text/javascript" src="storescripts/jquery-1.10.2.min.js"></script>
<script>
var my_pic = new Image();
my_pic.src = '<?php echo $image; ?>';
</script>
<script language="JavaScript" type="text/javascript">
function swapContent(cv){
$("#myDiv").html("animated gif").show();
var url ="myphpscript2.php";
var id = <?php echo $id; ?>;
$.post(url,{contentVar: cv, id: id,},function(data){
$("#myDiv").html(data).show();
});
}
</script>
These are the scripts used. I have no idea why just before the php variable will change but the other will just stay as ''

Related

How to pass json_encoded variables from .php to external .js?

that's my first post :)
I have a problem with passing json_encoded variables from PHP VIEW file to an external JS. I am using FuelPHP. The following is part of the VIEW:
1. These are the PHP variables:
<?php
$sensor_id_num = $sensor->id_num;
$sensor_name = $sensor->name;
$sensor_unit = $sensor->unit;
$sensor_lati = $sensor->lati;
$sensor_longi = $sensor->longi;
?>
2. Here the variables are json_encoded and their value is given to JS vars:
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript">
var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
var sensor_name = <?php echo json_encode($sensor_name); ?>;
var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
3. The above mentioned mapmarkers.js is the external JS that I want to pass the vars to. In that JS I am using google.maps javascript API to draw a map and one marker for each map. Every marker is representing a sensor's location, so that's why I'm passing latitude and longitude. That should be part of the php VIEW. That View shows some sensor's text info along with the map.
4. So the text info and the map are visualized, but not the markers. The problem is in the JS file. When I try to use the vars from tag into the JS, the browser console shows their value is 'undefined'. I'm accessing the vars with 'window.name_of_var'. Even when I access them without 'window.' their value is not shown, "Uncaught ReferenceError: sensor_lati is not defined" is shown instead. That's part of the JS:
var myLatLng = new google.maps.LatLng(window.sensor_lati,window.sensor_longi);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: window.sensor_name,
html: window.sensor_name
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(html);
infowindow.open(map, marker);
});
Does anybody has an idea where could the problem be? I don't have much experience with FuelPHP and JavaScript. Any help would be appreciated ;)
You cannot have a <script> element with a src attribute and javascript code within it. Well, you can, but it's pretty much undefined what browsers do with that so your results will vary from browser to browser.
The solution would be to first define the variables, then include the remote script:
<script type="text/javascript">
var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
var sensor_name = <?php echo json_encode($sensor_name); ?>;
var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript"></script>
Set the variables before calling the external js file
<script type="text/javascript">
var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
var sensor_name = <?php echo json_encode($sensor_name); ?>;
var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript">
You should divide your <script /> into 2 parts.
Probably you don't need json_encode php function here. Also add wrapping doublequotes.
mapmarkers.js must be added after defining your variables
<script type="text/javascript">
window.sensor_id_num = "<?php echo $sensor_id_num; ?>";
window.sensor_name = "<?php echo $sensor_name; ?>";
window.sensor_unit = "<?php echo $sensor_unit; ?>";
window.sensor_lati = "<?php echo $sensor_lati; ?>";
window.sensor_longi = "<?php echo $sensor_longi; ?>";
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript" />

document.title funcion can't set it work with php

I have this line of code
<script type="text/javascript">
document.title = "<?php include('pot1.php');?>"
</script>
My pot1.php gives me an echo with one random number, but that code does not seem to work and I don't know why the echo doesn't appear in my title. Is anything wrong with the code?
pot1.php code:
<?php
#include_once('link1.php');
$cg = fetchinfo("value","info","name","current_game");
$cb = fetchinfo("cost","games","id",$cg);
$cb=round($cb,2);
echo '$'.$cb;
?>
This echo is working and is giving me values because I can see them in one div.
You should just do it like this:
<?php include('pot1.php');?>
Now we know that there is a variable set. Echo that into the title tag.
<script type="text/javascript">
document.title = "<? echo $cb ?>"
</script>
The way you're doing it now... I don't think you can include an entire PHP page and expect to behave like a simple string, even if that's all it returns.
Put the value you want inside a variable in the pot1.php file and use it on the title
<?php include('pot1.php');?>
<script type="text/javascript">
document.title = "<?php echo $yourVar;?>"
</script>

Using values from PHP in JavaScript

I would like to use a value from PHP in JavaScript, but it's not working. I fetch data from a database and store it in a PHP variable and for conditional formatting. I need to also use the data in JavaScript for validation.
The value required by me is $date = date('Y-m-d'); If I pass the value by json Encode the alert function is not working...
$(document).ready(function()
{
alert("Hi");
var array = php print(json_encode($date));
});
Whereas if I just alert the function without...
var array = php print(json_encode($date)); ;
...it works fine.
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("Hi");
var array = '<?php print(json_encode($date)); ?>';
});
</script>
</head>
<body>
<form action="majorprocess.php">
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("<?php echo json_encode(date('Y-m-d')); ?>");
});
</script>
What is the resulting HTML?
There could be a PHP error outputting instead of valid javascript.
Try putting the PHP section at the top maybe?
When asking a question it is always best to post any outputs you are getting so people can help.
try this one
</head>
<body>
<form action="majorprocess.php">
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
}
?>
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var array = '<?php print(json_encode($date)); ?>';
alert(array);
});
</script>
You are using the PHP $date variable before setting its value. Make sure to move the php block before you try to output the date:
<?php
$dbconn = mysql_connect('localhost', 'root','' );
if(!$dbconn)
{
die(mysql_error());
}
else
{
$date = date('Y-m-d');
}
?>
<script type="text/javascript" src="assests/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
alert("<?php echo json_encode($date) ?>");
});
</script>

How to pass var to include file and then use this var in include file using php , javascript?

How to pass var to include file (test.php) and then use this var in include file (test.php) ?
First i want to declare var in main.php and then i want to pass this var to test.php and use this var in test.php (EG: echo , if..else)
I try with php and javascript but not work both , Could you please give me some advice ?
using PHP
main.php
<?php
include('test.php');
$number = '555';
test($number);
?>
test.php
<?php
function test($numeric)
{
return $sample = $numeric;
}
echo test($number);
?>
Using JS
main.php
<?php
include('test.php');
$number = '555';
?>
<script type="text/javascript">
doCallAjax();
</script>
test.php
<script type="text/javascript">
function doCallAjax() {
var number = "<?PHP echo $number; ?>";
alert(number);
}
</script>
if you want to share your data on multiple php files, consider to use the global variable or session, so it will be something like this:
SESSION:
main.php:
$_SESSION['number'] = '555';
test.php:
echo $_SESSION['number'];
GLOBAL VARIABLES:
main.php
global $number;
$number = '555';
test.php
global $number;
echo $number;

Pass JS value in php

How can I send JavaScript variable value in php?
Like :
for(var j=0;j<count_array_item;j++){
get_item = <?php echo json_encode($cld[???]['title']); ?>; //want to get 'j' value.
}
Try this code. foreach loop store the value in js get_item array.
<script type="text/javascript" language="javascript">
var get_item = new Array();
<?php foreach($cld as $val){ ?>
get_item.push('<?php echo json_encode($val["title"]); ?>');
<?php } ?>
</script>

Categories