SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Send array to another page.How?

    HI,
    I have a javascript array which i need to pass to another page and access this array using PHP code. I've written the javascript code but need your help on this to send the array to another page. Am i doing anything wrong?Any ideas?

    Code:
    function check()
    {
    
    var frm=document.forms.stream_selection
    sel_num = new Array()
    for(j=1;j<=1000;j++)
    {
      if(!frm[j].checked)
      {
          frm[0].checked=false
      }
      else
      {
    	 sel_num =frm[j].value
    	 	 
    	Parr = new Array();
    	for(i=0 ;i<sel_num.length ;i++){
    		Parr.push(escape(sel_num[i].innerHTML));
    	}
    	location.href="selected_streams.php?arr="+Parr
    	
      }
    
    	var par = new Array();
    	par = get_paragraphs();
    	
    	var parString = par.toString();
    	
    }
    
    }
    Thanks,
    Ramki.
    Last edited by ramki067; Feb 22, 2008 at 03:14.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    form array values

    If the form is the POST method, arrays like
    <input name="array_name[]" ...
    are passed to the "action" file as $_POST['array_name']

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    If the form is the POST method, arrays like
    <input name="array_name[]" ...
    are passed to the "action" file as $_POST['array_name']

    Thanks. But i'm using javascript to send an array to next page. But i'm unable to access the array in the next page. Infact, i've even given an alert just before the href line as shown below but the alert never executes. What could be the reason?

    Code:
    function check()
    {
    alert('1');
    var frm=document.forms.stream_selection
    sel_num = new Array();
    Parr = new Array();
    for(j=1;j<=500;j++)
    {
      if(!frm[j].checked)
      {
          frm[0].checked=false
      }
      else
      {
    	Parr.push(j);
      }
      
    }
    alert('3');
    ParrStr=Parr.join(','); 
    location.href="selected_streams.php?arr="+ParrStr;
    
    }
    and in retreiving form my code is :

    Code:
    $sel_numbers=$_POST['arr'];
    $sel_num=split(",", $sel_numbers);
    //$sel_num = explode(" ", $sel_numbers);
    
    $i=0;
    while ($i < count($sel_num)) 
    {
       //echo "$all_streams[$i]<br>";
       echo "$sel_num[$i]<br>";
       $i++;
    }
    Thanks,
    Ramki.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    pass javascript array

    If the alert that's inside the check() function isn't showing then the function isn't getting entered. I imagine it's getting called from the form somehow?
    Please post the pertinent form code.
    And it may help to var_dump() the POST array to see what it looks like. ie.
    PHP Code:
    var_dump($_POST['arr']);
    $sel_numbers=$_POST['arr']; 

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2007
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    If the alert that's inside the check() function isn't showing then the function isn't getting entered. I imagine it's getting called from the form somehow?
    Please post the pertinent form code.
    And it may help to var_dump() the POST array to see what it looks like. ie.
    PHP Code:
    var_dump($_POST['arr']);
    $sel_numbers=$_POST['arr']; 
    Alerts('1'), and another alert which i had placed inside the else loop also popped up, but the alert('3') didn't! I really don't know why is this happening. Can u help.

    Regards,
    Ramki.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    debug

    What browser/version are you using? Some (older) browsers don't support push().
    Try this (don't use 500)
    HTML Code:
    for(j=1;j<=5;j++)
    {
      if(!frm[j].checked)
      {
    alert("frm[" + j +"] not checked");
          frm[0].checked=false;//added missing semi-colon
      }
      else
      {
    alert("before push " + j);
    	Parr.push(j);
    alert("after " + j);
      }
      
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •