2011-10-22 233 views
0

我需要從jquery/json的前端插入一個site_title,site_address(url)site_description & site_category到我的數據庫中..它插入的網站和更新前端罰款,如果我不把「http://」在site_address輸入字段,但一旦我這樣做,它不會inster到數據庫..爲什麼插入到沒有http://的數據庫中,但沒有插入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> 

控制器

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"); 
}); 

編輯 - 解決方案

首先,讓我說非常感謝馬歇爾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)); 

} 

我還要說,我認爲誰也下來投我的問題,不管他們站在有多高堆棧溢出社區中的居民需要多出去!我是一個noob,我還在學習,我不能去uni學習這個,我有時需要幫助..你從來沒有在那個階段? Pffft!

+0

你爲什麼編輯問題Jakub? –

+0

另外,你爲什麼把這個問題投下來? –

+3

Jakub改進了你的問題。請參閱[FAQ]的這一部分(http://stackoverflow.com/faq#editing)。 –

回答

1

這可能是因爲你沒有url編碼你的值,試試這個。

$.post("/usersites/addusersites", { 

    "account_id" : account_id, 
    "site_title" : site_title, 
    "site_address" : encodeURIComponent(site_address), 
    "site_description" : site_description, 
    "site_category" : site_category, 
}, 
+0

謝謝,但沒有工作..這真的很奇怪,如果我不填寫其他輸入欄的網站地址輸入,並填寫它與http://examplesite.com它的工作..然後作爲一旦我填寫其他輸入,它不會再工作..謝謝你的時間。 –