Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables from javascript script to PHP function

Status
Not open for further replies.

bradoirs

Technical User
Jul 8, 2009
35
GB
Way out of my depth here but it nearly works so where am I going wrong (apart from trying it in the first place)

What I am trying to achieve is to take multiple values from a picklist then using a PHP function to create another picklist based on the results.

So far the code creates an SQL query string successfully and equally successfully passes the variable to the function but that is where it goes wrong. The function recognises the variable (it will display on screen) but will not function as a query. What is puzzling me more is that none of the usual string functions work on it - for example strlen() returns a length of 6. I am obviously missing something but that is the danger of running before you can walk when learning.

function changez(){
var px="select * from `goldsupplier` where goldsupplywhat like '%start%'";
var pl = document.getElementById("goldprods");
var xl= (pl.options.length-1);
for (i = 0; i < (pl.options.length); i++) {
if (pl.options.selected="true"){
wot=pl.options.value;
if (wot=="a"){
px=px+" or goldsupplywhat like '%a%'";
}
if (wot=="b"){
px=px+" or goldsupplywhat like '%b%'";
}
if (wot=="c"){
px=px+" or goldsupplywhat like '%c%'";
}
}
}
document.getElementById("moreinputs").innerHTML = "<?php pickproductsupply('"+px+"'); ?>";
}


the PHP code

function pickproductsupply($prods){
$sql=$prods;
echo $sql;# DISPLAYS THE SQL QUERY
$result = mysql_query($sql);
$total = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
$gsref=$row["goldsupplierid"];
$gstitle=$row["goldsuppliername"];
echo"$gsref $gstitle<p>";
echo"<option value='$gsref'>$gstitle</option>";
}
echo" </select>";

}



 
are you intending that the query (px)_ runs on the server side? If so then you will need to use ajax to communicate with the server.

something like this (uses jQuery. you will need to bind the relevant text box or button/whatever to a click/change event to fire the changez function).

Code:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function changez(){
	var pl = document.getElementById("goldprods");
	var wot = '';
    for (i = 0; i < (pl.options.length); i++) {
        if (pl.options[i].selected = "true") {
            wot = pl.options[i].value;
        }
    }
    $.ajax({
		url: 'myapi.php',
		dataType: 'html',
		data: {q: wot},
		type: 'POST',
		success: function(data){
			$('#moreinputs').html( data );
		},
		async: false
	})
}
</script>

Code:
<?php
/* NB NEED TO CONNECT TO DATABASE FIRST */
$sql= "select * from `goldsupplier` where goldsupplywhat like '%start%'";
if(strlen($_POST['q']) > 0 ):
	$sql .= " OR goldsupplywhat like '%'" . mysql_real_escape_string($_POST['q']) . "%";
endif;
$result = mysql_query($sql) or die(mysql_error());
$options = array();
$format = "<option value='%s'>%s</option>";
while ($row = mysql_fetch_assoc($result)):
	$options[] = sprintf($format, $row["goldsupplierid"], htmlspecialchars($row["goldsuppliername"]));
}
echo "<select>\n".implode("\n\t", $options) . "\n</select>";
die;
?>

Overall it would be better to pass back a json object rather than html, but that's by the by. Also the php could be more elegant and contain error checking etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top