1

Can someone tell me how to populate a dropdown menu with the elements in the array and when someone chooses a certain album like say Closing time after submitting the form it will replace with the 2nd element of the array "in voorraad" I have been trying to search for it I can't find anywhere.

Array in php form

$album["Closing time"]="in voorraad";
$album["Rumba Azul"]="uit voorraad"; 
$album["Survivor"]="in voorraad";  
$album["Greatest Hits Queen"]="uit voorraad";
$album["3121 Prince"]="in voorraad";

The html part I don't know how to query the array in

<form action="Albert_verwerken.php" method="post">
<table border=1 cellspacing=0 cellpading=0>  
<tr> <td>Achgternaam:</td> <td><input type="text" name="achternaam"></td></tr>   
<tr> <td>Voornaam:</td> <td><input type="text" name="voornaam"></td></tr>
<tr> <td>Adres:</td> <td><input type="text" name="adres"></td></tr>
<tr> <td>Postcode:</td> <td><input type="text" name="postcode"></td></tr>
<tr> <td>Plaats:</td> <td><select name="plaats">
<option value="">Kies...</option>
<option value="amsterdam">Amsterdam</option>
<option value="rotterdam">Rotterdam</option>
</select></td></tr>

1 Answer 1

3
foreach($array as $key=>$value){
    $html .= "<option value='$key'>$value</key>";
}

echo "<select name="process">$html</select>";

where $array is your array.

ref: http://php.net/manual/en/control-structures.foreach.php


and following on you can pick this up in processing as follows:

$p = $_POST; //assuming its post

$value = $p['process'];
Sign up to request clarification or add additional context in comments.

5 Comments

the array is in the processing form i have an index.php and processing.php the top part is in processing the bottom part is in index
why not move the array to the index page and just pass the value you need to the processing form?
hmm if i move it there what codes shuold i use ? im sorry for the simplistic questions but im in a test right now and i have to pass it it woul mean alot to me if yuo would help me :)
you can use the same code, just create the <select> part as above then when the form get passed to processing you will have the value. I will update my example as i think its wrong.
Index: <php? $voorraad["Closing time"]="in voorraad"; $voorraad["Rumba Azul"]="uit voorraad"; $voorraad["Survivor"]="in voorraad"; $voorraad["Greatest Hits Queen"]="uit voorraad"; $voorraad["3121 Prince"]="in voorraad"; foreach($voorraad $key=>$value){ $html .= "<option value='$key'>$value</key>"; } echo "<select name="process">$html</select>";?> this should work like this if i add it to the index?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.