Use the Variable from a SELECT to populate a SQL query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
J39L4753
Forum Newbie
Posts: 5
Joined: Sun Jan 19, 2014 2:52 pm

Use the Variable from a SELECT to populate a SQL query

Post 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?
Last edited by J39L4753 on Wed Feb 26, 2014 2:35 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

getdata isn't echoing anything out, so nothing is being returned to the jQuery callback.

Code: Select all

echo json_encode($array);
J39L4753
Forum Newbie
Posts: 5
Joined: Sun Jan 19, 2014 2:52 pm

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

Post by J39L4753 »

Sorry that was my fault when copy pasting the code across, I've edited the post.
Post Reply