pass selected (drop-down) value to submit button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hannable80
    New Member
    • Feb 2008
    • 2

    pass selected (drop-down) value to submit button

    Hi,

    I have being doing some php code for the very first time using wordpress as a template, basicaly what i want the prog to do is get data from a drop down menu, which i have populated with data from a MSQL db. pass it to a button (on the same page) and it to run a SQL statemment and populate the data in a table.
    What i am having problems with is to get the data to the botton where it will execute on the same page.
    Code below:[php]<?php
    $dbhost = 'it.test.com';
    $dbuser = 'audit';
    $dbpass = 'audit';
    $conn = mysql_connect($ dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    $dbname = 'WinAudi2';
    mysql_select_db ($dbname);
    $result = mysql_query( "SELECT DISTINCT Computer FROM $dbname" ) or die("SELECT Error: ".mysql_error() );
    $num_rows = mysql_num_rows( $result);
    print "There are $num_rows records.<P>";
    print "<table width=100 border=1>\n";
    print "<select>";
    while ($get_info = mysql_fetch_row ($result)){
    foreach ($get_info as $field)
    print "<option>$field </option>\n";
    }
    print "</select></table>\n";
    ?>
    </body>
    </html>
    [/php]

    Please read the Posting Guidelines, its link is on the top of this forum. Especially the part about enclosing code within the appropriate code tags. - moderator
    Last edited by ronverdonk; Feb 28 '08, 02:39 PM. Reason: adding code tags
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Welcome to TSDN!

    What exactly is it you want or your problem?
    You are already building a <select> drop down list, but I don't see a <form> or a submit button.
    When you add that and after the selection has been submitted, what do you want to do with the passed data? And do you want to process it in the same script or in a new one. The previous is not clear to me.

    If you have developed any more code to process the submitted data, show it here.

    Ronald

    Comment

    • hannable80
      New Member
      • Feb 2008
      • 2

      #3
      Originally posted by ronverdonk
      Welcome to TSDN!

      What exactly is it you want or your problem?
      You are already building a <select> drop down list, but I don't see a <form> or a submit button.
      When you add that and after the selection has been submitted, what do you want to do with the passed data? And do you want to process it in the same script or in a new one. The previous is not clear to me.

      If you have developed any more code to process the submitted data, show it here.

      Ronald
      Cheers

      sorry about the pm
      Code:
       <FORM>
      <INPUT type="button" value="Submit" name="GO"/>
      </FORM>
      When i even this small bit of code in it throws a syntax error...
      I have populated the drop down. I need to select a highlighted option in it and pass it as a var to a button. Then the button "onclick" will run a sql statement with the Value inserted from the drop down and refesh the page and dump out a table with the results of the statement...WoW that sounds alot but id say it would only be about 6 lines of code.

      I have been reading up on it all day but i can't find a way to pass the high lighted data to a button!

      As you can tell im more of a casual programmer SORRY!

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Before you go any further, the following remarks on the code shown:

        1. you use a database name 'WinAudi2' and a table 'WinAudi2'. That does not look correct to me.
        2. your <option>'s have no values assigned to them.
        3. your <select> list is not within a form, so where do you want to send the data to?
        4. the <form> statement just has a submit attribute, but the form itself goes nowhere after hitting that submit
        5. you want to pass the selection option value to a button. How do you intend to catch that value, via a submit to a script or using JavaScript?

        I would propose the following:

        1. assign values to the drop-down list, so they can be passed.
        2. have the drop-down list AND the submit button within a <form>
        3. after selecting an option from the drop-down, its value will be stored in the submit button. After hitting the submit button, the value of the drop-down is passed to the same script, where that value is inserted into the database and a 'dump-out' of the table is made? DUMP-OUT???

        In order to prevent endless discussions here about what everybody means or wants, I'll take a shortcut and did some code work (only this time!). See the next (sanitized) code:[php]<script type="text/javascript">
        function setButton(myval ue)
        {
        document.getEle mentById("myBut ton").value="Cl ick here to \nupdate '"+myvalue+" ' in database";
        }
        </script>
        <?php
        // prepare and connect MySQL actions
        $dbhost = 'it.test.com';
        $dbuser = 'audit';
        $dbpass = 'audit';
        $conn = mysql_connect($ dbhost, $dbuser, $dbpass)
        or die('Error connecting to mysql');
        $dbname = 'WinAudi2';
        mysql_select_db ($dbname);

        // Check to see if form was posted
        if (isset($_POST['selectit'])) {
        // if so, do what you plan to do
        echo "Clicked and passed value: {$_POST['selectit']}<br>";
        print "Update table in database<br>";
        print "Show table<br>";
        }
        // form not posted: display selection box
        else {
        $result = mysql_query( "SELECT DISTINCT Computer FROM $dbname" )
        or die("SELECT Error: ".mysql_error() );
        $num_rows = mysql_num_rows( $result);
        print "<p>There are $num_rows records.</p>\n";
        print "Select an option from the following list:\n";
        print "<form method='POST' action='".$_SER VER['PHP_SELF']."'}>\n";
        print "<select name='selectit' onChange='setBu tton(this.value );'> >\n";
        while ($get_info = mysql_fetch_row ($result)) {
        foreach ($get_info as $field)
        print "<option value='$field'> $field</option>\n";
        }
        print "</select>\n";
        print "<p><input type='submit' value='&nbsp;&n bsp;&nbsp;' id='myButton'/></p>\n";
        print "</form>";
        }
        ?>[/php]Ronald
        Last edited by ronverdonk; Feb 28 '08, 04:27 PM. Reason: change text and add code

        Comment

        Working...