0

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>
1
  • Why implode with a wacky delimiter? just do this <?php echo implode(',',$b);, and in your JS function: var parameters= Array.prototype.slice.apply(arguments,[0]);, and paramters will be an array, that looks like this: [1,2,3]. Job done Commented Nov 27, 2012 at 17:23

6 Answers 6

1

do this

$b = json_encode($a);

and look for JSON on stackoverflow or Google for how to access it.

Sign up to request clarification or add additional context in comments.

Comments

0

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'] );

1 Comment

thanks for your reply serverbloke but its not working ...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(parameter0); }
0

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

Comments

0

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];              
 }    
}

Comments

0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.