How to differentiate items from 2 menu and every submission

A

Anonymous

Guest
Please have a carefull look of my code. I am trying to differentiate the items after the final submission so that I have all items for each submission togather and those item must be identified whether it comes from top menu or the bottom menu. Please let me know if you have some idea how to do that. Thank you.



Code:
<html> 
<title> 
Menu test 
</title> 
<body> 
<script type="text/javascript"> 
 
</script> 
 
<pre> 
<?php 
extract( $_GET ); 

if( !isset($submit))// do this if no GET vars 
{ 
    print "<form method=\"GET\">"; 
    print "<p>Enter in the number of times below:</p>"; 
    print "<input type=\"text\" name=\"number\" size=\"30\">"; 
    $menu_counter = 0; 
    print "<input type='hidden' name='menu_counter' value=$menu_counter>"; 
    print "<input name=\"submit\" type=\"submit\" value=\"send\">"; 
    print "<input name=\"reset\" type=\"reset\" value=\"reset\">"; 
    print "</form>" ; 
} 
else 
{ 
    if( $submit=='submit' ) 
    { 
        $results[] = $currentSelections; 
    } 

    if( $number>0 ) 
    { 
        $months = array("January","February","March","April","May","June","July",  "August","September","October","November","December"); 

        $number = $HTTP_GET_VARS["number"]-1; 
        $menu_counter =$HTTP_GET_VARS["menu_counter"]+1; 
        print "<form   method=\"GET\">"; 
        print "Select components for term $menu_counter<br>"; 
?> 
<select size="5" name="user_choice[]" onclick="updateTextBox(this, currentSelections)"> 
<?php 

     foreach ($months as $Existing_Item) 
        { 
            print "<option value=\"$Existing_Item \">$Existing_Item</option>"; 
        } 
        print "</select>"; 
        print "<br>"; 
?> 

<select size="5" name="user_choice[]" onclick="updateTextBox(this, currentSelections)"> 
<?php 
     foreach ($months as $Existing_Item) 
     { 
         print "<option value=\"$Existing_Item \">$Existing_Item</option>"; 
     } 
     print "</select>"; 
     print "<br>"; 
?> 
<textarea name = 'currentSelections' readonly="readonly" rows = "3" cols = "20"></textarea> 
<br> 
<?php 

     print "<input type='hidden' name='number' value=$number>"; 
     print "<input type='hidden' name='menu_counter' value=$menu_counter>"; 
     print "<input name=\"submit\" type=\"submit\" value=\"submit\">"; 
     print "<input name=\"reset\" type=\"reset\" value=\"reset\">"; 

     $i=0; 
     if( isarray( $results ) ) 
     foreach( $results as $res ) 
     { 
         echo "<input type='hidden' name='results[$i]' value='$res'>"; 
         $i++; 
     } 
     print "</form>"; 
  } 
  else 
  { 
      print "<br>You choice are: <br>"; 
      $Selected_Array = $results; 
      foreach($Selected_Array as $name) 
      { 
          print "$name<BR>"; 
      } 
   } 
} 

?> 
</body> 
</html>
 
szms said:
I am trying to differentiate the items after the final submission so that I have all items for each submission togather and those item must be identified whether it comes from top menu or the bottom menu.

I'm not entirely sure what you're after, here, since phrases like "have all items together" doesn't mean anything in PHP. If you can express your wishes in terms of PHP (e.g. variables, expressions, arrays, keys, value, etc.) and HTML (e.g. <select>, <option>, <input>, <textarea>, etc.) instead of using your own vague terminology, it'll be a lot easier for the rest of us to translate into something relating to PHP.

Anyhow, I'm having a hard time trying to figure out exactly what you're trying to do, here. Whatever it is, I can't help but imagine that there's an easier way to do it. But that's your problem.

If you just want the user to be able to select multiple values from each of the <select> lists, just make the multi-selects:

Code:
<select multiple="multiple" ...>
   <option ...>...</option>
   ...
</select>

Then you can just give each <select> list a different name and skip the JavaScript nonsense (which, by the way, should go in the <head> and not the <body>). There's no sense in having several <select> lists with the same name and then trying to figure out which values came from which list later on. By the way, you can make HTML forms map to multi-dimensional arrays in PHP, too:

Code:
<select name="user_select[0][]" multiple="multiple">
   <option value="1">One</option>
   <option value="2">Two</option>
</select>

<select name="user_select[1][]" multiple="multiple">
   <option value="10">Ten</option>
   <option value="20">Twenty</option>
</select>

Now if, say, the user selects both values from both <select>s, you'll end up with a two-dimensional array called $user_select that looks like this:

Code:
array(2) {
   [0] => array(2) {
      [0] => string(1) "1"
      [1] => string(1) "2"
   }
   [1] => array(2) {
      [0] => string(2) "10"
      [1] => string(2) "20"
   }
}

And you can make them associative arrays, too, so there's lots of potential for handiness.

Lastly, and you surely don't want to hear this, but you seriously need to work on the legibility of both your code and the HTML it outputs. While I'm in no position to criticize anybody's coding style, as such, your code is so inconsistent as to render it near-illegible. It seems that sometimes you break out of PHP (?>)to output HTML (which is fine), but sometimes you do stuff like this:

