Jump to content

passing an array to a jascript function from php


napsternapster

Recommended Posts

hi..

My aim is to pass the content of an array to javascript function from php

 

//javascript function with two  parameters

function deleteFromList(text,fullList)

{

var newList = new Array();

 

newList = fullList;

 

//alert(newList);

var i = 1;

var found = "no";

var flag = "";

 

for(i =1; i <= 2 ;i++)

{

var j = 1;

for(j = 1;j <= newList.length; j++)

{

if(newList[j] == selected[j])

{

flag = found;

}

}

 

//alert(selected);

}

if(flag != found)

{

//unSelected = selected;

unSelected.push(selected)

}

alert(unSelected);

//return deleteFromList(text,unSelected);

}

================

php and html code to pass the array to deleteFromList

for($i = 2;$i <= 10; $i++)

{

//$ct = $ct_id ;

$previous = 0;

//$ctid++;

$display_info .="<select onchange=\"showNext(this.value,'txt$ct_id"."_$i','','','$counter','$ct_id')\" 

style =\"display:none\" id=\"select_id$ct_id"."_$i"."\" name=\"select_id$ct_id"."_$i"."\" >

<option value=\"0\">-- Select one --</option>";

 

//Populates the dropdown list from the $FullListDesc and the$FullListID

//array_push($FullListID,$FullListID);

for($d = 0;$d < $counter;$d++)

{

$display_info  .= "<option value=\"$FullListID[$d]\">$FullListDesc[$d]</option><br>";

 

}

$display_info .=" </select>";

 

$next = $i + 1;

if($i > 1)

{

$previous = $next - 1;

}

$ctid++;

//the onblur function contains 4 parameters

if(($i >1 ) && ($i <= 9))

{

 

 

$display_info .= " <input type=\"text\"  id=\"txt$ct_id"."_$i"."\"

name=\"txt$ct_id"."_$i"."\" style =\"display:none\"

onblur=\"deleteFromList(this.value,'$FullListID[$ctid]'),

showNext(this.value,'select_id$ct_id"."_$next','select_id$ct_id"."_$previous','txt$ct_id"."_$i')\"

size=15 maxlength=70>   ";

echo $FullListID[$ctid];

}

else

{

$display_info .=" <input type=\"text\"  id=\"txt$ct_id"."_$i"."\"

name=\"txt$ct_id"."_$i"."\"  style =\"display:none\"

size=15 maxlength=70>";

}

 

if(($i == 4) || ($i == 7))

{

$display_info .= "<br>";

}

//echo $FullListID[$i]."<br>";

//echo $FullListID[$ctid];

}

$display_info .="</td>";

can any one assist me with regard to the above mentioned problem.its urgent by the way

Link to comment
Share on other sites

<?php

echo "<script language=\"JavaScript\">";
echo "var MENU_ITEMS = [";
       //you can get these from teh db
echo "['a', 'a.php'],";
echo "['b', 'b.php'],";
echo "['c', 'c.php'],";
echo "['d', 'd.php'],";
echo "['e', 'e.php'],";
echo "['f', 'f.php']";

echo "];";
echo "</script>";

?>

 

if u need it ASAP you can use AJAX

Link to comment
Share on other sites

hi..

My aim is to pass the content of an array to javascript function from php.The textboxes and dropdowns are created dynamically and the name starts for dropdown select_id1_1 ->select_id1_9,select_id2_1 ->select_id2_9,select_id3_1->select_id3_9 and select_id4_1 ->select_id4_9.And Textboxes starts from txt1_1 -> txt1_9,txt2_1 -> txt2_9,txt3_1 -> txt3_9,txt4_1 -> txt4_9.The name of the array to be passed to javascript function on an onblur function of the textboxes is $FullList. $FullList is an array that contains id's of contact types from a database.

 

//javascript function with two  parameters

function deleteFromList(text,fullList)

