我是zend框架2和Web应用程序编程的新手.在我的应用程序中,我想有一个按钮,它触发一个改变数据库内容的函数,并返回一个String,我可以用它来更新网站的可见内容.因为我不想在点击按钮时重新加载网站,我想使用ajax来做这件事.在阅读了几个ajax教程之后,我想到解决方案看起来很简单:
HTML部分:
<head>
<script type="text/javascript">
function myFunction() {
var xmlhttp = new XMLHttpRequest();
// I am working with Chrome
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
var text = xmlhttp.responseText;
document.getElementById("text_paragraph").innerHTML =
text;
}
};
xmlhttp.open("GET", "function.php", true);
xmlhttp.send();
}
</script>
</head>
<body>
......
<button id="function_button" onClick="myFunction()">Click</button>
<p id = "text_paragraph">Initial text"</p>
......
</body>
Run Code Online (Sandbox Code Playgroud)
使用.php文件function.php(开头,我只想让它返回一个文本值):
<?php
echo "Text triggered by the button click";
?>
Run Code Online (Sandbox Code Playgroud)
当我尝试测试按钮时,没有任何反应.显然,xmlhttp.status是404并且找不到function.php文件.我想,无论是我放置function.php文件的位置(它与.phtml相同的文件夹 - 网站的视图文件)还是我在xmlhttp.open中使用的url - 函数都是错误的.你能告诉我如何正确使用zf2中的ajax吗?感谢您的时间,非常感谢每一个答案.
我正在考虑以下情况:我想计算字符串中字符的出现次数(例如,用于排列检查).
一种方法是分配一个256个整数的数组(我假设字符是UTF-8),用零填充它然后通过字符串并增加对应于int的数组位置的整数字符的价值.
但是,对于这种方法,每次都必须分配256个数组,即使分析的字符串非常短(因此只使用数组的一小部分).
另一种方法是使用Character to Integer HashTable并为每个遇到的char存储一个数字.这样,您只能拥有字符串中实际存在的字符键.
由于我对HashTable的理解是相当理论的,我真的不知道它是如何在Java中实现的,我的问题是:这两种方法中哪一种更有效?
编辑:
在讨论这个问题时(谢谢大家的答案)我确实意识到我对UTF-8的本质有一个非常模糊的理解.经过一番搜索后,我发现了这个我要分享的精彩视频,万一有人遇到同样的问题.
我想检查具有特定ID的节点是否已存在于我的图形中,或者是否必须创建新对象.目前我正在使用以下代码执行此操作:
// at this point I have the attributes for the node I need
String id = getIdOfNeededNode(); // The id is used to search for the node in the graph
// now I have to search for the node in the graph
Node node = new Node("dummy_id"); // This is the line I don't like;
// I would prefer not to have a dummy node
// but the compiler will then complain that the node might not be initialized …Run Code Online (Sandbox Code Playgroud)