使用PHP和Javascript的组合填充下拉列表
I have the following view:
我有以下观点:
<form action=<?=$target;?> method="post" name="editForm">
<div id="page1">
<select id="categories" name="category" onmouseover="populateCategory(<?=$result?>, <?=$id?>)" >
</select>
</div>
</form>
which utilizes the following javascript function on the select tag:
它在select标签上使用以下javascript函数:
function populateCategory(subCategories, id) {
var select = document.getElementById(id);
var opt = document.createElement("option");
opt.text = "--Please make a selection--";
for (i = 0; i < subCategories.length; i++) {
var opt = document.createElement("option");
opt.value = i;
opt.text = subCategories[i];
select.add(opt);
}
}
The arguments passed to the function are the following variables:
传递给函数的参数是以下变量:
$result = $this->getCategories(0);
$id = 'categories';
which calls the following function
它调用以下函数
public function getCategories($parent, $published = 1) {
//Sanitize Params...
//$sanParent = $this->db->getEscaped($parent);
//$sanPublished = $this->db->getEscaped($published);
$query =
"SELECT * FROM #__adsmanager_categories
WHERE parent = " . $parent .
" AND published = " . $published;
$this->db->setQuery($query);
$result = $this->db->loadAssocList();
var_dump($parent);
if (!$result) {
return false;
}
return $result;
}
The low-down:
低调:
The last function I posted makes a request to a database, obtaining a list of categories which are intended to be used to create a dropdown list. I thought that if I passed the obtained categories to the JavaScript function, that I may be able to populate it the easy way. Am I doing this wrong? I have a feeling it's a JavaScript issue, as the function call doesn't even seem to happen. When I open the browser debugger, for example, there is no indication of whether or not it was called.
我发布的最后一个函数向数据库发出请求,获取用于创建下拉列表的类别列表。我认为,如果我将获得的类别传递给JavaScript函数,我可以轻松地填充它。我做错了吗?我觉得这是一个JavaScript问题,因为函数调用似乎甚至没有发生。例如,当我打开浏览器调试器时,没有迹象表明它是否被调用。
update:
更新:
Here is the what the html-source lists:
这是html源列出的内容:
<script type="text/javascript">
function populateCategory(subCategories, id) {
var select = document.getElementById(id);
var opt = document.createElement("option");
opt.text = "--Please make a selection--";
for (i = 0; i < subCategories.length; i++) {
var opt = document.createElement("option");
opt.value = i;
opt.text = subCategories[i];
select.add(opt);
}
}
</script>
<form action=/index.php?option=com_adsmanager&task=write&Itemid=1 method="post" name="editForm">
<div id="page1">
<select id="categories" name="category" onmouseover="populateCategory(Array, categories)" >
</select>
</div>
</form>
1 个解决方案
#1
2
I't not a javascript issue but rather understanding issue.
我不是一个javascript问题,而是理解问题。
which calls the following function
<- here is your mistake.
它调用以下函数< - 这是你的错误。
JS cannot call PHP functions. As it's run in the browser, while PHP being run on the server, a thousand miles away.
Quite contrary, its indeed PHP creates all the JS along with HTML and stuff.
JS无法调用PHP函数。因为它在浏览器中运行,而PHP在服务器上运行,距离千里之外。相反,它确实PHP创建了所有的JS以及HTML和东西。
So, your PHP code have to create the JS function call already populated with data.
Have you checked the HTML source? Does it contain proper JS function call?
因此,您的PHP代码必须创建已填充数据的JS函数调用。你检查过HTML源代码吗?它是否包含正确的JS函数调用?
Update See, as soon as you posted the actual JS, it become as simple as an egg
更新看到,一旦你发布了实际的JS,它变得像鸡蛋一样简单
<select id="categories" name="category" onmouseover="populateCategory(Array, categories)" >
it says just "Array" instead of actual data.
Means you are trying to print an array as a strings. Which obviously won't work.
Use some loop to iterate that array and print it's contents in the appropriate (for JS) format.
它只是说“数组”而不是实际数据。意味着您尝试将数组作为字符串打印。这显然是行不通的。使用一些循环来迭代该数组并以适当的(for JS)格式打印它的内容。
更多相关文章
- mysql利用st_distance函数查询附近的点的功能
- MySQL按数组值排序
- mySql中SUBSTRING_INDEX函数用法
- 向数组添加一组数据库列的问题
- 在H2数据库中插入时间——函数“PARSEDATETIME”未找到
- MySQL 运算符和函数
- 如何使用webrequest使用Json数组绘制折线图
- 使用QSqlDatabase类的open函数导致程序崩溃
- 如何在php imap函数中看到看不见的电子邮件