Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

Passing arrays

How do we pass arrays via forms from one page to another using PHP??

Shireen:)
0
shireen_shabbir
Asked:
shireen_shabbir
1 Solution
 
maxkirCommented:
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
 
seredaCommented:
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
 
shireen_shabbirAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
maxkirCommented:
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
 
shireen_shabbirAuthor Commented:
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
 
us111Commented:
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
 
shireen_shabbirAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now