Code:
<?
print "<form method="GET">\n";
print "<p>Enter in the number of times below:</p>\n";
print "<input type="text" name="number" size="30">\n";
$menu_counter = 0;
print "<input type='hidden' name='menu_counter' value=$menu_counter>\n";
print "<input name="submit" type="submit" value="send">\n";
print "<input name="reset" type="reset" value="reset">\n";
print "</form>" ;
?>

When you could (should?) be doing this:

Code:
<?
   $menu_counter = 0;
?>

<form method="GET">
   <p>Enter in the number of times below:</p>
   <input type="text" name="number" size="30">
   <input type="hidden" name="menu_counter" value="<?= $menu_counter ?>">
   <input name="submit" type="submit" value="send">
   <input name="reset" type="reset" value="reset">
</form>

Or, at the very least, you should be minimizing the number of print or echo statements (and escaped double-quotes!) that you use. You don't need a new print statement for every line of text! You can either use dot-concatenation or, if you use echo, multiple parameters (which is purported to be faster), comma-delimited:

Code:
<?
   $menu_counter = 0;

   echo  '<form method="GET">', "\n",
         '<p>Enter in the number of times below:</p>', "\n",
         '<input type="text" name=\number" size="30">', "\n",
         '<input type="hidden" name="menu_counter" value="' . $menu_counter . '">', "\n",
         '<input name="submit" type="submit" value="send">', "\n",
         '<input name="reset" type="reset" value="reset">', "\n",
         '</form>', "\n";
?>

Also inconsistent is your quote styles (sometimes you escape double-quotes, sometimes you just use single quotes -- either is fine, but pick on and stick with it). And lasty, when you're echo-ing HTML code, add a "\n" at the end of every line so that when you or someone else does a View Source in their browser, they don't have to try to pick through an entire form all of the code for which has been inexplicably printed on a single line in the source!

Alright, I'm done. I'll be impressed if you bothered to read all that, but I appreciate it.
 
Thank you for your reply. I really appriciate your suggestion regarding my code. I will try to follow the convention you talked about.

Now I want to expalin what I really want from this code. Suppose user chose 2 and then press submit. Now for first time user chose January and March from the top menu and March from the bottom menu and hit submit. Now in second time user select April from Bootm Menu and hit submit. After getting the output items from all submission I want to store those in a MYSQL Database. The order of the selected items for each submission is also important.

Expected output:

First time: January(1) from Top Menu.
March(2) from Top Menu.
March(3) From Bottom Menu.

Second Time: April(1) from Bottom Menu

Hopefully you understand. Thank you.
 
So, your problem is that you're having trouble carrying the data from the first page to the second to the third, etc., and remembering which data came from which list on which page?

Well, I already explained how you can do multi-selects, so that solves the problem on a single page. To keep the values from different pages straight, though, is a little trickier. What I'd recommend doing, I think, is using serialize() to dump the values from the first page (the $user_select array) into a string, and then pass the string to subsequent pages in a hidden input field (<input type="hidden">). Then, when you get to the final page, you can unserialize() all of the serialized arrays. I'd actually recommend using a multi-dimensional array so that you can keep them all together, e.g.:

Code:
// The below isn't valid PHP code, nor is it intended to be.

array { 
   // first page
   [0] => array { 
      // first <select> on first page
      [0] => array { 
         [0] => "Vermont" 
         [1] => "West Virginia" 
      }
      // second <select> on first page
      [1] => array { 
         [0] => "Iowa" 
         [1] => "Illinois" 
      } 
   }

   // second page
   [1] => array {
      // first <select> on second page
      [0] => array {
         [0] => "China"
         [1] => "Venezuela"
      }
   }
}

.. And so on. It's a little hard to wrap your brain around, but it's the best way I can think of to do this. But while you're at it, I might recommend that you Google a few tutorials on multi-page forms.
 
Thank you for your reply. I would like to inform you that I have to use JavaScript for showing those selected data for each mouse click and at the same time I want to keep track of the selected item's order (sequence of mouse clock) and from which menu it is being selected. Hopefully you understand my goal. Thank you.
 
Ah. Well, that's certainly tricky. If you're really have to use JavaScript, then, instead of using a textarea, I'd recommend you use another <select> box instead of a <textarea>. Then whenever the user clicks on an item in one of the <select> boxes, you can just add the item to the final <select> box. This way, you can use the value attribute to store infomation about which <select> each item came from, and the order in which they were added. This JavaScript is a little more advanced, and unfortunately, JavaScript isn't my area of expertise, but it shouldn't be much more difficult. When the user clicks on the Submit button, you're probably going to need to use JavaScript again so that all of the items in the third box are selected before the submit.
 
Thank you for your reply. Little bit of code might help. Thank you.
 
Like I said, JavaScript isn't my area of expertise. You might try asking in the JavaScript forum, or on some other JavaScript-centric message board. A quick Google search might help, too. I managed to find this page: http://tutorials.beginners.co.uk/read/id/68
 
Back
Top