Page 1 of 1

Use the Variable from a SELECT to populate a SQL query

Posted: Tue Feb 25, 2014 6:35 pm
by J39L4753
I have a Chained SELECT that pull the Make and then Model of what I'm doing, this works great, I now want to be able to take the variable from the Model SELECT and use it in the SQL statement to run a query to return a value which will then be poplulated into the INPUT field I have.

This is the INPUT on my main Form

Code: Select all

<div class="row">                                                    
	<div class="col-md-4">
		<div class="form-group">
			<label>Seasonal Efficiency Rating</label>
			<input type="text" readonly="readonly" class="form-control" id="saprating" name="saprating">
		</div>
	</div>
</div>
My getdata.php

Code: Select all

<?php
  include "db.php";
  $boilermodel = $_POST["boilermodel"];
  $sql = "SELECT SAP2009AnnualEfficiency FROM blr.BoilerModel WHERE BoilerModel = '".$boilermodel."' ";      
  $res = mysql_query($sql);
  while($row = mysql_fetch_assoc($res)) {            
    	$array[] = $row['BoilerMakeModel'];
  }
        echo json_encode($array);
    }
?>
the jQuery

Code: Select all

<script type="text/javascript">
    $(document).ready(function(){
        $("select#boilermodel").change(function(){
        var boilermodel = $("select#boilermodel option:selected").attr('value');
        $.post("assets/configs/getdata.php", {boilermodel:boilermodel}, function(data){               
            $("input[name='saprating']").html(data);
        });
    });
});
</script>
When the variable is inserted into the query nothing is returned,no errors show either

question is where have I gone astray with this so far?

Re: Use the Variable from a SELECT to populate a SQL query

Posted: Tue Feb 25, 2014 7:17 pm
by Celauran
getdata isn't echoing anything out, so nothing is being returned to the jQuery callback.

Code: Select all

echo json_encode($array);

Re: Use the Variable from a SELECT to populate a SQL query

Posted: Wed Feb 26, 2014 2:36 am
by J39L4753
Sorry that was my fault when copy pasting the code across, I've edited the post.