我需要插入site_title,site_address(网址)site_description& site_category从前端使用jquery / json进入我的数据库..如果我没有在site_address输入字段中输入“http://”,它会插入网站并更新前端,但是只要我这样做,它就会不是对数据库的帮助..
我有这段代码
主页视图中的表格:
<form action="" method="post" class="addsite_form">
<input id="account_id" type="hidden" value="<?php echo $this->session->userdata('account_id');?>"/>
<label for="site_title"><strong>Site Name:</strong></label>
<input id="site_title" type="text" maxLength="25" tabindex="1" name="site_title" />
<label for="site_address"><strong>Site Address:</strong></label>
<input id="site_address" type="text" tabindex="2" name="site_address" />
<label for="site_description"><strong>Site Description:</strong></label>
<input id="site_description" type="text" tabindex="3" name="site_description" />
<label for="site_category"><strong>Site Category:</strong></label>
<input id="site_category" type="text" tabindex="4" name="site_category" />
<input id="addsite_form_submit" type="submit" value="Add Site" name="submit" tabindex="5" />
</form>
CONTROLLER
public function addUserSites() {
$account_id = $this->input->post('account_id');
$site_title = $this->input->post('site_title');
$site_description = $this->input->post('site_description');
$site_address = $this->input->post('site_address');
$site_category = $this->input->post('site_category');
$array = array(
'account_id' => $account_id,
'site_title' => $site_title,
'site_description' => $site_description,
'site_address' => $site_address,
'site_category' => $site_category,
);
echo json_encode($this->usersites_model->insertsites($array));
}
MODEL:
public function insertsites($data) {
$this->db->insert('usersites', $data);
return TRUE;
}
JQUERY
$('.addsite_form').submit(function() {
var account_id = $("#account_id").val();
var site_title = $("#site_title").val();
var site_address = $("#site_address").val();
var site_description = $("#site_description").val();
var site_category = $("#site_category").val();
$.post("/usersites/addusersites", {
"account_id" : account_id,
"site_title" : site_title,
"site_address" : site_address,
"site_description" : site_description,
"site_category" : site_category,
},
function(data){
}, "json");
});
编辑 - 解决方案
首先,我要非常感谢 Marshall Brekka 谁建议使用encodeURIComponent ..我现在已经阅读了它并找到了php版本..
我已将urlencode添加到我的控制器中,它现在可以运行..这是新控制器
public function addUserSites() {
$account_id = $this->input->post('account_id');
$site_title = $this->input->post('site_title');
$site_description = $this->input->post('site_description');
$site_address = $this->input->post(urlencode('site_address'));//added urlencode
$site_category = $this->input->post('site_category');
$array = array(
'account_id' => $account_id,
'site_title' => $site_title,
'site_description' => $site_description,
'site_address' => $site_address,
'site_category' => $site_category,
);
echo json_encode($this->usersites_model->insertsites($array));
}
我可以说,我认为那些投票的人投了我的问题,无论他们站在筹码溢出社区有多高,都需要更多!我是一个菜鸟,我还在学习,我不能去大学学习这个,我有时需要帮助..你是不是在那个阶段? Pffft!
答案 0 :(得分:1)
可能是因为您没有对您的值进行网址编码,请尝试此操作。
$.post("/usersites/addusersites", {
"account_id" : account_id,
"site_title" : site_title,
"site_address" : encodeURIComponent(site_address),
"site_description" : site_description,
"site_category" : site_category,
},