Jump to content
WesleyA

how to have table content placed in a select/option dropdown menu?

Recommended Posts

So my goal is now to have a table list read from the database. The content of the table is supposed to be put in an <SELECT> <OPTION VALUE> form for a dropdown menu having the user select only 1 value.

     $conn = new mysqli($servername, $username, $password, $dbname);     if ($conn->connect_error) {        die("Connection failed: " . $conn->connect_error);       }     $sql = "SHOW TABLES";      if (!$result = $conn->query($sql)){	     die('There was an error running the query[' .$conn->error. ']');	       }       foreach($row = $result->fetch_assoc()){		echo "<center>"; 	     $reeks = implode(" " , $row);	 // echo $reeks;		// echo "<br>";	echo "</center>";            ?>      <center>        <form name = "inpform"  method="post" action="Add-succ7.php">         <SELECT >		<option value = "<?php echo $reeks; 		?>" > </option>       </SELECT>            </center>	      <?php       }      mysqli_close($conn);       ?>

Do I have to do this with foreach or while? (or something else?)

 

In the checkbox version I use while but when doing that I get a list of 8 checkboxes without any value. I see the dropdown menu is placed in the loop of while so another condition (like foreach) seems to be the first thing to think about.

 

But what kinda solutions would you have for the script above?

Share this post


Link to post
Share on other sites

Your loop should be printing option elements. It looks like you have a single option element with the values from every row stuffed into it. I assume you want multiple options, so that's what goes in the loop.

Share this post


Link to post
Share on other sites

Ok here is the php code in a more inline style.

    $conn = new mysqli($servername, $username, $password, $dbname);    if ($conn->connect_error) {        die("Connection failed: " . $conn->connect_error);    }    $sql = "SHOW TABLES";    if (!$result = $conn->query($sql)){	die('There was an error running the query[' .$conn->error. ']');	    }     ?>        <form name = "inpform"  method="post" action="Add-succ7.php">         <SELECT >		<option value = " <?php 		$reeks='';		echo $reeks; 		while($row = $result->fetch_assoc() ) {				$reeks = implode(" " , $row);		?>  " ></option>	 </SELECT>      </center>	      <?php        }      mysqli_close($conn);      ?>

Although I have closed the option value which I will quote here:

 

<option value = " <?php

