?
Solved

Passing arrays

Posted on 2000-03-01
7
Medium Priority
?
310 Views
Last Modified: 2008-03-17
How do we pass arrays via forms from one page to another using PHP??

Shireen:)
0
Comment
Question by:shireen_shabbir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:maxkir
ID: 2575502
In one form:
<INPUT type=hidden name='name[]' value="val1">
<INPUT type=hidden name='name[]' value="val2">
<INPUT type=hidden name='name[]' value="val3">

It is the 3-elements array in one form.

It is passed as $name array.
0
 
LVL 1

Accepted Solution

by:
sereda earned 150 total points
ID: 2577089
Smart-ass addition to kir's answer :)

<?
  for ($i = 0; $i < count ($name); $i++) {
?>
<input type="hidden" name="name[<?echo $i;?>]" value="<?echo $name[$i];?>">
<?
  }
?>

Or you can use cool array functions to transmit more complex array (see php reference).
0
 

Author Comment

by:shireen_shabbir
ID: 2587133
I have two forms, A and B.

In form A, I am, through, PHP creating 11 MULTIPLE select boxes and filling them with values from the MySQL database.

On submit of form A, I am checking whether the user has selected atleast one value, if not, the user cannot go ahead.

If this validation is true, the user is transported to form B where all the selected values should be displayed.

I am able to get only one selected value from each select box to form B without any code due to the submit action of the form.

How do I get multiple values to form B?

0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 2

Expert Comment

by:maxkir
ID: 2589109
I Didn't test this code. It isn't very clean.
But I hope, youl catch the idea:

<!-- FORM A -->
// here, array $values[11][n] contains values for your selects
// Now, output all out selects
for( $select=0; $select<11; $select++)
{
  print "<SELECT name='sel_name[$select][]'>";
  for($val=0; $val<count($values[$select]); $val++)
  {
      print "<OPTION>". $values[$select][$val];
  }
  print "</SELECT>";
}



<!-- FORM B -->
// this form receives data from form a as two-dimensional array
// named $sel_name[11][]
<H1>all values:</H1>
for( $select=0; $select<11; $select++)
{
  print "selector NO $select. Selected values:<P>";
  for($val=0; $val<count($sel_name[$select]); $val++)
  {
      print $sel_name[$select][$val] . "<BR>\n";
  }
}
0
 

Author Comment

by:shireen_shabbir
ID: 2594613
I still dont understand what u r trying to say so I decided that I will show u the code that I have written and let u decide what should be done:

Please copy this code to a file on ur computer. Its 129 lines long. I have included both the forms here but they probably are not good to view on browser.


<!-- Form Skills to list all the skills in the database and allow the user to choose more than one from each list. There are a total of 11 MULTIPLE selects -->
<html>
<head>
<title>Registration Process - Skills</title>
<script language="JavaScript">

<!-- This function checks whether atleast one value is chosen from the entire list and all selected values except for zero are assigned to an array. But I am not using the array anywhere since I dont know how to do so -->

