[英]editing html using javascript that calls php function that gets information from sql database.. complicated…
i am trying to edit a select list every time the list before it is being changed. 我试图每次更改列表之前都编辑一个选择列表。 what i am trying to do is change the list already existing into another list, called by a php function, that generates a list of the new information it has from the first list and an sql database.
我想做的是将已存在的列表更改为另一个列表,该列表由php函数调用,该列表从第一个列表和sql数据库生成它具有的新信息的列表。
it's pretty hard to explain so here is my code: 这很难解释,所以这是我的代码:
the base of my code 我代码的基础
//php & html
....
$cli = lastNameList(); //first list
$tli = firstNameList(); //second list
echo"
$cli
<div id='editHtml'>$tli</div>
";
....
functions (php) 函数(php)
//the functions
function lastNameList(){
$options="<select id='lastNames'>";
$sql="SELECT * FROM lastName";
$result=mysql_query($sql);
$options.="<option value='blank'>-- last names --</option>" ;
while ($row=mysql_fetch_array($result)) {
$name=$row["name"];
$options.="<option value='$name'>$name</option>";
}
$options.= "</SELECT>";
return "$options";
}
function firstNameList(){
$options="<select id='firstNames' style='margin-right:40px;'>";
having a problem over here: 在这里有一个问题:
$sql="SELECT DISTINCT Name FROM firstName WHERE LastName ='lastNameSelectedGoesHere'";
//how do i get the value of the last name so i could make a query for it?
//using distinct to make sure there are no duplicates
$result=mysql_query($sql);
$options.="<option value='blank'>-- first name --</option>" ;
while ($row=mysql_fetch_array($result)) {
$name=$row["Name"];
$options.="<option value='$name'>$name</option>";
}
$options.= "</SELECT>";
return "$options";
}
js (inside a php file if it matters) js(如果重要的话,在php文件中)
echo" <script type='text/javascript'>
$('#lastNames').change(function() {
document.getElementById('eTech').innerHTML=";
$ll = firstNameList();
echo"$ll;
});";
so as you can see my js file is in a php file, it does work, what i tried to do there, is generate a new first name list to come and replace the old one but i failed... anyone has an answer or suggestion to what i should do? 因此,如您所见,我的js文件在一个php文件中,它确实起作用,我尝试在其中进行的操作是生成一个新的名字列表来替换旧的名字列表,但我失败了……任何人都有答案或建议我该怎么办?
THis isn't complicated at all using ajax & jquery! 使用ajax和jquery一点都不复杂!
A quick dirty way is to do this 一种快速的肮脏方法是这样做
<div id="firstName"></div>
<script type='text/javascript'>
$('#lastNames').change(function() {
$('#firstName').load('getfirstname.php?lastname=' + $(this).val());
});
</script>
and for getfirstname.php page 对于getfirstname.php页面
$lastName = mysql_real_escape_string($_GET['lastname']);
$sql="SELECT DISTINCT Name FROM firstName WHERE LastName ='$lastName'";
//using distinct to make sure there are no duplicates
$result=mysql_query($sql);
$options="<select id='firstNames' style='margin-right:40px;'>";
$options.="<option value='blank'>-- first name --</option>" ;
while ($row=mysql_fetch_array($result)) {
$name=$row["Name"];
$options.="<option value='$name'>$name</option>";
}
$options.= "</SELECT>";
echo $options;
Here is a very simple page that shows how to execute php code using ajax and jquery. 这是一个非常简单的页面,显示了如何使用ajax和jquery执行php代码。
<?
if(!isset($_GET['value'])):?>
<script src="jquery-1.7.1.js"></script>
<select id="select" >
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</options>
</select>
<div id="phpoutput"></div>
<script>
$('#select').change(function() {
$('#phpoutput').load('?value=' + $(this).val());
});
</script>
<?else:
$val = $_GET['value'];
echo "$val sent to server. $val*3 = " . $val *3;
?>
<?endif;?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.