我在VIEW
文件夾中有兩個文件:addcustomer.php
和phoneError.php
。如何在Codeigniter中獲取onkeyup事件的Ajax功能
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
<?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>';
}
?>
我要添加Ajax功能到onkeyup
事件addcustomer形式檢查輸入有效的電話號碼。我打電話給addcustomer方法,並在Controller中加載了phoneError
,但沒有工作。我不確定我是否爲xmlhttp.open "GET"
提供了正確的網址。
是否在控制器中加載phoneError? public function phoneError(){ this-> load-> view('customer/phoneError'); } 在ht – jenii
不,不需要在'public function phoneError()中加載任何視圖' –