Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/209914-not-sure-is-it-php-or-java/
Share on other sites

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

}

}

?>

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">

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);

<?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?

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.

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???

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.

 

 

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);

<?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"/>

<?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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.