[英]How to get Ajax function for onkeyup event in Codeigniter
I have 2 files in VIEW
folder: addcustomer.php
and phoneError.php
. 我在
VIEW
文件夹中有2个文件: addcustomer.php
和phoneError.php
。
addcustomer.php addcustomer.php
<input type="text" id="textHint" onKeyUp="showHint(this.value)" name="phone" placeholder="1235558888">
<span id="txtHint"></span>
<script type="text/javascript" >
function showHint(str) {
var base_url = <?php echo base_url(); ?>
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
}
else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
// Get $p from phoneError.php
(xmlhttp.open("GET", "phoneError.php?p=" + str, true));
xmlhttp.send();
}
}
</script>
<input type="text" id="textHint" onKeyUp="showHint(this.value)" name="phone" placeholder="1235558888">
<span id="txtHint"></span>
phoneError.php phoneError.php
<?php
defined('BASEPATH') || exit('No direct script access allowed');
$p = $_REQUEST['p']; // required
$string_exp = "/^[0-9]{3}[0-9]{3}[0-9]{4}$/";
if ($p == !preg_match($string_exp, $p)) {
echo $error_message .= '<span style="color:red">Oops! The Phone you entered does not appear to be valid.</span>';
}
?>
I want to add Ajax function into onkeyup
event in addcustomer form to check valid phone number entered. 我想将Ajax函数添加到addcustomer形式的
onkeyup
事件中,以检查输入的有效电话号码。 I called addcustomer method and also loaded phoneError
in Controller but did not work. 我调用了addcustomer方法,并在Controller中加载了
phoneError
,但是没有用。 I am not sure I put correct url for xmlhttp.open "GET"
. 我不确定我为
xmlhttp.open "GET"
输入了正确的URL。
You can use this jquery code for your purpose. 您可以根据需要使用此jquery代码。 This code do exactly same that you want.
此代码与您想要的完全相同。
$("#textHint").keyup(function () {
$.get("phoneError.php?p=" + $(this).val(), function (data) {
$("#txtHint").html(data);
});
});
Well if your are using Codeigniter you should know basic structure of it. 好吧,如果您使用的是Codeigniter,则应该了解它的基本结构。 So put php code in same controller file which loads your view and name it as
因此,将php代码放在相同的控制器文件中,该文件会加载您的视图并将其命名为
public function phoneError(){
// your php code..
}
In html side 在HTML端
change id of span as id should be unique in same page. 将span的id更改为id在同一页面中应该是唯一的。
Replace 更换
<span id="txtHint"></span>
with this 有了这个
<span id="txtResult"></span>
In input tag remove onKeyUp attr. 在输入标签中,删除onKeyUp属性。 So replace with this
所以用这个代替
<input type="text" id="textHint" name="phone" placeholder="1235558888">
And some change in js js中的一些变化
So basically your view file is as 所以基本上你的视图文件是
addCustomer.php addCustomer.php
<input type="text" id="textHint" name="phone" placeholder="1235558888" value="">
<span id="txtResult"></span>
<script type="text/javascript" >
$(document).ready(function () {
$("#textHint").keyup(function () {
var str = $(this).val();
$.get("http://localhost/sitename/controllername/phoneError?p=" + str, function (data) {
$("#txtResult").html(data);
});
});
});
</script>
Now try with this. 现在尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.