Jump to content

Select box populating help


dsp77

Recommended Posts

I don't know how to make the select box to show the selected item that user picked at sign up (this is made in the first query) without showing all "domenii" (second query)  including the picked one.

Here is the select box output for a better understanding. The domain Stiinte Sociale appears 2 times.

<select name="id_domeniu"><option value="1" selected="selected">Stiinte Sociale</option><option value="1">Stiinte Sociale</option><option value="2">NanoBioSystems</option></select>

<?php
require_once('config.php');

$query="SELECT domenii.nume, domenii.id FROM domenii
INNER JOIN inregistrari
ON domenii.email_1=inregistrari.email_1";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
	$id_ales=$row['id'];
	$nume_ales=$row['nume'];
}
$select_box="SELECT nume,id FROM domenii";
$result2 = mysql_query ($select_box);
echo "<select name=id_domeniu>";
echo "<option selected='selected' value='$id_ales'>$nume_ales</option>";
while($nt=mysql_fetch_array($result2)){
echo "<option value=$nt[id]>$nt[nume]</option>";
}
echo "</select>"; ?>

Link to comment
https://forums.phpfreaks.com/topic/206851-select-box-populating-help/
Share on other sites

It isn't real clear what you're asking. It sounds like you want all the options in the first select list except for the one that was selected to go away after the first form is submitted. Is that right, or . . .  ?

yes the record appears two times and i want to remove the one that repeats that doesn't have the selected statement. As you can see its two times the first one is the good one that needs to remain and the second removed but its generated by while statement.

<option value="1" selected="selected">Stiinte Sociale</option>

<option value="1">Stiinte Sociale</option>

Well,

 

First thing.

 

There is no where in your code to recieve any data as to which item the user has selected.  PHP is a server side language and does not "talk" to client side, so anything the user is doing in the browser (click boxes, choosing select menu items, etc), PHP does not know this is happening, unless you send that information back to the server such as SOAP, Ajax, or a simple form POST/GET.

 

To retrieve the value of POST or GET, you can simply use the following in the event that the field name is "yourvarname"

 

// retrieving a POST variable
$myphpval = $_POST['yourvarname'];

// retrieving a GET variable
$myphpval = $_REQUEST['yourvarname'];

 

Then, you can modify your code such as the following:

<?php
require_once('config.php');

$query="SELECT domenii.nume, domenii.id FROM domenii
INNER JOIN inregistrari
ON domenii.email_1=inregistrari.email_1";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
   $id_ales=$row['id'];
   $nume_ales=$row['nume'];
}
$select_box="SELECT nume,id FROM domenii";
$result2 = mysql_query ($select_box);
echo "<select name=id_domeniu>";
echo "<option selected='selected' value='$id_ales'>$nume_ales</option>";
while($nt=mysql_fetch_array($result2)){
    if($nt[id] != $myphpvar ) {
         echo "<option value=$nt[id]>$nt[nume]</option>";
    }
}
echo "</select>"; ?>

 

Which of course you would need to redraw your form. You could use an AJAX form POST, and have the response as the new form.

 

This should help you with the PHP side of things anyway. For AJAX and such, I would suggest checking out jQuery, but that would be more for appearance than functionality ( aka, you wouldn't necessarily see a page refresh when reloading the form, depending on how the javascript code is setup). 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.