function checkSkill(f)
{
      var i;
      var arrSkill=new Array;

      var isChecked;
      isChecked = false;
      
      var SkillElement = f.elements;
      for (i=0; i < SkillElement.length; i++)
      {
            if (SkillElement[i].type == "select-multiple")
            {
                  var j;
                  for (j = 0; j < SkillElement[i].options.length; j++)
                  {
                        if (SkillElement[i].options[j].selected==true)
                        {
                              if (SkillElement[i].options[j].value != "0")
                              {
                                    arrSkill[arrSkill.length] = SkillElement[i].options[j].value;
                                    isChecked=true;
                              }
                        }
                  }
            }
      }
      if (isChecked == false)
      {
            alert ('It is necessary to choose atleast one skill from the list');
            return false;
      }
      else
      {
            f.submit();
            return true;
      }
}
</script>
</head>
<body link="#000066" alink="#ffffdd" vlink="#000066">
<table width=100%>
        <tr>
                       <td bgcolor="#67A0BA" width="70%">
                        <table width=100% valign="top">
                                <tr>
                                               <td width="80%">
                                    <table width=100%>
                                    <form id="skills" method="post" action="skillsexp.php3">
                                          <tr><td>&nbsp;</td></tr>
                                          <?
                                                $cat=array("AppPack"=>"Application Packages", "Certifications"=>"Certifications", "DBTools"=>"Database Tools", "DevelopCASE"=>"Development & CASE Tools", "Hardware"=>"Hardware", "IndBus"=>"Industry & Business Expertise", "MultPub"=>"Multimedia & Publishing", "Operating"=>"Operating Systems", "Programming"=>"Programming Languages", "Standards"=>"Standards", "Networking"=>"Networking & Telecommunication");
                                                $mysql_link=mysql_connect("localhost", "root", "");
                                                mysql_select_db("Recruit",$mysql_link);
      
                                                for (reset($cat); $key=key($cat); next($cat))
                                                {
                                                      print("<tr>\n");
                                                      print("<td><font face=Verdana size=2><b>");
                                                      print("$cat[$key]");
                                                      print("</b></font></td>");
                                                      print("</tr>\n");
                                                      print("<tr>\n");
                                                      print("<td>\n");
                                                      
                                                      print("<select name=$key size=5 multiple>\n");
                                                      print("\n<option id=0 value=0>------------------------------------------------------------------</option>");

                                                      $list_query="select tblSkill.SkillID, tblSkill.CategoryID, tblSkill.SkillName, tblCategory.CategoryID, tblCategory.CategoryName FROM tblSkill, tblCategory where tblSkill.CategoryID=tblCategory.CategoryID and tblCategory.CategoryName='".$cat[$key]."'";
                                          
                                                      $mysql_result2=mysql_query($list_query, $mysql_link);
                                                      while ($row2=mysql_fetch_row($mysql_result2))
                                                      {
                                                            print("<option id=\"$key$row2[0]\" value=\"$row2[2]\">".$row2[2]."</option>\n");
                                                      }
                                                      print("</select>\n");
                                                      print("</td>\n");
                                                      print("</tr>\n");
                                                }                                          
                                          ?>
                                          <tr>
                                                <td align="center"><input type=button value="Submit" onClick="checkSkill(this.form);"></td>
                                          </tr>
                                    </form>
                                    </table>
                              </td>
                        </tr>
                  </table>
               </td>
               </tr>
</table>
</body>
</html>







<!-- Form SkillsExp for display of results -->
<table width=100%>
        <tr>
              <td bgcolor="#67A0BA" width="70%" height=250>
                        <table width=100%>
                                <tr>
                                        <td valign="top" rowspan=3 width="20%"><!-- For links --></td>
                                        <td width="80%">
                                              <?
                                          print("$AppPack"."<BR>\n");
                                          print("$Certifications"."<BR>\n");
                                          print("$DBTools"."<BR>\n");
                                          print("$DevelopCASE"."<BR>\n");
                                          print("$Hardware"."<BR>\n");
                                          print("$IndBus"."<BR>\n");
                                          print("$MultPub"."<BR>\n");
                                          print("$Operating"."<BR>\n");
                                          print("$Programming"."<BR>\n");
                                          print("$Standards"."<BR>\n");
                                          print("$Networking"."<BR>\n");
                                    ?>
                              </td>
                                </tr>
                        </table>
                </td>
              </tr>
</table>


Awaiting ur speedy reply (with a solution to my nightmare hopefully)

Shireen:)
0
 
LVL 8

Expert Comment

by:us111
ID: 2623523
I don't really understand what you want. Why do need to have
all of your selected box in form B ?? You only need to have the selected item ?
0
 

Author Comment

by:shireen_shabbir
ID: 2631920
What I have there in form B is just to check what comes on form B.

Somebody told me that when a form is submitted, php takes the name value pair of all the elements. The name converts into a PHP variable and it takes the value in it.

I did those to check what values I get. I was told that the select name gets converted into  a PHP variable and the selected items become its value. Unfortunately, it shows only the last selected box, it doesnt give multiple results.

I need only the selected items. But those selected items could be multiple that is more than one from each box.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question