Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:55:59+00:00 2026-06-16T18:55:59+00:00

I have a dropdown menu that is generated in a different file. It is

  • 0

I have a dropdown menu that is generated in a different file. It is generated with a while loop, and I would like to add one static value ( Select contract ).

Right now a dropdown menu looks like this:

1234
4321
2323
3232

And I would like to make it like this:

Select contracr
1234
4321
2323
3232

Here is my code in index.php:

<select id="text2" name="text2">

</select>

And here is my code in process.php (where items are generated):

<?php
$selectedKey = $_GET['selected_key'];
$query = "SELECT * FROM `1 received` WHERE Key = '".$selectedKey."'";
$run = mysql_query($query);
while($row = mysql_fetch_assoc($run)) {
    echo "<option value='".$row['Number']."'>".$row['Number']."</option>";
}
?>

Ajax code:

<script>
$("select#select2").change(function(){
    $.ajax({
        type: "GET",
        url: "process.php",
        data: "selected_key=" + $(this).val(),
        success: function(result) {
            $("select#text2").html(result);
        }
    });
});
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:56:01+00:00Added an answer on June 16, 2026 at 6:56 pm

    I think you’re overthinking this.

    Just add an extra option to the <select> – i.e. a literal value, not something PHP generates – before you add all the other values from the database. Give it a value like “0” or “NotSelected”, then check for that in your validation routines.

    Oh, and as an unrelated issue, don’t do this:

    $selectedKey = $_GET['selected_key'];
    $query = "SELECT * FROM `1 received` WHERE Key = '".$selectedKey."'";
    

    You’re opening yourself to SQL injections. If you don’t know what that is, look it up. You should be using prepared/parameterized queries for this. Both the PDO and mysqli extensions have this capability, so you should be using one of those instead of the mysql extension, which has been deprecated (i.e. “this will be removed from the language soon, so stop using it”) in the latest version of PHP.


    Update:

    Based on the fact that you’re updating the HTML directly using an AJAX request to get new content for the <select> element, you need to move the <option> item I mentioned to be part of the script you call in your AJAX request.

    Specifically, you should do it directly before your foreach loop:

    <?php
    $selectedKey = $_GET['selected_key'];
    
    // DO SOME VALIDATION ON $selectedKey here
    // Remember what I said about SQL injection and using mysqli/PDO
    
    $query = "SELECT * FROM `1 received` WHERE Key = '".$selectedKey."'";
    $run = mysql_query($query);
    echo "<option value='NotSelected'>Select Contract</option>";
    while($row = mysql_fetch_assoc($run)) {
        echo "<option value='".$row['Number']."'>".$row['Number']."</option>";
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dropdown menu that is generated in another file, and I would
I have a dropdown menu that works fine, but I would like it so,
I would like to have a Dropdown Menu in Tkinter, that includes the shortcut
I have a dropdown (ul.sub-menu) with items inside (ul.sub-menu > li) that I'd like
I have some code that first selects an option value in a dropdown menu
Background: I have a dropdown menu that basically works the same as the one
In my Magento install, I have a dropdown menu that is generated from a
Two problems, one biggy one little'n. I have a lovely dropdown menu that's giving
I have a dropdown menu that pulls in text from a database column. The
I have a dropdown menu that I am using and can't quite get it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.