Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

issue with Passing PHP String array to Javascript Function

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

issue with Passing PHP String array to Javascript Function

Postby stephenrodrics » Thu Apr 25, 2013 4:25 am

I have a issue in passing PHP array to Javascript function.

where as when I pass only normal array with Integer it works, the issue is only with String array


<script type="text/javascript">
function Submittedone(var1,var2)
{
alert(var1);
};
</script>

<?php
$json_monday = "SS|test|test|test|test|test|test|test|test|test|test";
$i = 10;


print "\t <input type ='Button' id = 'Approve' name = 'Approve' title = 'ss' onclick='Submittedone(

$json_monday , $json_monday )'>Preview \t \n";


print "\t <input type ='Button' id = 'Approve' name = 'Approve' title = 'ss' onclick='Submittedone(

$i , $i )'>Preview \t \n";


?>

When I pass $json_monday it does not work

when i pass $i it works

even when I pass week array like $json_week[0]; it works, but it does not work when I pass string array, is there any way to pass string array to Javascript function.

I also used



Submittedone(json_encode($json_week) , json_encode($json_monday) ) still it fails.

Let me know if someone can help me.

Thanks
stephenrodrics
New php-forum User
New php-forum User
 
Posts: 58
Joined: Thu Mar 14, 2013 6:10 am

Re: issue with Passing PHP String array to Javascript Functi

Postby johnj » Thu Apr 25, 2013 4:44 am

Make the array variable like this,
$json_monday = array('SS','test1','test2'); then, proceed with your manipulation.
if you want a full example, take a look at this blog, http://phillipnb.wordpress.com/2012/12/ ... es-part-1/
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: issue with Passing PHP String array to Javascript Functi

Postby swarups » Thu Apr 25, 2013 5:36 am

Hi,

Your code should look like this:
Code: Select all
<script type="text/javascript">
function Submittedone(var1,var2)
{
alert(var1);
};
</script>

<?php
$json_monday = '"'."SS|test|test|test|test|test|test|test|test|test|test".'"';
$i = 10;


print "\t <input type ='Button' id = 'Approve' name = 'Approve' title = 'ss' onclick = 'Submittedone($json_monday , $json_monday)'>Preview \t \n";


print "\t <input type ='Button' id = 'Approve' name = 'Approve' title = 'ss' onclick='Submittedone(

$i , $i )'>Preview \t \n";


?>


The string should pass with quotes. Here it is difficult to add quotes with parameter. So I have added the quote to the string at initialization.

Thanks
Swarup
swarups
New php-forum User
New php-forum User
 
Posts: 20
Joined: Tue Mar 06, 2012 11:05 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.