Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 9th, 2004, 12:42 PM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing value in select box to another page

Hi,
- I want pass multiple values in select box (which is populated by another drop down box) and display them in another page in php.
- How I could keep these values, because when users want modify these values, they can come back on page 1 to add more values. Actually when I return on page 1, all values that I add in the select box are deleted.
It's urgent, I tried many times but it didn't work.
Thanks for your help.

Page 1


<input type="HIDDEN" name="art_show" action="">
<select name="artSelectionne[]" style="width:300px" size=10>
 <option value=0></option>
</select>



Page 2

$artSelectionne = array();
$artSelectionne[]=$_POST['artSelectionne[]'];
$action=$_POST['art_show'];

function show_article()
{
 Global $artSelectionne;
 $num_art=count($artSelectionne);

 for ($i = 0; $i < $num_art; $i++) {
   $show_art = $artSelectionne[$i];
   echo $show_art;
   }


}//end show_article

<select name="artSelectionne[]" style="width:300px" size=10>
<option value=0> <? show_article(); ?></option>
</select>
Bandy


 
Old February 9th, 2004, 01:55 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Ok, a number of errors here..

1.) In order to be able to select multiple items in the select box, you must include the multiple='multiple' attribute.

<select name='artSelectionne[]' style='width:300px;' size='10' multiple='multiple'>
...

2.) $artSelectionne = array(); <- this line isn't neccessary.

3.) $artSelectionne[]=$_POST['artSelectionne[]']; <- This is wrong for a couple of reasons. First, you are using the empty bracket notation to create a new indice in the $artSelectionne array then you are assigning $_POST['artSelectionne[]'] to that array, $_POST['artSelectionne'] is the correct way to reference the array, and the empty bracket notation isn't neccessary on either variable. This is a common mistake. Empty bracket notation is used to create a *new* numerical offset entry in an array. Because of the empty bracket notation in the select field's name, artSelectionne, becomes a multi-dimensional array when passed to the server.

Page 2

$action=$_POST['art_show'];

function show_article()
{
 for ($i = 0; $i < count($_POST['artSelectionne']); $i++) {
   echo $_POST['artSelectionne'][$i];
 }
}//end show_article

You are creating a lot of varibles that you don't need. The $_POST array is a superglobal array, meaning that it may be accessed in any scope, and as such, there would be no need to import it. If the variable isn't gauranteed to be called $_POST['artSelectionne'], then, IMO, it would be better to pass that variable as an argument to the function which makes your code less obfuscated.

You're placing all of the previous selections in one option?

<option value=0> <?php show_article(); ?></option>

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old February 9th, 2004, 02:45 PM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. But I don't know why it didn't work although I have changed the code.

On page 1 I have 2 drop down and one select box. The first drop down hold all the categories which is populated from data base (mysql). The second one ,which hold all articles, is populated depends on the selected value in the first one (the data is from the data base)
When user select on value in the second drop down and press button ADD, the value will be added in the select box in where users can add many values or retrieve them. For this step I did it but I could not pass all values in the select box to another page.

Could you help me?
Thanks
Bandy

 
Old February 9th, 2004, 04:29 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

It sounds like you might need some JavaScript.

Have a look at these related threads:
http://p2p.wrox.com/topic.asp?TOPIC_ID=8284
http://p2p.wrox.com/topic.asp?TOPIC_ID=8040

If you are still having trouble let us know.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old February 10th, 2004, 12:57 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you,
I will see it.
Bandy

 
Old February 11th, 2004, 04:40 PM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I have read the link that you gave me. The only thing is I don't know how to do with the select box when I add another value from drop down. Here is my code in the project.
I took a couple of java script in the Internet to do the function add article, delete article and sort, but the function sort and add didn't work correctly. Because the select box (art_selectionne[] did not do the job) and value can add many times although in the code avoid the duplicate.
Thank you very much about all your help.
Bandy

<?php
//article.php
include "inc/common_db.inc";


// Connection BD
$id_lien = db_connect("novo163");
if(!$id_lien) die(sql_error());

//request item in table categorie

$categorie = "SELECT no_categorie, categorie FROM categorie_t ORDER BY categorie";
$resultat_cat = mysql_query($categorie);
if(!$resultat_cat) error_message(sql_error());

?>

<!*************************************!>

<script language="JavaScript">
     liste=new Array()
     liste[0]=new Array("Articles","")
