dwex Posted August 5, 2010 Share Posted August 5, 2010 lets say I have a dropdown menu , <select name="adddirector"> <option value="1">add 1</option> <option value="2">add 2</option> <option value="3">add 3</option> . . <option value="20">add 20</option> </select> How do I code it that when I send the value to the actual add page that if it's value 1 , it will echo out 1 textbox and if its 2 , will be 2 textbox and so on? Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/ Share on other sites More sharing options...
.josh Posted August 5, 2010 Share Posted August 5, 2010 if your form method is POST then echo $_POST['adddirector']; if the form method is GET then echo $_GET['adddirector']; Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095641 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 Something like this: for($i=0; $i<$_POST['adddirector']; $i++) { echo '<input type="text" name="director[]">'; } Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095642 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 @abra Brilliant. thanks man! Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095645 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 but now I have another problem. Instead of a textbox , I wanna change it to a dropdown menu that displays out the data from my database as options. How do I do that consistently for more than 1 dropdown menu? I tried this but didnt work. $amount = $_GET['adddirector']; $link7 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql'); $sql7 = "SELECT * from director "; $status7 = mysqli_query($link7,$sql7) or die (mysqli_error($link7)); <?php for($i=0; $i<$amount; $i++) { { ?> <tr> <td height="1"></td> <td><select name="ddrop"> <?php while ($row7 = mysqli_fetch_assoc($status7)) { ?> <option value="<?php echo $row7['directorID'];?>"><?php echo $row7['director'];?></option> <?php } mysqli_close($link7); ?> </select> </td> </tr> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095647 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 What do you mean "didn't work"? If you want multiple dropdowns with the same name, then they need to be an array: <select name="ddrop[]"> But this is unpredictable when using multiple selects. Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095652 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 What do you mean "didn't work"? If you want multiple dropdowns with the same name, then they need to be an array: <select name="ddrop[]"> But this is unpredictable when using multiple selects. Come to think of it, you could just use 1 select that allows multiple selections: <select name="ddrop[]" multiple="multiple"> Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095662 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 thx for replying. If there are the value is 3 then 3 dropdown menus will be popped out but only the very first dropdown menu will show all the data in my database while the second and third has nothing in it. Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095667 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 thanks for replying. If there are the value is 3 then 3 dropdown menus will be popped out but only the very first dropdown menu will show all the data in my database while the second and third has nothing in it. Sorry, missed that. The first time thru the for loop your while loops fetches all the rows so the pointer is at the end. So after the while loop you need to reset the results to the first row. Right after the close of the while loop, do: mysql_data_seek($status7, 0); Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095678 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 <?php for($i=0; $i<$director; $i++) { { ?> <tr> <td height="20"><b>Director:</b></td> </tr> <tr> <td height="1"></td> <select name="ddrop"> <?php while ($row7 = mysqli_fetch_assoc($status7)) { ?> <option value="<?php echo $row7['directorID'];?>"><?php echo $row7['director'];?></option> <?php } mysqli_close($link7);?> <?php mysql_data_seek($status7, 0);?> I tried putting it after the close statement but still the same. Did I place it wrongly? Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095682 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 OMG my bad , should put it after </select> too. IT WORKED !!!! THANK YOU SO SO MUCH Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095684 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 1 more question though , lets say I have 3 dropdown menu which means there will be 3 values that I need to post to my saving page. How do I bring post all 3 values over to my saving page because right now only the last dropdown menu's value is passed down. Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095687 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 What do you mean "didn't work"? If you want multiple dropdowns with the same name, then they need to be an array: <select name="ddrop[]"> But this is unpredictable when using multiple selects. Though, why not just use 1 dropdown and allow multiple selections in that dropdown??? Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095696 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 because I actually have an edit page where let say I have 3 directors in that drama. Then there will be 3 dropdown menu for me to change directors for each of the 3 directors and the options of the directors will be from my database. So I still have to find a way to pass all 3 values from the dropdown menus to my save page. Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095702 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 this is how my save looks like. Which I think need some kind of array or loop to save so many values. $link4 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql'); $query4 = "UPDATE drama_directors SET dramaID = '$id' , directorID = '$director' WHERE drama_directorsID='".$ddid."'"; $result4 = mysqli_query($link4, $query4) or die(mysqli_error($link4)); if(!$result4){ echo "failed"; }else{ echo "Updated Successfully."; } mysqli_close($link4); Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095703 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 I can't tell what your variables are or where they come from. If you use this: <select name="ddrop[]"> Then you will have a $_POST['ddrop'] array that contains the values of all of the dropdowns. Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095711 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 Array ( [ddrop] => Array ( [0] => 4 [1] => 5 [2] => 3 ) [dramaID] => 1 ) This is the print_r result. only 1 dramaID ended up in my database instead of 3 Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095718 Share on other sites More sharing options...
AbraCadaver Posted August 5, 2010 Share Posted August 5, 2010 You''l have to show your entire form, and wrap it in code tags. Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095719 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 <?php //admin $id = $_POST['dramaID']; $director = $_POST['adddirector']; $actor = $_POST['addactor']; $genre = $_POST['addgenre']; $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'drama'; $link7 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql'); $sql7 = "SELECT * from director "; $status7 = mysqli_query($link7,$sql7) or die (mysqli_error($link7)); $link6 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql'); $sql6 = "SELECT * from genre "; $status6 = mysqli_query($link6,$sql6) or die (mysqli_error($link6)); $link8 = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql'); $sql8 = "SELECT * from actor "; $status8 = mysqli_query($link8,$sql8) or die (mysqli_error($link8)); ?> <form method='post' action='doincreasedirector.php' enctype="multipart/form-data"> <?php for($i=0; $i<$director; $i++) { { ?> <tr> <td height="20"><b>Director:</b></td> </tr> <tr> <td height="1"></td> <select name="ddrop[]"> <?php while ($row7 = mysqli_fetch_array($status7)) { ?> <option value="<?php echo $row7['directorID'];?>"><?php echo $row7['director'];?></option> <?php } mysqli_close($link7);?> </select> <?php mysqli_data_seek($status7, 0);?> </td> </tr> <br><br> <?php } } ?> <?php for($i=0; $i<$genre; $i++) { { ?> <tr> <td height="20"><b>Genre:</b></td> </tr> <tr> <td height="1"></td> <td><select name="gdrop"> <?php while ($row6 = mysqli_fetch_assoc($status6)) { ?> <option value="<?php echo $row6['genreID'];?>"><?php echo $row6['genre'];?></option> <?php } mysqli_close($link6); ?> </select> <?php mysqli_data_seek($status6, 0);?> </td> </tr> <br><br> <?php } } ?> <?php for($i=0; $i<$actor; $i++) { { ?> <tr> <td height="20"><b>Actor:</b></td> </tr> <tr> <td height="1"></td> <td><select name="adrop"> <?php while ($row8 = mysqli_fetch_assoc($status8)) { ?> <option value="<?php echo $row8['actorID'];?>"><?php echo $row8['actor'];?></option> <?php } mysqli_close($link8); ?> </select> <?php mysqli_data_seek($status8, 0);?> </td> </tr> <br><br> <?php } } ?> <input type="hidden" id="dramaID" name="dramaID" value = "<?php echo $id ?>"/> <input type="submit" Value="Add"/> Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095721 Share on other sites More sharing options...
dwex Posted August 5, 2010 Author Share Posted August 5, 2010 <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'drama'; $link = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or trigger_error('Error connecting to mysql'); print_r ($_POST); $id = $_POST['dramaID']; $actor =$_POST['adrop']; $director = $_POST['ddrop']; $genre = $_POST['gdrop']; $query = "INSERT into drama_actors set actorID = '$actor' , dramaID = '$id'" ; $result = mysqli_query($link, $query) or die(mysqli_error($link)); if(!$result){ echo "failed"; }else{ echo "Updated Successfully."; } $query1 = "INSERT into drama_directors set directorID = '$director' , dramaID = '$id' "; $result1 = mysqli_query($link, $query1) or die(mysqli_error($link)); if(!$result1){ echo "failed"; }else{ echo "Updated Successfully."; $query2 = "INSERT into drama_genre set genreID = '$genre' , dramaID = '$id' "; $result2 = mysqli_query($link, $query2) or die(mysqli_error($link)); if(!$result2){ echo "failed"; }else{ echo "Updated Successfully."; } } mysqli_close($link); Quote Link to comment https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/#findComment-1095722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.