www.webdeveloper.com
Results 1 to 4 of 4

Thread: passing many select values

  1. #1
    Join Date
    Jan 2014
    Posts
    5

    passing many select values

    Hi, I have this code to create a number of select, each one with a number of options. Now, let's say for example that I have 5 select (name=1,2,3,4,5), I don't understand what I should do to pass the values to the next php page (where they will be stored into a db)

    Code:
    for($i=1;$i<=$_POST['numeroComponenti'];$i++){ // create (example) 5 select
        $res=mysql_query("SELECT DISTINCT nomeArte FROM cantante ORDER BY nomeArte ASC",$con);
        echo"Cantante $i
            <form action='addCantanti2.php' method='post'>
            <select name='[$i]'>"; //1,2,3,4,5
            while($Autors=mysql_fetch_array($res)){
                $length=count($res);
                for($n=0;$n<$length;$n++){ //creates all the options for each select
                    echo"<option value='$Autors[$n]'>$Autors[$n]</option>";
                }
            }
            echo"</select>
                <input type='hidden' name='nomeGruppo' value='$_POST[nomegruppo]'></input>
                <input type='hidden' name='numCant' value='$_POST[$i]'></input>
                </form><br><br>";
    }
    I guess I should make an array into the next page where I can store the 5 names of the 5 select and for each one I should then take the selected value. But honestly I'm really lost on this. Can you help me?

    thanks

    B.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    851
    Cleaning up your code I see this now:
    PHP Code:
    for($i=1$i<=$_POST['numeroComponenti']; $i++)

        
    // FOR SOME NUMBER OF TIMES ABOVE YOU ARE DOING THE SAME EXACT QUERY FOLLOWING      REPEATEDLY
         // create (example) 5 select
        
    $res=mysql_query("SELECT DISTINCT nomeArte FROM cantante ORDER BY nomeArte ASC",$con);
        echo 
    "Cantante $i <form action='addCantanti2.php' method='post'>";
        echo 
    "<select name='[$i]'>";     //1,2,3,4,5
        
    while($Autors mysql_fetch_array($res))
        {    
    // EVERY TIME THRU THIS LOOP YOU ARE GETTING LENGTH WHICH IS ALWAYS THE SAME
             
    $length count($res);
             for(
    $n=0$n<$length$n++)
             { 
    //creates all the options for each select
                  
    echo"<option value='$Autors[$n]'>$Autors[$n]</option>";
             }
        }
        echo 
    "</select>";
         
    // I HAVE NEVER SEEN AN </INPUT> TAG USED BEFORE.  IS IT LEGAL?
        
    echo "<input type='hidden' name='nomeGruppo' value='$_POST[nomegruppo]'></input>";
        echo 
    "<input type='hidden' name='numCant' value='$_POST[$i]'></input>";
        echo 
    "</form><br><br>";

    So you are going to do the same query over and over for whatever value your POST variable has and then you are going to output a form with a dropdown list in it every time. I can see that for a large number of rows you will possible run out of time in your script. Do you have error checking turned on??

  3. #3
    Join Date
    Jan 2014
    Posts
    5
    I know there were a few mistakes. Anyway I solved..this is my working code if anyone will need (no idea what is legal or not buit it's working fine)

    Code:
    $options="";
    $res=mysql_query("SELECT DISTINCT nomeArte FROM cantante ORDER BY nomeArte ASC",$con);
            while($Autors=mysql_fetch_array($res)){
                $length=count($res);
                for($n=0;$n<$length;$n++){ //genera tutte le opzioni
                    $options.="<option value='$Autors[$n]'>$Autors[$n]</option>";
                }
            }
    
    for($i=1;$i<=$_POST['numeroComponenti'];$i++){ // esempio 5
        echo"Cantante $i
            <form action='addCantanti2.php' method='post'>
            <select name='cantante_".$i."'>"; //1,2,3,4,5
    echo $options;
            echo"</select>
                <input type='hidden' name='nomeGruppo' value='$_POST[nomegruppo]'></input>
                <input type='hidden' name='numCant' value='$_POST[$i]'></input>";
    
    }
    		echo"<input type='submit' value='Conferma'></input>
    			</form>";
    and the other page

    Code:
    foreach ($_POST as $keyname => $keyvalue):
    	echo "<h10><br><br>$keyvalue</h10><br><br>";
    		if($keyvalue != "" && $keyvalue!=$_POST['nomeGruppo']){
    			$sql="INSERT INTO Partecipare (nomeCantante, nomeGruppo)
    			VALUES
    			('$keyvalue','$_POST[nomeGruppo]')";
    	
    			if (!mysql_query($sql))
    			{
    			die('Error: ' . mysql_error($con));
    			}
    	}
    endforeach;

  4. #4
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    <input> tags can either end as <input ... /> or <input ... > but not <input ...>...</input> as it probably would be incompatible across different browsers or cause the web page to not behave in the desired manner. But if it doesn't generate any errors, warnings or notices across different browsers, then that's great. It probably could be written differently but if it works, then job well done!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles