![](/img/trans.png)
[英]Pass php variable from view to controller to model using codeigniter
[英]Pass variable from controller to javascript using PHP(Codeigniter)
我有注冊模塊,到目前為止,我已經完成了所有字段的驗證(字段是:名稱,電子郵件,用戶名和密碼),檢查電子郵件和用戶名是否已經存在。
並嘗試添加建議,如果用戶名已經存在。 我在用戶名中添加了前綴,但是在將變量傳遞給javascript並在我的視圖中顯示時遇到了問題
這是我的控制器
$get_username = clean_data($_POST['username']);
$where = array(
"username" => $get_username
);
$check_username = $this->Crud_model->count_result('username','users',$where);
if($check_username > 0)
{
$fetch_username = $this->Crud_model->user_exists('users',$where);
$last_username = strrev((int)strrev($fetch_username)); // returns last numeric value of username
if($last_username){
$count = count($last_username);//counts number of digit
$str = substr($username, 0, -($count));;// subtract numeric value from last of username
}
$newstr = $last_username+1;
$username= $get_username.$newstr;
echo json_encode("existing");
// echo "var username = ". json_encode($username).";";
}
else
{
$insert_user = array(
'first_name' => clean_data(ucwords($_POST['first_name'])),
'last_name' => clean_data(ucwords($_POST['last_name'])),
'profile_picture' => "profile-picture.jpg",
'username' => $get_username,
'email' => $_POST['email'],
'password' => hash_password($_POST['password']),
'status' => 1,
);
$this->Crud_model->insert('users',$insert_user);
echo json_encode("success");
}
這是我的Ajax JavaScript
$(document).ready(function(){
$("#registration-form").on('submit',function(e){
$.ajax({
url: base_url+"formsubmit/new_form_submit",
type: "POST",
data: $(this).serialize(),
success:function(data)
{
var result = JSON.parse(data);
if(result === "success")
{
$("h5").html("");
success_message("#success-message-new-account","Create Successful!");
window.setTimeout(function(){location.href=base_url},2000);
}
else if(result === "existing")
{
$("h5").html("");
success_message("#existing-message-account","You may use!".$username);
// window.setTimeout(function(){location.href=base_url},2000);
}
else{
$("#first_name_error").html(result.first_name_error);
$("#last_name_error").html(result.last_name_error);
$("#username_error").html(result.username_error);
$("#email_error").html(result.email_error);
$("#password_error").html(result.password_error);
}
},
error: function(data) {
alert('error');
}
})
e.preventDefault();
})
})
這是我的看法
<div id="existing-message-account"></div>
<div class="wrap-input100 validate-input">
<input class="input100" type="text" name="first_name" id="first_name">
<span class="label-input100">First</span>
</div>
<div class="wrap-input100 validate-input">
<input class="input100" type="text" name="last_name" id="last_name">
<span class="label-input100">Last</span>
</div>
用戶填寫完注冊表后。 這將在我的JavaScript中處理,現在將檢查注冊的用戶名是否已存在。 如果不是,它將保存在我的表中。 如果存在,則會添加數字前綴。
例
In my table users. I have existing username abcd, if the user register abcd then there would be a message "Username is already taken, you may use abcd1"
問題:如何將變量$ username傳遞到我的JavaScript中?
注意:我嘗試過這種方法,更改echo json_encode(“ existing”); 進入此回顯json_encode($ username)。 我的javascript else if(result === $ username)...該消息將不再起作用。
希望這個能對您有所幫助 :
對於existing
狀態記錄,請執行以下操作:
$data['username'] = $username;
$data['status'] = 'existing';
echo json_encode($data);
exit;
對於success
狀態記錄,返回如下所示
$data['username'] = $username;
$data['status'] = 'success';
echo json_encode($data);
exit;
您的ajax success
部分應具有以下代碼:
var result = JSON.parse(data);
if(result.status === "success")
{
$("h5").html("");
success_message("#success-message-new-account","Create Successful!");
window.setTimeout(function(){location.href=base_url},2000);
}
else if(result.status === "existing")
{
$("h5").html("");
success_message("#existing-message-account","You may use!" + result.username);
// window.setTimeout(function(){location.href=base_url},2000);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.