Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts

    Pass two values into mysql

    Hi guys, I want to pass two values into a my database through a dropdown list.

    Here is my dropdown list
    PHP Code:
    <select name="roomsize" size="1">
    <option value="23.8" <?php if ($_POST['roomsize'] == '23.8') { echo 'selected="selected"'; } ?>>Living Room</option>
    <option value="14.1" <?php if ($_POST['roomsize'] == '14.1') { echo 'selected="selected"'; } ?>>Bedroom 1</option>
    <option value="11.6" <?php if ($_POST['roomsize'] == '11.6') { echo 'selected="selected"'; } ?>>Bedroom 2</option>
    </select>
    I want to be able to put into the database the option value (i.e 23.8) but also into another field 'Living Room - 23.8'

    Any thoughts?

  • #2
    New Coder
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    0
    Thanked 5 Times in 5 Posts
    I'm not sure I understand your question - do you want to pass the value of the selected "roomsize" to another form field value called "livingroom" with an onChange event?


    Quote Originally Posted by thoford75 View Post
    Hi guys, I want to pass two values into a my database through a dropdown list.

    Here is my dropdown list
    PHP Code:
    <select name="roomsize" size="1">
    <option value="23.8" <?php if ($_POST['roomsize'] == '23.8') { echo 'selected="selected"'; } ?>>Living Room</option>
    <option value="14.1" <?php if ($_POST['roomsize'] == '14.1') { echo 'selected="selected"'; } ?>>Bedroom 1</option>
    <option value="11.6" <?php if ($_POST['roomsize'] == '11.6') { echo 'selected="selected"'; } ?>>Bedroom 2</option>
    </select>
    I want to be able to put into the database the option value (i.e 23.8) but also into another field 'Living Room - 23.8'

    Any thoughts?

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    154
    Thanks
    52
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jamied_usa View Post
    I'm not sure I understand your question - do you want to pass the value of the selected "roomsize" to another form field value called "livingroom" with an onChange event?
    I want to pass into the database two values.
    1) the value of the selected "roomsize"
    2) another value "Living Room" into a different field

  • #4
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    if you are trying to do this with php you will have to use onChange=submit()

    1. parse_str($value); to Field1 and Field2 (which will require a double post)

    2. This works much better if you split the values using java

    Code:
    <option value="$field1,$field2">$field2<option>

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You don't have to repeat everything everywhere! Just make an array in the serverside, say
    PHP Code:
    $roomsize=array(
    '23.8'=>'Living room',
    '14.6'=>'Bedroom 1'
    '14.1'
    =>'Bedroom 2'
    ); 
    and use this through out your code (by making it's available by an include). For example, you may simplify your php for dropdown to
    PHP Code:
    <select name="roomsize" size="1">
    <?php
    foreach($roomsize as $key=>$value){
     
    printf('<option value="%s" %s>%s</option>',
     
    $key,($_POST['roomsize']==$key?'selected="selected"':''),$value);
    }
    ?>
    </select>
    Last edited by abduraooft; 12-09-2010 at 11:28 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    That's actually pretty slick abduraooft, but how do you control the array if you have a lot of records?
    that would be a lot of work hard coding all the array values.
    Is there a way to query the array without manual coding?


    I have been using this method with java, but if you view the source code you can see all
    the values.

    Code:
    <?php 
    include("plugin/connection.php") ;
    
    if($_POST) mysql_query("INSERT INTO costcodes ( 
    CostCode, 
    weight) VALUES( 
    '".$_POST['CostCode']."', 
    ".$_POST['weight'].")"); 
    }
    header("Location: ./costcodes.php");
    }
    ?>
    
    <html>
    <head>
    <script type="text/javascript">
    function Split(sel,first) 
    {
      if (sel.selectedIndex == 0 ) return;
      var temp = sel.value.split(",");
      sel.form["F"+first].value = temp[0];
      sel.form["F"+(first+1)].value = temp[1];
    }
    </script>
    </head>
    
    <body>
    <form method="post" name='Validation' target='_self' onsubmit="return validateform(this);">
    <!-- First Dropdown -->
    <select name="Sel1" onChange="Split(this,1)" >
    <option>=== Selection 1 ===</option>
    <?php 
    $sel1 = mysql_query("SELECT * FROM costcodes");
      while($r1=mysql_fetch_assoc($sel1)){
          echo "<option value='$r1[id],$r1[CostCode]'>$r1[CostCode]</option>\n";
    }
    ?>
    </select>
    
    <!-- if you dont want to see these fields type="hidden" -->
    <br><input type="text" name="id" id="F1" value="" size="5">
    <br><input type="text" name="CostCode" id="F2" value="" size="40">
    </body>
    </html>
    Last edited by DataTalk; 12-09-2010 at 02:21 PM.

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    but how do you control the array if you have a lot of records?
    that would be a lot of work hard coding all the array values.
    Is there a way to query the array without manual coding?
    That depends on the location where the values are coming from. If it's from a DB, then there's no need for an extra hard coded array. Anyway, wouldn't it be easier to hard code that array once than repeating the values everywhere (in the client side and serverside/DB) ?
    Last edited by abduraooft; 12-09-2010 at 02:32 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    I tried your example using array query, it only loads the first record in the database?

    Code:
    <?php
    $sql = mysql_query("SELECT * FROM costcodes");
      while($row = mysql_fetch_array($sql)){
        $costcode = array(
    $row[id]=>$row[CostCode],
    ); 
    }
    ?>
    
    
    <select name="CostCode" size="1">
    <?php
    foreach($costcode as $key=>$value){
      printf('<option value="%s" %s>%s</option>',
        $key,($_POST['costCode']==$key?'selected="selected"':''),$value);
    }
    ?>
    </select>

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by abduraooft View Post
    That depends on the location where the values are coming from. If it's from a DB, then there's no need for an extra hard coded array. Anyway, wouldn't it be easier to hard code that array once than repeating the values everywhere (in the client side and serverside/DB) ?

    If the db-values get changed and updated from time. It would be easier to store the values in the database and be able to manage the db-values +add ?edit -delete.

    How would you properly write the array in my example:


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •