Hello People please find my code below as you can see iam trying to pass php array values to js function if i run this scipt i get 3 alerts parameter0=1,parameter1=2 and parameter2=3 differently...what iam trying to do is i need to save parameter0,parameter1 and parameter3 so that i can pass it one more function....please help me to do this.....
<script type="text/javascript">
function mufunc(a)
{
var temp = new Array();
dump(temp);
temp = a.split('~');
var parameter;
for(i=0;i<temp.length;i++)
{
alert('parameter'+i+'='+temp[i]);
}
//alert('sri'+i+'='+temp[i]);
}
</script>
<?php
$a = array('1','2','3');
$b = implode("~",$a);
?>
<a href="javascript:void(0)" onClick="mufunc('<?php echo $b; ?>')">Click Here</a>
You can declare temp
in the global scope, so that it can be used later in any scope:
var temp = new Array();
function mufunc(a)
{
dump(temp);
temp = a.split('~');
var parameter;
for(i=0;i<temp.length;i++)
{
alert('parameter'+i+'='+temp[i]);
}
//alert('sri'+i+'='+temp[i]);
}
Alternatively:
var params = new Array();
function mufunc(a)
{
var temp= new Array();
dump(temp);
temp = a.split('~');
var parameter;
for(i=0;i<temp.length;i++)
{
alert('parameter'+i+'='+temp[i]);
params['parameter'+i] = temp[i];
}
}
Now you can access it like:
alert( params['parameter0'] );
alert( params['parameter1'] );
Its working as your code is writen.
Your counts are different, one starts at 0, one starts at 1:
Try changing your start variable in the for loop to 1 and then minus 1 off the i in temp, you should get:
parameter1=1,parameter2=2 and parameter3=3
Just use a global array variable, and assign parameters to that.
var parameter = [];
function mufunc(a)
{
var temp = new Array();
dump(temp);
temp = a.split('~');
for(i=0;i<temp.length;i++)
{
alert('parameter'+i+'='+temp[i]);
parameter[i] = temp[i];
}
}
Take advantage of PHP's json_encode function on this one.
<script type="text/javascript">
function mufunc(a)
{
// removed "var" to make "temp" global.
// and we don't have to parse anything, the JS engine did
// it for us already!
temp = new Array();
for(var i=0; i < temp.length; i++)
{
alert(i + ": " temp[i]);
}
}
</script>
<?php
$a = array('1','2','3');
$object_string = json_ecode($a);
?>
<a href="javascript:void(0)" onClick="mufunc(<?php print $object_string; ?>)">Click Here</a>
Since my comment could be considered an answer, I'll (re) post it here:
In your php script, just pass the arguments like so:
onclick="<?php echo implode(',',$b);?>">
//or if the you're dealing with strings:
onclick="<?php echo '"'.implode('","',$b).'"';?>">
And then do this in myfunc
function myfunc()
{
var parameters = Array.prototype.slice.apply(arguments,[0]);
}
Arguments are available in all functions, and contain all data that was passed to that function as an argument, weather the function was defined with a certain number of arguments or not. Read all about it on MDN
The arguments
object looks like an array, but isn't one, that's why -to slice it- you need to apply
the slice method from the Array
prototype. MDN - Apply for more info.
All in all, that's the quickest way to get to what you need, IMHO
BTW: Don't use the array constructor, use the array literal notation: []
. The array constructor might behave a little odd from time to time
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.