Solved

Passing arrays

Posted on 2000-03-01
7
288 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:maxkir
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mail Not Sent 6 41
how to us ob_start in a function 11 32
lastpass auto fill login form 5 21
Creating Image Thumbnails Using PHP 3 14
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now