How to execute the php foreach loop in java script? i need to loop latitude_code & longitude_code in java script
this line should be in loop
var centerLatLng = new google.maps.LatLng(latitudes,longitudes);
please suggest me..
thanks for advance..
in your view;
<script>
// Your Java...
<?php foreach($items as $item) { ?>
google.maps.LatLng(<?php echo $item->latitude; ?>,<?php echo $item->longitude; ?>);
<?php } ?>
</script>
Here's [demo](http://jsfiddle.net/2crQ7/),I think it will help you, in this example java script array is used and you are using php array.Just use your array for looping, don't pass location in setMarkers(map,locations) function .
Try with this example if you don't get answer I will provide my code.
Related
Im trying to show GET variable data in a textarea. I tried may solutions in StackOverflow but none of them worked. I have no idea why. Can you please tell me if I'm missing something.
My Story
I have a link on a page and href is /index.php?page=contact-us&performance=Lamborghini
and it takes me to the Contact Us page and the url is still /index.php?page=contact-us&performance=Lamborghini which is what I want.
So, Im tyring to grab performance variables's content which is Lamborghini and print it in a textarea which is
<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32"></textarea>
So what I did is added <?php $performanceInfo= $_GET['performance']; ?> to the top of my page. Then added $("#fbrp__32").val(<?php echo $performanceInfo; ?>); in my script.js file. But it never prints Lamborghini in the textarea.
When I try $("#fbrp__32").val('<?php echo $performanceInfo; ?>'); (Notice the ' marks) it prints <?php echo $performanceInfo; ?> in the textarea.
When I <?php echo $_GET["performance"]; ?> it prints Lamborghini fine too.
Can you guys tell me why its not working for me?
NOTE : I cant edit the textarea manually as it is generated using a plugin Thanks a lot.
PHP Parse will not parse .js files, you will need to do this outside of the .js file. If not you will need to in your webserver config file set to parse .js files as PHP files.
EDIT
You mentioned that you added <?php $performanceInfo= $_GET['performance']; ?> to the top of the page and then $("#fbrp__32").val(<?php echo $performanceInfo; ?>); to your script.js file. This will not work as per default configuration script.js or any .js file will not be interpreted by the PHP parser. so your statement $("#fbrp__32").val(<?php echo $performanceInfo; ?>); will cause a syntax error.
The best way to handle it without changing server configuration would be to do this at the top of your page.
<script type='text/javascript'>
var performance = "<?php echo $_GET['performance']; ?>";
</script>
and in your script.js file do $("#fbrp__32").val(performance);
Try this
<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32">
<?php echo $_REQUEST['performance']; ?></textarea>
OR in JS
$(document).ready(function(){
var performance='<?php echo $_REQUEST["performance"]; ?>';
$("#fbrp__32").val(performance);
});
Just add the following right before the textarea (in your PHP/HTML-file, not JS-File!):
var performance = <?php echo '"' . htmlspecialchars( $_GET['performance'] ) . '"'; ?>
$("#fbrp__32").val(performance);
Just set value directly using PHP
<?php
$performanceInfo= $_GET['performance'];
?>
JQuery is a bit tricky.Instead try simple javascript for setting value:
document.getElementById("Enter Here Textarea Id").value = <?PHP echo $_GET['performance']; ?>
Ok. Finally this is the solution which worked for me.
In the php file I put
<?php
$performanceInfo= $_GET['performance'];
echo "<script>";
echo "$('#fbrp__32').val('$performanceInfo');";
echo "</script>";
?>
Its weird other solutions didnt work for me for somereason.
Try this
var value = <?php $performanceInfo= $_GET['performance']; ?>
$("#fbrp__32").val(value);
NB: php tags will not not render in js files
I have a javascript function as follows::
//THIS IS javascript.js
function dosomething(data){
//splits and do something else
}
$(document).ready(function () {
dosomething();
}
Below is a php file from search(database search with jquery and ajax)
//THIS IS mysearch.php
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$url = $row['url'];
$text = $row['text'];
$argument = $url"."$separator"."$text";
);
How do I pass $argument to javascript function? I have tried something like this.
echo '<p>'.$text.'</p>';
What would be good way to approach this?
Any help would be appreciated! Thanks in advance!!
This should do the trick:
echo '<p>'.$text.'</p>';
// Edit:
If you run into problems with special characters like öäü… you can use json_encode()
A lot of people like using json and ajax.
http://api.jquery.com/jQuery.getJSON/
http://php.net/manual/en/function.json-encode.php
I'm using google Line chart for my college project, here i want to add rows dynamically based on the user selection from database, addRow() function is used to add a row but i want it to be add by looping. someone can help me?
Here is my code:
var rowArray1 = [];
var rowArray2 = [];
<?php
for($i=1;$i<=$count;$i++)
{
$row=mysql_fetch_array($rows);
echo "rowArray1.push('". $row['a'] ."')";
echo "rowArray2.push(". $row['b'].")";
array_push($rowArray,"'".$row['a']."',".$row['b']);
}
?>
for(i=0;i<count;i++)
{
data.addRow( [rowArray1[i], rowArray2[i]] );
}
it's not working properly... :-(
Finally I got the solution. it's very simple way. when i asked this question, i was just a beginner so i dont know how to do it. now i got the answer.
I just included the PHP scripts inside the javascript code like this,
<script>
// Google chart codes....
<?php
$row=mysql_fetch_array($rows);
foreach($row as $data) {
echo "data.addRow( $data['a'] , $data['b'] );";
}
?>
// Google chart codes....
</script>
It's worked fine... :)
I have a php array called $user.
At the minute I am getting the variables across to javascript by manually adding them like so.
var username = ' . $user['username'] . ';
And so on is there a way I can make php echo a javascript array that I can access?
You're looking for json_encode
PHP:
$json_user = json_encode($user);
JavaScript:
var user = JSON.parse('<?php echo $json_user; ?>');
That's untested code but the idea behind it is sound
Have you tried json_encode ?
<?php echo json_encode ( $array ); ?>
See the documentation here.
For example I have a PHP array, such as this one
<?php $s= array('a','b','c','d','e','f') ; ?>
And I need to loop through it in JavaScript, any ideas how do I do that?
for ( i=0 ; i < <?php echo sizeof($s) ?> ; i++) {
document.write('<?php echo $s [somehow need to get the 'i' value into here] ?>');
}
Any suggestions? Thanks!
Before your ehco/print or else your php array we make sure it's in JavaScript syntax.
<?php
$s=array('a','b','c','d','e','f');
$s_to_json=json_encode((array)$s);
?>
<script type="text/javascript">
var fromPHP=<? echo $s_to_json ?>;
for (i=0; i<fromPHP.length; i++) {
yourValue=fromPHP[i];
}
</script>
<?php
$s= array('a','b','c','d','e','f') ;
?>
<?php foreach($s as $a){ ?>
document.write('<?=$a?>');
<?php } ?>
Not tested but thats one way.
Javascript and PHP cannot be combined. They are two completely different programs that communicate only vaguely. The PHP runs on the server computer and generates the HTML. The javascript runs on the client computer in the webbrowser and acts on that HTML. If you need to move information from PHP into Javscript somehow, then you have to store it in the HTML and have the Javascript access it through that HTML. If you need to do the reverse, move information from Javascript to PHP, have the Javascript call a PHP page with a query string.
One way to place the information in your array somewhere where Javascript can get to it, would be to echo it into a hidden div. Either in a series of ided spans or just a comma separated list. Then you can pull it out of the DOM.
For example:
<div style="display: none;" id="myArray">
<?php
echo '<span id="myArray.count">'.sizeof($s).'</span>';
for ($i = 0; $i < sizeof($s); $i++) {
echo '<span id="myArray.'.$i.'">'.$s[$i].'</span>';
}
?>
</div>
Then in the Javascript you can access the array in the DOM:
var myArray = new Array();
for(i = 0; i < document.getElementById('myArray.count').innerHTML; i++) {
document.write(document.getElementById('myArray.'+i).innerHTML);
}
Disclaimer: untested code, and I don't have the time to perfect it right now. If someone else wants to comment or edit to fix any errors feel free :)
Yes.... echo out your PHP array as a JavaScript array first, and then loop over that. Don't try looping over your PHP array; you can't.
<?php $product = array('1'=>'acids','2'=>'chemical','3'=>'microbilogy'); ?>
<script>
<select name="product_id" class="form-control">
<?php foreach ($product as $key => $value)
{ echo" <option value=$value[product_id]> $value['product_name']
</option>"; } ?>
</select>
</script>
if this is helpfull please give a thumbs up
Actually you can do it, if you reverse the priority from JavaScript being the base, to PHP being the loop base.
<?php
$s= array('a','b','c','d','e','f') ;
for ( $i=0 ; $i < sizeof($s); i++)
{
?>
<script type="text/javascript">
document.write('<?php echo $s[$i]?>');
</script>
<?php
}