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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    155
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Exclamation Insert Form Value into Query & Execute

    I am trying to get the value of "field_item" when selected to be put into a query and only the row in the database with the "field_item" value in a specific row will be drawn upon from the database and put into "field_cond" .

    Do I need to add a button to execute the query? Am I even on the right track?

    I'd prefer for it to just load but a button would work to.

    Sorry for my amateur-ish php Im still quite new at it.


    PS I searched for a long time before posting but I cannot seem to find a correct answer for what I want, tutorials it seems to vary wildly and will not work for me.
    PHP Code:
    <?php
        $allPrices 
    "SELECT * FROM tbl_prices ORDER BY prices_id DESC LIMIT 30";
        
    $pricesResults mysql_query($allPrices);

        
    ?>

        <form id="formQuote" action="#" method="post" enctype="multipart/form-data">
            <ul>   
             <li><label>Item</label>
             <select name="field_item" id="field_item" type="number">
                 <option value = "--">--</option>
    <?php
    while($row mysql_fetch_array($pricesResults)){
    echo
    "
    <option>"
    .$row['prices_2']."</option>
    "
    ;
    }
    ?>
                
             </select></li>
             
             <li><label>Condition</label>
             <select name="field_cond" id="field_cond">
              <option value="--">--</option>
    <?php
    $dropdownValue 
    $_POST['field_item'];

    $allPrices2 "SELECT * FROM tbl_prices WHERE prices_2 = '".$dropdownValue['field_item']."' ORDER BY prices_id DES;";
        
    $pricesResults2 mysql_query($allPrices2);

    while(
    $row mysql_fetch_array($pricesResults2)){
    echo
    "
    <option value=\""
    .$row['prices_3']."\">Good Condition</option>
    <option value=\""
    .$row['prices_4']."\">Cracked, Good LCD</option>
    <option value=\""
    .$row['prices_5']."\">Bad LCD</option>
    <option value=\""
    .$row['prices_6']."\">Dead</option>
    "
    ;
    }
    ?>
    Last edited by 7daytheory; 10-29-2013 at 03:23 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If you're basing a query off of a selected value, you will need to use a multi-step form to submit the data and pass off to the next form. So the chain will be a very large set of if statements to assemble the form and provide which step it is on.
    Effectively, you'll need to branch check each step:
    PHP Code:
    $iStep = isset($_GET['step']) ? (int)$_GET['step'] : 0;

    if (
    $iStep >= 1)
    {
        
    // get data for the next input
    }

    if (
    $iStep >= 2)
    {
        
    // get data for the third input
    }

    //. . . 
    And add the step to the form action (or you can post it and use a hidden input, either way). The fun part will be that you need to check inside of the steps if another input is set so you can add the selected option for the form.

    When complete, you can add Javascript to use AJAX to do it in the background.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Aug 2013
    Posts
    155
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Okay thanks!

    My buddy was saying something along the lines of this..

    PHP Code:
    <?php
        $allPrices 
    "SELECT * FROM tbl_prices ORDER BY prices_id DESC LIMIT 30";
        
    $pricesResults mysql_query($allPrices);
    ?>

        <form id="formQuote" action="#" method="post" enctype="multipart/form-data">
            <ul>
             
             <li><label>Item</label>
            <select name="field_item" id="field_item" onchange=“getSecondDropDownValues()”>
                 <option value = "--">--</option>
    <?php
    while($row mysql_fetch_array($pricesResults)){
    echo
    "
    <option>"
    .$row['prices_2']."</option>
    "
    ;
    }
    ?>

    <script>
    function getSecondDropDownValues(){
           $.post("field_cond.php",{ 
            dropdownvalue: $("#field_item").val()
        }, function(data) {    
            $("#field_cond").html(data);
        });  
    }
    </script>
                
             </select></li>
    field_cond.php file ..

    PHP Code:
    <?php 
    include('connect.php');

    $dropdownValue $_POST['dropdownvalue']; 
    $dropdownValue mysql_escape_string($dropdownValue);

    $pricesResults2 mysql_query("SELECT * FROM tbl_prices WHERE prices_2 = '$dropdownValue' ORDER BY prices_id DES;") or die(mysql_error());

    while(
    $row mysql_fetch_array($pricesResults2)){ 
    echo

    <option value=\""
    .$row['prices_3']."\">Good Condition</option> 
    <option value=\""
    .$row['prices_4']."\">Cracked, Good LCD</option> 
    <option value=\""
    .$row['prices_5']."\">Bad LCD</option> 
    <option value=\""
    .$row['prices_6']."\">Dead</option> 
    "


    ?>
    Any Merit in that?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, that'd be the ajax equivalent. Note that you will require Javascript to use that though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    7daytheory (10-29-2013)

  • #5
    Regular Coder
    Join Date
    Aug 2013
    Posts
    155
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Thanks so much! Appreciate it


  •  

    Posting Permissions

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