Retrieve id from a <select><option>//php//html

Solved
the_jb Posted messages 731 Status Member -  
 KIKOFF -
Bonjour, j'ai un petit souci de récupération de variable très simple sauf que je me creuse la tête pour trouver où est le problème ...

Tout d'abord j'ouvre mon formulaire :
echo "<form action='modif.php' method='POST'>";

Je récupère des données via ma base de données :
$requete="select * from tarif";
$resultat=mysql_query($requete);

Après ça je fais :
echo "<select name='id_prix'>
"; while($rows=mysql_fetch_array($resultat))
{
echo "<option value='" . $rows['id_prix'] . "'>" . $rows['desc'] . " - " . $rows['prix'] . "</option>";
}
echo "</select>";

tout simplement. Cela fonctionne car il me récupère bien tous les tarifs sauf que j'aimerais pouvoir modifier le prix d'un article.

Donc j'ai ajouté :
echo "<input type='text' name='prix'><input type='submit' name='modif' id='modif' value='Modifier'></form>";

Et donc quand je clique sur modifier rien ne se passe.. j'ai fait un echo $_POST['id_prix'] et $_POST['prix']... il m'affiche que le prix. Et quand je fais un $rows['id_prix'] dans mon <option>, il me les affiche...

À l'événement submit j'effectue une requête simple :

$sql="UPDATE 'tarif' SET prix='$_POST['prix']' WHERE id_prix='$_POST['id_prix']'";
$result=mysql_query($sql);

Je suis proche je le sens mais... P'tite question : Auriez-vous une meilleure solution pour modifier le prix d'un article ?
Merci pour votre aide.

11 answers

tryan44 Posted messages 1289 Registration date   Status Member Last intervention   220
 
$_POST['id_prix'] returns $rows['id_prix']
$_POST['prix'] cannot return anything since it is outside of the while loop and contains no value (value).

The idea would perhaps be to add the price after the id returned from the value of the loop, to refresh the page upon selecting from the list to display the modification and validation fields, etc...

Basically, untested and just for the idea:

 <form action="modif.php" method="POST"> <?php $requete="select * from tarif"; $resultat=mysql_query($requete); //refresh the page upon selection echo '<select name="id_prix" onchange="javascript:submit(this)">'; while($rows=mysql_fetch_array($resultat)) { //insert id + price in the value separated by a dash echo '<option value="'.$rows['id_prix'].'-'.$rows['prix'].'">'.$rows['desc'].' - '.$rows['prix'].'</option>'; } echo '</select>'; //upon selection of the list if(isset($_POST['id_prix'])){ //explode the value to separate id from price $explore = explode('-',$_POST['id_prix']); //$explore[0] = id_prix //$explore[1] = price //put it all in fields echo '<input type="text" name="id" value="'.$explore[0].'"> <input type="text" name="prix" value="'.$explore[1].'"> <input type="submit" name="modif" id="modif" value="Modifier">'; } echo '</form>'; //if action to validate if(isset($_POST['modif'])){ $sql="UPDATE tarif SET prix=$_POST['prix'] WHERE id_prix=$_POST['id']"; $result=mysql_query($sql); } ?> 

--
A stupid question leads to a stupid answer!
A poorly worded question leads to a random answer!
1
the_jb Posted messages 731 Status Member 58
 
Ok, thanks for everything. I'm testing something like this and I'll let you know how it goes.
0
the_jb Posted messages 731 Status Member 58
 
The idea of awarding the prize regarding the article was already done, but I hadn't added it to my post. In any case, this one does exactly the same thing for me: during the selection, it always returns to the first value, however, it displays the correct price of the selected article..

It still does not retrieve the corresponding value (the corresponding ID)...
0
tryan44 Posted messages 1289 Registration date   Status Member Last intervention   220
 
Amazing... your form in simplified version based on my last post:
 <form action="#" method="POST"> <select name="id_prix" onchange="javascript:submit(this)"> <option value="1-100" <?php if($_POST['id_prix']=='1-100'){ echo "selected='selected'";}?>>100</option> <option value="2-200" <?php if($_POST['id_prix']=='2-200'){ echo "selected='selected'";}?>>200</option> <option value="3-300" <?php if($_POST['id_prix']=='3-300'){ echo "selected='selected'";}?>>300</option> </select> <?php //on selection of the list if(isset($_POST['id_prix'])){ echo 'Value = '.$_POST['id_prix'].''; //we explode the value to separate the id from the price $explore = explode('-',$_POST['id_prix']); echo '<pre>'; print_r($explore); echo '</pre>'; //we put everything in fields echo 'id:<input type="text" name="id" value="'.$explore[0].'" READONLY><br/> price:<input type="text" name="prix" value="'.$explore[1].'"><br/> <input type="submit" name="modif" id="modif" value="Modify">'; } ?> </form> <?php //if action to validate if(isset($_POST['modif'])){ echo 'id='.$_POST['id'].'<br/>price='.$_POST['prix'].''; /*here we have the Update with redirection to the same page so that the list takes into account the price modification*/ } ?> 

--
A stupid question leads to a stupid answer!
A poorly formulated question leads to a random answer!
0
the_jb Posted messages 731 Status Member 58
 
I don't understand, yet my ID is displayed correctly in my input... I tested my query in phpMyAdmin and it works perfectly... Except that my selection always resets to 0 after the submit event

I don't know what the problem is?
0
tryan44 Posted messages 1289 Registration date   Status Member Last intervention   220
 
In your "option" list, there must be a comparison element to maintain the "selected='selected'" selection; otherwise, on each submit, the list reverts to the default at the beginning.

<option value="valeur" <?php if($_POST['id_prix']=='valeur'){ echo "selected='selected'";}?>>***</option>

--
A stupid question leads to a stupid answer!
A poorly formulated question leads to a random answer!
0
the_jb Posted messages 731 Status Member 58
 
oww... <option value="valeur" yes="" well=""> returns a different value each time. namely 1,2,3,... a simple $rows['id_prix'] placed in the option displays this. so it's true that each id is different. The thing is why it doesn't want to keep it. Yet at the submit event I tell it to display the id_prix and it shows it to me...

I'm going to try what you wrote above to keep the selected option.</option>
0
tryan44 Posted messages 1289 Registration date   Status Member Last intervention   220
 
it should result in something like:
 echo '<option value="'.$rows['id_prix'].'-'.$rows['prix'].'"'; if($_POST['id_prix']==''.$rows['id_prix'].'-'.$rows['prix'].''){ echo "selected='selected'";} echo '>'.$rows['desc'].' - '.$rows['prix'].'</option>'; 

--
A stupid question leads to a stupid answer!
A poorly phrased question leads to a random answer!
0
KIKOFF
 

Congratulations, the code is clear and optimal. There are some commas that escape me, but really, well done and thank you, really thank you, Christophe from B68.

0
KIKOFF
 
I have never seen such an incrementation and for a simple code, but besides being well thought out, it is optimally written. Well done, that's programming! I'm blown away, you master this language.
But where did you learn this syntax? What training? Self-taught?
I would like to get in touch and submit a few lines of code to you. I will pay if you want.
Christophe Diebold, Alsace 68, address d.christophe68@yahoo or hotmail.fr. Thank you, it would really be cool to get in touch at 1 of C 4 ' I hope.
0
the_jb Posted messages 731 Status Member 58
 
I test ;D.
0
the_jb Posted messages 731 Status Member 58
 
Yes, it works. It keeps the selected article well! But it still doesn't want to modify =(
0
the_jb Posted messages 731 Status Member 58
 
I did it! After keeping the selection, I modified the query by just adding 2 little quotes and it works. Thanks for your help!
0