<?
  //**********
  //Request to display the drop down of articles
  //**********
    while ($cat_select = mysql_fetch_array($resultat_cat))
    {
     $resultat_art = "SELECT description FROM article_t WHERE no_categorie = ";
     $resultat_art .= $cat_select["no_categorie"];
     $article = mysql_query($resultat_art);
?>
     liste[<?echo $cat_select["no_categorie"];?>]=new Array(<? while ($val2 = mysql_fetch_array($article))
          echo "\"".$val2["description"]."\",";
     ?>"")
<? }
?>
   //**********
   //Change the drop down of articles when select categorie in the drop down categorie
   //**********
     function change_liste() {
      with(document.article_enregistrement){
        choix=categorie.value;
        article="<select name='articles'>";
        for(i=0;i<liste[choix].length-1;i++)
          article+="<option>"+liste[choix][i]+"</option>"
          articles.outerHTML=article+"</select>"
         }//end with
     }

//Sort items in the select box
     function sortSelect(obj) {
    var o = new Array();
    if (obj.options==null) { return; }
    for (var i=0; i<obj.options.length; i++) {
      o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
      }
    if (o.length==0) { return; }
    o = o.sort(
      function(a,b) {
        if ((a.text+"") < (b.text+"")) { return -1; }
        if ((a.text+"") > (b.text+"")) { return 1; }
        return 0;
        }
      );

    for (var i=0; i<o.length; i++) {
      obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
      }
    }



     //**********
     //Add articles in the select box
     //**********
    function ajout_article(from,to)
    {
      var options = new Object();
      for (var i=0; i<to.options.length; i++)
      {
      options[to.options[i].value] = to.options[i].text;
      }
         for (var i=0; i<from.options.length; i++)
         {
      var o = from.options[i];
      if (o.selected)
      {
        if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text)
        {
        to.options[to.options.length] = new Option( o.text, o.value, false, false);
         }
      }
       }
       if ((arguments.length>3) || (arguments[0]==true)) {
        sortSelect(to);
        }

      from.selectedIndex = -1;
      to.selectedIndex = -1;

       }

     //*********
     //Retrieve article in the select box
     //*********
       function retire_article(from)
       {
      for (var i=(from.options.length-1); i>=0; i--)
      {
        var o=from.options[i];
     if (o.selected)
     {
       from.options[i] = null;
     }
      }
    from.selectedIndex = -1;
  }
</script>

<!*************************************!>
</head>
<body>


<form method="post" name="article_enregistrement" action="confirmation.php">
<input type="HIDDEN" name="art_show">
  <tr>
    <td width="27%"><strong>Choose category</strong>
    </td>
    <td width="23%"><strong>Choose article</strong></td>
    <td> </td>

    <td><strong>Aticle added</strong></td>
  </tr>

  <tr>
    <td align="left" valign="top">
      <select name="categorie" onChange=change_liste()>
        <Option value=0>Categories</option>
        <?
          //display the drop down of category
          mysql_data_seek($resultat_cat,0); // movefirst
          while ($val = mysql_fetch_array($resultat_cat)) {
        ?>
        <option value=<?echo $val["no_categorie"];?>>
        <?
          echo $val["categorie"];?></option>
        <?
          }
          mysql_close();
        ?>
      </select>
    </td>

    <td align="left" valign="top">
      <select name="articles">
        <option>Articles</option>
      </select>


    //This is two button to add or delete articles in the select box (art_selectionne[])

            <input type="button" name="add" value="Add ->" onClick="ajout_article(document.forms[0]['articles'],document.forms[0]['artSelectionne[]'],false);return false;">


             <input type="button" name="delete" value="<- Delete " onClick= "retire_article(document.forms[0]['artSelectionne[]']); return false;">


      <select name="artSelectionne[]" style="width:300px" size=10 multiple="multiple">
        <option = 0></option>
      </select>
    </td>
  </tr>


              <input type="submit" name="submit" value=" Submit" >
              <input type="button" name="cancel" type="text" value=" Cancel " onClick="window.location='../Index.html'">

</body>
</html>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Fill select box and select recordset value markd Classic ASP Databases 1 February 20th, 2006 06:41 PM
radio button, select box, redirect page ismnanie Pro PHP 1 March 17th, 2005 08:20 AM
multicolored List Box or Select Box sasidhar79 Javascript 1 February 15th, 2005 01:47 AM
select box/List box alphabetic sort sasidhar79 Javascript How-To 3 November 10th, 2004 03:04 AM
Passing variables into the SELECT of Stored Proc kerrj SQL Language 1 October 15th, 2003 06:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.