Please help, so I have this combobox in html/php, So I want to load the records from mysql to a combobox, I had done that, so when the user selects a record in combobox, how can i get the selected value in the combobox?? Please guide me. I'm new in web.

so this is my table

<tr>
    <td></td>
    <td><?php include('cboProject.php'); ?></td>       
</tr>

cboProject.php(getting records from mysql)

$sql=mysql_query("SELECT DISTINCT pcode, projid FROM projb");

        echo "<select name='pcode'>";
        echo "<option>-- Select Project --</option>";

        while ($row = mysql_fetch_array($sql))

            echo "<option value='$row[projid]'>$row[pcode]</option>";

        echo "</select>";

So when the user selects from a combobox. how will I get the id?? Please guide me. I really do not know what's next. Am I doing it right? Thank you so much for the help.

Member Avatar for LastMitch

@bLuEmEzzy

So when the user selects from a combobox. how will I get the id?? Please guide me. I really do not know what's next. Am I doing it right? Thank you so much for the help.

In order to do that you need a <form> to select the query
It should look like something like what I wrote:

<form id="" name="" method="post" action="" >

<input type="hidden" name="" value="yes" />

<?php

$query = "Select id, pcode FROM projb ORDER BY pcode";

$result = mysql_query ($query);

echo "<select name=post value=''>POST Project</option>";

while($row = mysql_fetch_array($result)){

    echo "<option value=$row[id]>$row[pcode]</option>";

}echo "</select>";

?>

<input type="submit" name="submit" id="submit" value="Submit" />

</form>

Hopefully this will give you an idea how to write it base on your table. I don't really know your table. I just plug the words.

Give it a try and test it out.

Member Avatar for diafol

I'd suggest concatenating a string rather than using echo. That way you can further separate your html and php. Otherwise you end up with hard-to-maintain pages mixing markup and php.

$output ='';
$sql = mysql_query("SELECT DISTINCT pcode, projid FROM projb");
$output .= "<select name='pcode'>";
$output .= "<option>-- Select Project --</option>";
while ($row = mysql_fetch_array($sql)){
   $output .= "<option value='{$row['projid']}'>{$row['pcode']}</option>";
}
$output .= "</select>";

You can place that into an include file or before the !doctype declaration (html).

Then it's just a question of echoing $output in the relevant place.

To further develop this, you could wrap the code in a function or even place it in a class as a method.

For a vanilla function:

function getDropDown(){
    $output ='';
    $sql = mysql_query("SELECT DISTINCT pcode, projid FROM projb");
    $output .= "<select name='pcode'>";
    $output .= "<option>-- Select Project --</option>";
    while ($row = mysql_fetch_array($sql)){
       $output .= "<option value='{$row['projid']}'>{$row['pcode']}</option>";
    }
    $output .= "</select>";
    return $output;
}

...

<td><?php echo getDropDown();?></td>

That ain't brilliant, just illustrates my point. That way you don't have to worry about variable names clashing as they're all limited (scoped) inside the function.

commented: Nice Solution! +5
Member Avatar for LastMitch

@bLuEmEzzy

I would suggest to used diafol method because it's much cleaner than using the echos and my example is too difficult to make changes because of the combination of html/php. It's much easier to make adjustments or changes if used diafol methods than mines and he also explains the pro & cons about each method. I agree with his point of view.

Okay. I'll inform you for progress. Thank You for the help.

Guys, so the records are loaded in the combobox, example the value are {Sample1, Sample2, Sample3}, I choose Sample2, how can I get it's id? I need the id of Sample2 in my another query. Do I need a session to do that? Please help me. thank You

Member Avatar for LastMitch

@bLuEmEzzy

Guys, so the records are loaded in the combobox, example the value are {Sample1, Sample2, Sample3}, I choose Sample2, how can I get it's id? I need the id of Sample2 in my another query. Do I need a session to do that? Please help me. thank You

I'm not sure what you are talking about?

When you select Sample1, Sample2, Sample3 it should be a row (id) not a session?

Member Avatar for LastMitch

@bLuEmEzzy

I realize what you did. You're missing a step! You didn't have a previous query to select the id, that's why you can't select. You need to create a query to select the id in order for diafol example to work!

It should look like this :

$query = "SELECT * FROM WHERE id=$";

Member Avatar for diafol

If you use 'id' - you can use javascript to capture it. Form fields rely on the 'name' attribute when submitted directly to the server. So a quick fix may be to duplicate id and name attributes. However, this isn't always possible when considering array data as ids must be unique.

yeah, and how can I do that? Getting it's value and id? I really do not know I'll post all the codes. Hope you can understand it. Thank You
so this is my html codes and I use "include" to call cboProject.php

