<select>
    <option value="1">one</option>
</select>

now i want to get 1 and "one" both using php..so plz reply me

Recommended Answers

All 12 Replies

the value in the select option is 1, if you want to get a different value you can do so programmatically, or by changing the value in the field. changing the value is an obvious method so I won't show it.
giving the select a name so it is accessible
programmatically

<?php if($_POST['name']=="1") {
echo "1 one";
//or
$variable="1 one";
//or
$name="1";
$name2=="one";}
?>

Have an array with indexes the values and elements the words you want to represent the values:

$numbers_array = array('1' => 'one', '2' => 'two', '3' => 'three');
// wrap everything within form tags
echo '<form action="#" method="post">';

// use PHP to construct the select element
echo '<select name="numbers">';
foreach($numbers_array as $value => $text) {
    echo " <option value='$value'>$text</option>"
}
echo "</select>";
// submit button
echo "<input name="submit" value="Submit" />";
echo "</form>";

// check if submit button was pressed
if(isset($_POST['submit'])) {
    $chosen_value = $_POST['numbers'];
    // display chosen value and text
    echo $chosen_value . ' - ' . $numbers_array[$chosen_value];
}
commented: thanks..plz solve this also +0
Hi..broj1

now i have dropdown list like this



    <?php 
        for($i=0; $i<$count; $i++)
        {
            echo "<option value='".$countryName[$i][0]."'>".$countryName[$i][1]."</option>";  
        }
    ?>

    Here,$countryName[$i][0] is countryID and $countryName[$i][1] is countryName so now as per ur answer how i can store values in array..?? plz reply me now

The values are already in an array $countryName so just use them.

Maybe you post the whole code so I can get the picture.

<html>
    <body>
        <?php
            include("includes/connectionPool.php");
            $obj=new connectionPool;

            $getQuery="select * from country";
            $countryName=$obj->select($getQuery);
            $count=count($countryName);

    ?>

            <h3>Registration Form</h3>
            <h5>* fields are mandatory</h5>
            <form name="form1" id="myform" action="addDetails.php" method="post" enctype="multipart/form-data" 
             onsubmit="return requiredValidation()">

            <!-- <input type="hidden" name="verificationCode" value="" />-->
            <table>
            <tr>
                <td>First Name:*</td><td><input type="text" name="txtFirstName" value=""/></td>
            </tr>
            <tr>
                <td>Last Name:*</td><td><input type="text" name="txtLastName" value=""/></td>
            </tr>
            <tr>
                <td>Address:*</td><td><textarea cols="15" rows="5" name="txtAddress"></textarea></td>
            </tr>
            <tr>
                <td>Country:*</td><td><select id="txtCountry" name="txtCountry" onchange="getState(this.value)">

                                            <option value="">Select Country</option>
                                            <?php 
                                            for($i=0; $i<$count; $i++)
                                            {
                                                echo "<option value='".$countryName[$i][0]."'>".$countryName[$i][1]."</option>";  
                                            }
                                            ?>
                                      </select></td>
            </tr>
            <tr>
                <td>State:*</td><td><div id="statediv"><select id="txtState" name="txtState">
                                            <option value="">Select Country First</option>
                                                    </select></div></td>
            </tr>
            <tr>
                <td>Contact No:*</td><td><input type="text" id="txtNumber" name="txtNumber" value=""/></td>
            </tr>
            <tr>
                <td>Email-ID:*</td><td><input type="text" name="txtEmail" value=""/></td>
            </tr>
            <tr>
                <td>Password:*</td><td><input type="password" name="txtPassword" value=""/>(min. 6 characters)</td>
            </tr>
            <tr>
                <td>Upload Profile Image:</td><td><input type="file" name="profileImage"/></td>
            </tr>
            <tr>
                <td><input type="submit" name="submit" value="SUBMIT"/></td>
                <td><input type="reset" name="clear" value="CLEAR"/></td>
            </tr>
            </table>

            </form>
            </center>
    </body>
    </html>

hi broj1 i want to insert countryname in DB when submit the form..

and ya i cant change option value bcoz it affects on my ajax code..so plz tell me how to get country name from dropdown list

You need to tell me what are the fields $countryName[$i][0] and $countryName[$i][1] in the tabče county (the first two fields).

Sorry, disregard my last post, I see you already explained that.

Please post the addDetails.php page code too since everything is happening there.

Just my guess since I havent seen the code of the addDetails.php page: On addDetails.php you will get the ID of the country and this is the information you want to store so you do not need a country name at all (or do you?). In case you really need it you can put it in a hidden field on the previous page and it will be sent over to addDetails.php in the $_POST array. Hope this helps.

suppose table name is tblcourse and suppose i select course_code from the dropdown list then releted records will be automatically show the remainig textfeild like course_name,course_fee etc. how can it is possible in php?

<?php

<?php
                    $arr =array("this", "is", "the", "dropdown");
                    echo "<select name='image'>";
                    foreach($arr as $option){
                        echo "<option value='{$option}'>{$option}</option>";  
                    }
                    echo "</select>";
                    ?>


                echo "<select name='image'>";
                foreach($arr as $option){
                    echo "<option value='{$option}'>{$option}</option>";  
                }
                echo "</select>";
                ?> 
        function model(){
        $user= mysql_query("select * from product ");
        while($record=mysql_fetch_array($user))
        {
        echo'<option value="'.$record['model'] .'">'.$record['model'] .'</option>';
        }   
        } 

        <select name="cid" onChange="this.value" >
                    <option value="">Select Category:</option>
                        <?php model() ?>
                    </select>
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.