PHP,Javascript和SQL代码混淆了 [英] PHP , Javascript and SQL code mix up
问题描述
您好我有这个代码将从数据库中提取客户端名称和地址。
它将每个条目的客户名称输入到下拉列表中(< option value =<?php echo$ client?>><?php echo $ client?>< / option>
)这是在while循环中完成的。
然后我有一个Javascript,当你在下拉列表中选择一个选项时,它将改变名为'content'的DIV的innerHTML - 这是基于从数据库中提取的内容而言是唯一的。 - 这是我无法工作的地方..以下是我的代码,非常感谢任何帮助。
Hi I have this code that will pull the client name and address from the database.
It echo's out the client name for each entry into a dropdown (<option value="<?php echo "$client" ?>"><?php echo "$client" ?></option>
) which is done in a while loop.
Then i have a Javascript that will change the innerHTML of a DIV named 'content' when you select a option in the dropdown - this is unique based on what is pulled from the database. - This is where I can't get it to work.. below is my code any help is much appreciated.
<div id="selectBox">
<div class="ui-widget">
<form>
<label>Select a Client:</label>
<select id="combobox" >
<option value="">Select...</option>
<?php
include "db_conn.php";
callDB();
function callDB(){
$sql = 'SELECT * FROM clientlist';
$query = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($query))
{
$ID= $row['id'];
$client= $row["client"];
$postal_add= $row["postal_add"];
?>
<option value="<?php echo "$client" ?>"><?php echo "$client" ?></option>
<?php
}} ?>
</select>
</form>
</div>
</div>
<br>
<div id="content">
</div>
</div>
<script type="text/javascript">
// Script for changing Div
function change()
{
switch (document.getElementById("combobox").value)
{
case "<?php echo $client ?>":
document.getElementById("content").innerHTML = "<h2><?php echo $client ?></h2><b>Postal Address:</b> <?php echo $postal_add ?>"
break;
}
}
</script>
推荐答案
到Javascript执行时(客户端), PHP已经完成(服务器)。您需要在While-Loop中从PHP构建Javascript,然后在页面中稍后将其发送到浏览器:
By the time the Javascript is executed (Client), PHP already finished (Server). You will need to build the Javascript from PHP inside the While-Loop and send it to the browser later in the page:
// in while
$javascript .= "case $client:
document.getElementById('content').innerHTML = '<h2>$client</h2>
<b>Postal Address:</b>$postal_add'
break;"
// later in page
switch (document.getElementById("combobox").value)
{
<?php echo $javascript; ?>
}
这篇关于PHP,Javascript和SQL代码混淆了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!