Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    16
    Rep Power
    0

    Seach and dropdown in php


    I've done a textbox and type in the word to serach from the Mysql database. When the record is found it is put in the dropdown list. What I could not get is to get the value selected from the dropdown list. Here's my coding. Hope to get help. Thanks.

    <?PHP
    require_once('Connectionchart.php');

    $descacc = $_POST[descacc];
    $descacc = preg_replace("#[^0-9a-z]#i","",$descacc);

    if ($_POST[descacc] != "") {

    $SQL1 = "SELECT * FROM acccode WHERE acccode.Descrip like '%$descacc%' ORDER BY Accno";

    $RES1 = mysqli_query($S, $SQL1); //SUBMIT A QUERY TO MYSQL

    $icount1=0;
    while ($VAR1 = mysqli_fetch_array($RES1)) //KEEP FETCHING RECORD
    {

    $accno2 = $VAR1[Accno];
    $descrip2 = $VAR1[Descrip];
    $icount1++;
    $accno3[$icount1] = $VAR1[Accno];
    $descrip3[$icount1] = $VAR1[Descrip];

    } // while

    echo "<form action='chartacc.php' method='post'>
    <select name='$accno3' id='$accno3' size='1'>";

    for ($counter = 1 ; $counter <= $icount1; $counter += 1) {

    echo "<option value='$accno3[$counter]' select='selected']>$accno3[$counter] $descrip3[$counter]</option>";

    $descrip4 = $descrip3[$counter];
    $accno4 = $accno3[$counter];

    } // for the for

    echo "<input type='submit' name='submit' value = 'HANTAR'></select>
    </form>";


    } // if
  2. #2
  3. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,200
    Rep Power
    495
    $descacc = $_POST[descacc];

    Should be:

    $descacc = $_POST['descacc'];

    Likewise ...

    if ($_POST[descacc] != "") {

    Should be:

    if ($_POST['descacc'] != "") {

    There ARE other problems, these are the first ones I spotted. The main thing I have observed is that you're trying to access array keys incorrectly; the key name is a string so needs to be enclosed with quotes, except in the cases where you're using variables.

    You know, by attempting a very basic debugging technique, you could have saved yourself all the hassle. Why can't people do a simple "echo" statement in their code to see if the values they're assigning to variables are what they expect??
    Last edited by badger_fruit; November 13th, 2014 at 03:55 AM. Reason: Increased sarcasm level in response
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    16
    Rep Power
    0

    Search and dropdown in php


    Hi,

    A newbie like me did put the echo statement .... I cannnot get the selected value in the dropdown even though I've put select = 'selected'...

    My textbox is a search where the user can type eg ... LAND and the data containing LAND will appear in the dropdown for eg 01001 LAND ASSETS and so on...

    I want to get the value that is the 01001 to be displayed...

    Please do help ...I've going thru for some time...
    Thanks
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,414
    Rep Power
    24
    With your 'echo' that builds your SELECT's <option> list, does every option on that list contain select='selected' ? This should not be. Also, simply using just selected vs select="selected" works just as well.
    What I usually do to define a preselected option is similar to the following:

    EDIT: Sorry I removed that. There is a ton that can be done to clean up an edit that code. If you wish, you can contact me via Steam username Triple_Nothing@hotmail.com and I will appear as Twank. If you do decide this way, the only thing I ask is for you to still post the final outcome of the script here for any future individuals looking to do similar.

    I am also a little lost via your
    I want to get the value that is the 01001 to be displayed...
    since you had said
    LAND and the data containing LAND will appear in the dropdown for eg 01001 LAND ASSETS
    Did you mean that the data is ALL showing, but you ONLY want the 01001 displayed and not the LAND ASSETS?
    (I think I got this, if you meant the "to be displayed" as it being the default selected.)

    EDIT: In my bored/free time I just tossed this together. I don't know the rest of your script, so if there was any use for the incrementing of the $accno and $descript variable names, it no longer exists. Since your SELECT grabs EVERYTHING from each row, each of the option echo's are displaying the data in the first 2 columns via the $row[0] and $row[1]. I see you were building larger arrays within your while() loop, but if you've no use for this data beyond the drop-down build, you don't need to build the arrays and can just echo out each option per loop. As far as the name and ID of your drop-down, I could not figure your intent since the variable you defined in there is an array, and does not hold a static value.
    PHP Code:
    <?PHP
    require_once('Connectionchart.php');

    $descacc $_POST['descacc']; 
    $descacc preg_replace("#[^0-9a-z]#i","",$descacc);
    $selName =
    $selID =
    if(
    $descacc != "") {
        
    $SQL1 "SELECT * FROM acccode WHERE acccode.Descrip LIKE '%$descacc%' ORDER BY Accno"
        
    $RES1 mysqli_query($S$SQL1); //SUBMIT A QUERY TO MYSQL 
        
    echo "<form action='chartacc.php' method='post'>
        <select name='
    $selName' id='$selID' size='1'>\n";
        while(
    $row mysqli_fetch_array($RES1MYSQLI_NUM)) {
            echo 
    "<option value='$row[0]'>$row[0] $row[1]</option>\n";
        }
        echo 
    "<input type='submit' name='submit' value = 'HANTAR'></select> 
        </form>"

    // if
    ?>
    Last edited by Triple_Nothing; November 13th, 2014 at 09:12 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  8. #5
  9. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,200
    Rep Power
    495
    There are problems with both the PHP and the basic HTML syntaxes within the script.
    Here is a tidier version ...

    PHP Code:
    <?PHP

    require_once('Connectionchart.php');

    $descacc $_POST['descacc'];
    $descacc preg_replace("#[^0-9a-z]#i","",$descacc);

    if (
    $_POST['descacc'] != "") {

        
    $SQL1 "SELECT * FROM acccode WHERE Descrip like '%" $descacc "%' ORDER BY Accno";
        
    $RES1 mysqli_query($S$SQL1); //SUBMIT A QUERY TO MYSQL 
        
        
    echo "<form action='chartacc.php' method='post'>";
        echo 
    "<select name='$accno3' id='$accno3' size='1'>";

        while (
    $VAR1 mysqli_fetch_array($RES1)) { 
            echo 
    "<option value='" $VAR1['Accno'] . "'>" $accno3[$counter] . " " $VAR1['Descrip'] . "</option>";
        } 
    // while    

        
    echo "<input type='submit' name='submit' value = 'HANTAR'></select>";
        echo 
    "</form>"

    // if
    You will notice a few things I have changed.
    1. There's no need to loop through the $VAR1 resultset assigning a load of variables when you can just write the <option>s as you iterate the results.
    2. I have corrected the HTML, as you were making every single <option> selected, I simply removed that.
    3. For readability, I have put each line of HTML into a separate echo statement.

    I haven't included anything to check that the $RES1 resultset actually contains any entries, nor have I done any checks to ensure the $_POST variables contain anything.
    I hope this helps.
  10. #6
  11. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,414
    Rep Power
    24
    Ah, I may still be way off, but as far as the selected item in the drop-down goes, will there ALWAYS be no more/less than 1 returned row containing 01001 for the accno?
    If this is the case, then you could add something similar to:
    PHP Code:
    $selected = ($row[0] == "01001" " selected" ""); 
    just above the echo'ed <option>s, and insert into your echo'ed <option>s:
    PHP Code:
    echo "<option value='$row[0]'" $selected ">$row[0] $row[1]</option>\n"
    If this is not the case, you need to define how you are going to pick which selected item is to be displayed.
    Last edited by Triple_Nothing; November 13th, 2014 at 09:08 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  12. #7
  13. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,200
    Rep Power
    495
    Hi triple_nothing
    Haha, I see we both had a bit of bored / free time to whack something together for the OP!
    I think that from a use-ability perspective, there shouldn't be a default selection option, maybe the <select> should have a "Please select" option in it.
    Also, nice to see that you've confirmed my "you don't need to loop twice" thought.
    Anyway, let's see what the OP comes back with

    Comments on this post

    • Triple_Nothing agrees : Ya. I say the default/selected is more an "Edit" mode type thing.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    16
    Rep Power
    0
    Hi guys...thanks a lot... Well did follow thru...juz wanna find out how to get the selected value from the dropdown as in : -

    echo "<option value='" . $VAR1['Accno'] . "'>" . $VAR1['Accno'] . " " . $VAR1['Descrip'] . "</option>";

    Where do I put the select=selected so that I can get the value depends on the dropdown which the user click and send.
  16. #9
  17. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,414
    Rep Power
    24
    I feel you might be misunderstanding the use of the 'selected' attribute. This has nothing to do with what the user is selecting. The value that gets submitted will simply be the value of the selected option under the <select>'s name. The 'selected' would be used in, for example, an instance where a user goes to edit their profile, and the state in the address has previously been defined.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  18. #10
  19. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,200
    Rep Power
    495
    Yeah, that's right ... the "selected=selected" attribute for an option sets that option to be the default one "picked" and you should only really have one OPTION selected ... consider this example, perhaps then it'll make more sense ...

    Example: When the SELECT box is rendered, "Option 2" will be selected by default:

    <select name='select1'>
    <option value='1' >Option 1</option>
    <option value='2' selected >Option 2</option>
    <option value='3' >Option 3</option>
    </select>

    So for your code, there's no need to default to any particular option, as triple_nothing says, it's used more when you're editing an exisiting record, like for example, when you're on this forum and you click into your user control panel to select the default language or theme - what you're using currently is 'selected' by default.

    So for your use, you determine what the user selected when they submit the form and it'll be one of the $_POST variables, however, may I suggest that you give the HTML select box a fixed name, rather than a variable because otherwise, you'd have a hard time figuring out what $_POST variable you're looking for.

    In my previously provided "tidier version" of your code, replace this line:
    echo "<select name='$accno3' id='$accno3' size='1'>";

    With this line:
    echo "<select name='selectbox' id='selectbox'>";

    Then, when the user clicks SUBMIT, you can see which OPTION they selected in the variable $_POST['selectbox'].
    Last edited by badger_fruit; November 14th, 2014 at 04:46 AM. Reason: Corrected typo
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    16
    Rep Power
    0
    Thanks guys...trying to figure out...
    Well.. when the dropdown value is chosen...how do I echo the selected value on the browser page...so is it as below :-
    if $_POST['selectbox'] = ??? {
    echo ???
    }

    Huhuhu...not so sure of this part....
  22. #12
  23. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,200
    Rep Power
    495
    Well, as I said, when you submit the form, the selected option will be in the variable $_POST['selectbox'], so you can ECHO that.

    if $_POST['selectbox'] != "" {
    echo $_POST['selectbox'];
    }
    Last edited by badger_fruit; November 14th, 2014 at 07:10 AM.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  24. #13
  25. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,414
    Rep Power
    24
    If you named it 'selectbox', you can echo it directly via:
    echo $_POST['selectbox'];
    or assign it to any other variable you may be using in your script and echo that.

    As far as the if/then in your question goes, it may depend on a few things. For one, if the user does NOT pick anything from the drop-down, what gets submitted? Generally this lands 1 or 2 ways, similar to one another. It would be the first item on the drop-downs list. Now, if you just build the drop-down via the returned values, the first will be selected if they overlook the drop-down. Kinda like a drop-down for the 50 state of America and the 1st option on the list always being AL for Alabama.

    Now, if it is more important that the person does select something themselves, you can manually enter a first option to the list, just before the while() loop, that offers an option with a "Please select..." option holding perhaps a "None" value. You can then use javascript to offer an alert to avoid submission without a selected value, and even unhide a hidden div/span near the field of issue. Otherwise, if not using javascript, you can use PHP to run an if/then such as below to check if a value was chosen.
    if($_POST['selectbox'] != "None") {
    // Do your intended actions...
    } else {
    // skip intended actions and reload page, alerting/noting where there's an issue.
    }
    Last edited by Triple_Nothing; November 14th, 2014 at 07:13 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    16
    Rep Power
    0
    Thanks guys...well it works fine...juz wanna get the description out...did try but not working...
    Here's the full code .. I've already sent the code across but I do want to send the description of the code that is the code $accno4 is 010101 and this code stands for LAND ACQUISITION. Now I want to show the description of the code that is LAND ACQUISITION....

    Please help....

    <?PHP

    require_once('Connectionchart.php');

    $descacc = $_POST['descacc'];
    $descacc = preg_replace("#[^0-9a-z]#i","",$descacc);

    if ($_POST['descacc'] != "") {

    $SQL1 = "SELECT * FROM acccode WHERE Descrip like '%" . $descacc . "%' ORDER BY Accno";
    $RES1 = mysqli_query($S, $SQL1); //SUBMIT A QUERY TO MYSQL

    echo "<form action='chartacc.php' method='post'>";
    echo "<select name='selectbox' id='selectbox' size='1'>";

    while ($VAR1 = mysqli_fetch_array($RES1)) {

    echo "<option value='" . $VAR1['Accno'] . "'>" . $VAR1['Accno'] . " " . $VAR1['Descrip'] . "</option>";

    } // while

    echo "<input type='submit' name='submit' value = 'HANTAR'></select>";
    echo "</form>";

    } // if

    ?>

    <?php
    if($_POST['selectbox'] != "") {
    $accno4 = $_POST['selectbox'];;
    $_SESSION['$accno4'] = $accno4;

    // tosend the value to the next page that's why the session is used.
    print "<script language=javascript> self.location='chartproj.php'</script>";

    }

    ?>
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    16
    Rep Power
    0
    Hi guys....help...help...help

    I've used as below :-

    <?php
    if($_POST['selectbox'] != "") {
    $accno4 = $_POST['selectbox'];
    $descrip4 = $_POST['selectbox'];
    $_SESSION['$accno4'] = $accno4;
    $_SESSION['$descrip4'] = $descrip4;

    // $_SESSION['$descrip4'] = $descrip4;
    print "<script language=javascript> self.location='chartproj.php'</script>";
    // Do your intended actions...
    }

    When I display the values become 010101LAND ACQUISITION. What I do want is to break the two fields....that is 010101 is one field and LAND ACQUISITION is another field so that I can place anywhere I like on the browser....

    Please.....help...help....Thanks...
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo