Solved

Passing arrays

Posted on 2000-03-01
7
291 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
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 50 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 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