{

  var newList = new Array();

 

  newList = fullList;

 

  //alert(newList); 

  var i = 1;

  var found = "no";

  var flag = "";

 

  for(i =1; i <= 2 ;i++)

  {

      var j = 1;

      for(j = 1;j <= newList.length; j++)

      {

        if(newList[j] == selected[j])

        {

            flag = found;           

        }                 

      }

     

      //alert(selected);

  }

  if(flag != found)

  {

      //unSelected = selected;

      unSelected.push(selected)

  }

  alert(unSelected);

  //return deleteFromList(text,unSelected);

}

================

php and html code to pass the array to deleteFromList

for($i = 2;$i <= 10; $i++)

                  { 

                        //$ct = $ct_id ;

                        $previous = 0;

                        //$ctid++;

                        $display_info .="<select onchange=\"showNext(this.value,'txt$ct_id"."_$i','','','$counter','$ct_id')\"

                        style =\"display:none\" id=\"select_id$ct_id"."_$i"."\" name=\"select_id$ct_id"."_$i"."\" >

                          <option value=\"0\">-- Select one --</option>";

 

                        //Populates the dropdown list from the $FullListDesc and the$FullListID

                        //array_push($FullListID,$FullListID);

                        for($d = 0;$d < $counter;$d++)

                        {

                          $display_info  .= "<option value=\"$FullListID[$d]\">$FullListDesc[$d]</option><br>";

                 

                        }                       

                        $display_info .=" </select>";

                       

                        $next = $i + 1;

                        if($i > 1)

                        {

                          $previous = $next - 1;

                        }

                              $ctid++;                             

                        //the onblur function contains 4 parameters

                        if(($i >1 ) && ($i <= 9))

                        {

                       

                       

                              $display_info .= " <input type=\"text\"  id=\"txt$ct_id"."_$i"."\"

                              name=\"txt$ct_id"."_$i"."\" style =\"display:none\"

                              onblur=\"deleteFromList(this.value,'$FullListID[$ctid]'),

                              showNext(this.value,'select_id$ct_id"."_$next','select_id$ct_id"."_$previous','txt$ct_id"."_$i')\"

                              size=15 maxlength=70>   ";

                              echo $FullListID[$ctid];

                        }

                        else

                        {

                          $display_info .=" <input type=\"text\"  id=\"txt$ct_id"."_$i"."\"

                          name=\"txt$ct_id"."_$i"."\"  style =\"display:none\"

                            size=15 maxlength=70>";

                        }

                                   

                        if(($i == 4) || ($i == 7))

                        {

                          $display_info .= "<br>";                                                           

                        }

                        //echo $FullListID[$i]."<br>";

                        //echo $FullListID[$ctid];

                  }

                  $display_info .="</td>";

can any one assist me with regard to the above mentioned problem.its urgent by the way

 

Link to comment
Share on other sites

var http = create_request_object();

 

function create_request_object() {

if(window.XMLHttpRequest) {

// client use Firefox, Opera etc - Non Microsoft product

try {

MyHttpRequest = new XMLHttpRequest();

}

catch(e) {

MyHttpRequest = false;

}

}

else if(window.ActiveXObject) {

// client use Internet Explorer

try {

MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

}

catch(e) {

try{

MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");

}

catch(e){

MyHttpRequest = false;

}

}

}

else{

MyHttpRequest = false;

}

if(!MyHttpRequest) {

alert("cant create activex htttp request object");

}

return MyHttpRequest;

}

 

 

 

function get_array() { 

http.open("get", "script_to_get_array.php");

http.onreadystatechange = set_array;

http.send(null);

}

 

function set_array() {

if(http.readyState == 4 && http.status == 200){

var response = http.responseText;

if(response) {

the_array = eval(response);

}

}

}

 

thge script to get array

 

<?php

 

echo "[";

      //you can get these from teh db

  echo "['a', 'a.php'],";

  echo "['b', 'b.php'],";

  echo "['c', 'c.php'],";

  echo "['d', 'd.php'],";

  echo "['e', 'e.php'],";

  echo "['f', 'f.php']";

 

echo "];";

 

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.