<body>
    <div id="wrap">
    <form id="SelProjCode" action="LoadProj.php" >
    <table width="200" height="200" id="tbl">
        <tr>
            <td colspan="2">Project Monitoring</td>
        </tr>
        <tr>
            <td>Select Project to Edit:</td>  
            <td></td>         
        </tr>
        <tr>
            <td>**<?php include('cboProject.php');
                echo $output ;
            ?>**</td>
            <td></td>
        </tr>
        <tr></tr>
        <tr>
            <td></td>
            <td><input type="Submit" id="Go" name="Go" value="Go"/></td>
        </tr>
    </table>
    </form>
    <br><br>
    </div>

This is my cboProject.php

<?php
    require_once('config.php');
    $con=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$con)
    {
        die('Could not connect:' . mysql_error());
    }

mysql_select_db("proj", $con);
    $output = '';
    $sql=mysql_query("SELECT DISTINCT pcode FROM projb");
    $output .= "<select name='pcode'>";
    $output .= "<option>-- Select Project --</option>";

    while ($row = mysql_fetch_array($sql)) {
        $output .= "<option value='{$row['projid']}'>{$row['pcode']}</option>";
    }

    $output .= "</select>";

?>

I tried diafol's code. Please inform me if I done it well/wrong.
So when the user selects record in the combobox how can I get the ID.
This is my code so far.

<?php
    session_start();

$projid = $_POST['pcode']; // I want to get the id and I do not know how
echo $projid;

$con=mysql_connect("localhost","root","");
if (!$con)
    {
    die('Could not connect:' . mysql_error());
    }
mysql_select_db("proj", $con);

$result = mysql_query("
Select * from projb INNER JOIN proja ON projb.projid = proja.projid 
INNER JOIN parti ON proja.parid = parti.parid INNER JOIN partb ON partb.parid = parti.parid WHERE projb.projid=$projid");

while($row = mysql_fetch_array($result))
    {
    session_regenerate_id();

    $_SESSION['projid'] = $row['projid'];
    $_SESSION['pcode'] = $row['pcode'];
    $_SESSION['projno'] = $row['projno'];
    $_SESSION['prtit'] = $row['prtit'];
    $_SESSION['propo'] = $row['prdur'];
    $_SESSION['pname'] = $row['pname'];

    session_write_close();
        header("location: fillup.php");
    exit();

    }
mysql_close($con);
?>

Am I doing it right? I'm sorry I'm a newbie in php. Thank You for the help!

If i get your question right, you want to retrieve a record, along with it's id. If it is so, lemme ask; between projid and pcode; which of the two is the id you want to retrieve.

Member Avatar for LastMitch

@bLuEmEzzy

You can either used javascript to capture the ID like what diafol said or you can used this:

$query = "SELECT * FROM WHERE id=$showthis"; to fetch the ID

This is where you put it:

$result = mysql_query("
Select * from projb INNER JOIN proja ON projb.projid = proja.projid 
INNER JOIN parti ON proja.parid = parti.parid INNER JOIN partb ON partb.parid = parti.parid WHERE projb.projid=$projid");
while($row = mysql_fetch_array($result))
    {
    session_regenerate_id();
    $_SESSION['projid'] = $row['projid'];
    $_SESSION['pcode'] = $row['pcode'];
    $_SESSION['projno'] = $row['projno'];
    $_SESSION['prtit'] = $row['prtit'];
    $_SESSION['propo'] = $row['prdur'];
    $_SESSION['pname'] = $row['pname'];
    session_write_close();
        header("location: fillup.php");
    exit();
    }
mysql_close($con);

oohhh nooo... I'm confused. @Webville312 im getting the projid(the id for pcode). @LastMitch thank u. I hope I can get it. thank u ;)

Member Avatar for LastMitch

@bLuEmEzzy

oohhh nooo... I'm confused

OK, let me explain it little here is an example of how to select the id

$query = "SELECT * FROM table WHERE id=$showthis";
tresult = mysql_query ($query);
while($row = mysql_fetch_array($tresult)){
        $projid = $row['id'];
     }

On diafol file when you select either Sample1, Sample2, Sample3 all of them will select the id because of the select id

$output ='';
$sql = mysql_query("SELECT DISTINCT pcode, projid FROM projb");
$output .= "<select name='pcode'>";
$output .= "<option>-- Select Project --</option>";
while ($row = mysql_fetch_array($sql)){
$output .= "<option value='{$row['projid']}'>{$row['pcode']}</option>";
}
$output .= "</select>";

I hope you get a picture how it works.

I know you are using Join so it's bit confusing.

Select * from projb INNER JOIN proja ON projb.projid = proja.projid
INNER JOIN parti ON proja.parid = parti.parid INNER JOIN partb ON partb.parid = parti.parid WHERE projb.projid=$projid");

Thank You for the help ;) I got the id using this code. Thank You God bless u ;)

$projid = $_GET['pcode'];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.