napsternapster Posted February 4, 2009 Share Posted February 4, 2009 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 https://forums.phpfreaks.com/topic/143752-passing-an-array-to-a-jascript-function-from-php/ Share on other sites More sharing options...
nadeemshafi9 Posted February 4, 2009 Share Posted February 4, 2009 <?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 https://forums.phpfreaks.com/topic/143752-passing-an-array-to-a-jascript-function-from-php/#findComment-754241 Share on other sites More sharing options...
napsternapster Posted February 4, 2009 Author Share Posted February 4, 2009 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 https://forums.phpfreaks.com/topic/143752-passing-an-array-to-a-jascript-function-from-php/#findComment-754274 Share on other sites More sharing options...
nadeemshafi9 Posted February 4, 2009 Share Posted February 4, 2009 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 https://forums.phpfreaks.com/topic/143752-passing-an-array-to-a-jascript-function-from-php/#findComment-754281 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.