$reeks=''; echo $reeks; while($row = $result->fetch_assoc() ) { $reeks = implode(" " , $row); ?> " >

 

this code gives a strange repeat of "> about 12 times and unfortunately an empty dropdown menu.

 

I cant remove "> at the end but would like to receive some help finding a place where to put this part of the code.

Share this post


Link to post
Share on other sites

Think about it! You require multiple options, so where are these options placed? Yes, between the opening <select> and closing </select>, you don't repeat producing opening closing select tags, just the options, so where should while of while loop and its closing curly bracket be? Again!between the opening <select> and closing </select>Now to produce options you loop each time creating new open option tag, (<option ) add value details (value="whatever") from table and close open option tag (>) add option text from table ('whatever') and then closing option tag (</option>) within the loop.

Share this post


Link to post
Share on other sites

Okay dsonesuk this was very valuable. I managed to have the database tables show up in the dropdown menu.

 

 

But the strange thing is that submitting doesnt work anymore.

 

 

I changed the previous properly working checkbox input script into this dropdown menu with the idea it would work. So the name attribute was set properly but no new records are added.

 

 

Is there anything important that should be heeded when programming this?

 

 

The script now is like this:

    <form action="Add-Succ7.php" id="rublist" >     <SELECT name="kolom2" method="post" form="rublist" >     <?php 			while($row = $result->fetch_assoc() ) {				$reeks = implode(" " , $row);         echo $reeks;                       ?>		    <option value = "<?php // $reeks; ?>" >         <?php                               }  ?>                 </option>	     </SELECT>         </center>	       <?php         mysqli_close($conn);                ?>        // part of the script that looks after several input fields.    <input type="submit" method="post" value="Kies Rubriek"  />		    </form>

The input fields are later added as records to the database.

Edited by WesleyA

Share this post


Link to post
Share on other sites

You are still not getting it.

 

Your code produces with corrected form element and select

 

<form action="Add-Succ7.php" id="rublist" method="post">

<SELECT name="kolom2">

whatever1 <option value = "">

whatever2 <option value = "">

whatever3 <option value = "">

whatever4 <option value = "">

whatever5 <option value = "">

</option>

</select>

 

...</form>

 

when it should be producing

 

<form action="Add-Succ7.php" id="rublist" method="post">

<select name="kolom2">

<option value = "whatever1">whatever1</option>

<option value = "whatever2">whatever2</option>

<option value = "whatever3 ">whatever3</option>

<option value = "whatever4">whatever4</option>

<option value = "whatever5 ">whatever5</option>

</select>

 

...</form>

 

if you view the source and its not like this, IT WON'T WORK

Edited by dsonesuk

Share this post


Link to post
Share on other sites
    <form action="Add-Succ7.php" id="rublist" method="post">    <SELECT name="kolom2"><?php     while($row = $result->fetch_assoc() ) {         $reeks = implode(" " , $row); ?>        <option name ="kolom2" value = "<?????? dont know what to do here? >" > <?php           echo $reeks;           } ?></option>        </SELECT>      </center>         <?php     mysqli_close($conn);    ?>

The ouptut in Add-Succ7.php of var_dump is empty, so not null but ' ' .I tried to place $reeks in value and $_POST['kolom2'] but the var_dump($_POST['kolom2']) remained giving an empty or a variable as output. placing the name of one of the tables in value - in this case value="cars" - gave the wanted output. Is there a sort of instruction in between so that the form confirms which option is chosen?

Edited by WesleyA

Share this post


Link to post
Share on other sites

You STILL are not getting it!This is what you wnat to output INSIDE OF LOOP, ALL of this should BE INSIDE THE LOOP<option value = "whatever1">whatever1</option>Do this, ignore data from table, you just know if can read values when submitted, we just use number of records to produce temporary sample data

<form action="Add-Succ7.php" id="rublist" method="get">     <SELECT name="kolom2"><?php     $tmp=0;     while($row = $result->fetch_assoc() ) {         //$reeks = implode(" " , $row); ?>        <option value="<?php echo 'whatever'.$tmp; ?>"><?php echo 'whatever'.$tmp; ?></option> $tmp++;           } ?>         </SELECT>                <?php       mysqli_close($conn);     ?>
You DON'T need name attribute on options, it is only required on select that will store value/s from option/s selected, you have a misplaced </center>, the opening <center> MUST BE WITHIN <form>...</form>, think of it as box 'center' within box 'form' the box 'center' boundary cannot extend beyound the boundary of 'form'.Right! I changed method attribute value to 'get', when form submitted the page it goes to should show values selected in the address bar. So when it works, you just need to change tmp sample data to reference table data. Edited by dsonesuk

Share this post


Link to post
Share on other sites

I changed as you told, but I dont see which values you mean that are showing up in the address bar.

 

This is what is coming up after Add-Succ7.php

    ../Add-Succ7.php?kolom2=%3C%3Fecho+%27whatever%27+.%24tmp%3B+%3F%3E&linkoms[]=franse+thee&linkadd[]=http%3A%2F%2Fwww.thee.fr

Share this post


Link to post
Share on other sites

Sorry! missed opening php tag

<form action="Add-Succ7.php" id="rublist" method="get">     <SELECT name="kolom2"><?php     $tmp=0;     while($row = $result->fetch_assoc() ) {         //$reeks = implode(" " , $row); ?>        <option value="<?php echo 'whatever'.$tmp; ?>"><?php echo 'whatever'.$tmp; ?></option> <?php  //missed this one$tmp++;           } ?>         </SELECT>                <?php       mysqli_close($conn);     ?>

what you should see is Add-Succ7.php?kolom2=whatever1&linkoms[]=franse+thee&linkadd[]=http%3A%2F%2Fwww.thee.fr

 

attribute name

 

attribute name of 'kolom2' value

Share this post


Link to post
Share on other sites

Arrghhh! give us link, or all your code not just bits and pieces, as far as i can see it should work! it showed the name, but its value was reading php code, I suppose this page the form is on is php? unless the html form is valid it will work.

Share this post


Link to post
Share on other sites

OK here is the first script:

    // make connection with database    <form action="Add-Succ7.php" id="rublist" method="get">     <SELECT name="kolom2"><?php         $tmp=0;        while($row = $result->fetch_assoc() ) {                  // $reeks = implode(" " , $row); ?>  	<label form="rublist">	<option id="kolom2" value = "<?echo 'whatever' .$tmp; ?>" >   <?php 	             echo 'whatever' .$tmp;                      ?></option></label>       <?php $tmp++; }?>	     </SELECT>         	     <?php     mysqli_close($conn);            ?>    // code for input of name and webadres     <?php     $num = $_POST['number'];  // previous page gives user possibility to choose between 1 and 5    for ($i=0; $i < $num ; $i++) {         ?><p><center>        		<label for="linkoms_<?php echo $i ?>">		Omschrijving: <?php echo ($i + 1) ?>		</label>		        <input type="text" name="linkoms[]" 		id="linkoms_<?php echo $i ?>" 		placeholder="website naam" required /> <br> <br>						<label for="linkadd_<?php echo $i ?>">		Het webadres <?php echo ($i + 1) ?>		</label>				 <input type="text" name="linkadd[]" 		id="linkadd_<?php echo $i ?>" 		 placeholder="www.website.com" /><br><br>        </center>	</p>	<?php	        }    ?>     <br><br><br>	      <input type="submit" method="get" value="Submit"  />		      </form >

Then the Add-succ7.php which executes the adding of the link address and name to the database:

     <?php    // .. make connection     $conn = new mysqli($servername, $username, $password, $dbname);    if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}     if ($_SERVER['REQUEST_METHOD'] == 'POST') 	{     if (isset($_GET['kolom2']) )      {       var_dump($_GET['kolom2']);     $tab="";	 $tab=$_GET['kolom2'];			 $tab = filter_input(INPUT_POST, 'kolom2');    } 						   }	 If (isset($_GET['linkoms']) && (is_array($_GET['linkoms'])) && ($oms = " ") && isset($_GET       ['linkadd']) && (is_array($_GET['linkadd'])) && ($add = " ")  ) {						$tab="";		$tab=$_GET['kolom2'];		$tab = filter_input(INPUT_POST, 'kolom2');			          foreach($_GET['linkoms'] as $key => $dummy) {     $oms = $conn->real_escape_string($_GET['linkoms'][$key]);     $add = $conn->real_escape_string($_GET['linkadd'][$key]);     $sql = "INSERT INTO $tab (linkoms, linkadd) VALUES ('$oms', '$add')" ;        if ($conn->query($sql) === TRUE)                                { // echo  $sql .'<br>'; //  connection made                                                                } else {            echo "Error: an error occured while adding to the database" . " <br>" . $conn->error;            }						 echo "<br>" . "The records are added to the database "             ;                                      } 				}  else { echo "error: try again to add to the database"; } 	       ?>

It is not all of the code, parts with the password data etc. are left out just as the header parts, but I hope it gives a good impression of the way it should work.

Edited by WesleyA

Share this post


Link to post
Share on other sites

Geez! remove label, label does absolutely nothing to prevent it submission, the only problem is YOU! not following my example, where did you get the idea that label goes in select, I'll tell you, no where, MY EXAMPLE will work if you follow it correctly. I'm really starting to get bored with this, its obvious you have no idea on creating basic form and proper use of name attribute, until you do, you'll just going to repeat going round and round in circles, wrong php start tag, php closing tag in comment. The code is there to work you just have to learn basics of form to get that right first, so its good night from me and good night from him.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

Geez! remove label, label does absolutely nothing to prevent it submission, the only problem is YOU! not following my example, where did you get the idea that label goes in select, I'll tell you, no where, MY EXAMPLE will work if you follow it correctly. I'm really starting to get bored with this, its obvious you have no idea on creating basic form and proper use of name attribute, until you do, you'll just going to repeat going round and round in circles, wrong php start tag, php closing tag in comment. The code is there to work you just have to learn basics of form to get that right first, so its good night from me and good night from him.

 

Well I admit that I dont know a lot about html pogramming. Im still a beginner actually. I know name is used for giving a name to a form.

 

I have used name in the form element, but I'm not sure if that is enough, maybe you could explain that?

 

If you know any good and profound info online about the usage of html and attributes in html/php forms then please tell. I am looking for good info.

Share this post


Link to post
Share on other sites

php has nothing to do with correct usage with forms, it processes and return html content, you have to make sure what it returns is correctly laid out html and correct valid form html, You apparently don't know what/how to structure a form using labels, names attributes correctly, just guessing each time and probably hoping we do it for you.

 

Just the learn basics before attempting to dynamically create form elements with php.

 

http://www.w3schools.com/html/html_forms.asphttp://www.w3schools.com/html/html_form_elements.asp

http://www.w3schools.com/tags/att_select_name.asp

Share this post


Link to post
Share on other sites

Good !

 

I first tested a separate script making a dropdown menu, and managed to do that

 

I admit I never had done it before, at least not with php and transferring it to another page. My programming skills are not so high its more on a hobby level.

 

I realize that html is very important for webprogramming. Just starting with php will confront you with interesting challenges. :)

 

But anyway thanks for the help I really appreciate your patience and time.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...