使用AJAX将表单数据传递给控制器​​,使用Codeigniter传递jquery

时间:2021-11-10 09:05:45

I am trying to post the data from this form into the database. I have tried some tutorials with no success. Here is my code. Any ideas?

我试图将此表单中的数据发布到数据库中。我尝试过一些没有成功的教程。这是我的代码。有任何想法吗?

View:

视图:

<form method="post" name="myForm1" id="myForm1" enctype="multipart/form-data" >
Email: <input type="text" name="email" id="email">
Question: <input type="text" name="qText" id="qText">
<input id="submitbutton" type="submit">
</form>

AJAX (in the view, right below the form)

AJAX(在视图中,在表单下方)

<script type='text/javascript' language='javascript'>

$("#submitbutton").click(function(){


$.ajax({
       url:'http://localhost:8888/index.php/trial/insert_into_db',
       type: 'POST',
       data: $("#myForm1").serialize(),
       success: function(){
           alert("success");
       },
       error: function(){
           alert("Fail")
       }
   });
   e.preventDefault();
});


</script>

Controller

调节器

  function insert_into_db(){
    $this->load->model('insert_db');
    $this->insert_db->insertQ();  
}

Model

模型

 class Insert_db extends CI_Model{

    function insertQ(){
        $email = $_POST['email'];
        $qText = $_POST['qText'];
        $this->db->query("INSERT INTO questions VALUES('','$email','$qText','','')");
    }
 }

2 个解决方案

#1


4  

As @David Knipe already said, you should wait for the DOM to be ready before trying to access one of its elements. Moreover, you likely have an e is undefined error, since you're not passing the event reference to the click handler:

正如@David Knipe所说,在尝试访问其中一个元素之前,您应该等待DOM准备好。此外,您可能有一个未定义的错误,因为您没有将事件引用传递给click处理程序:

<script>   //no need to specify the language
 $(function(){
  $("#submitbutton").click(function(e){  // passing down the event 

    $.ajax({
       url:'http://localhost:8888/index.php/trial/insert_into_db',
       type: 'POST',
       data: $("#myForm1").serialize(),
       success: function(){
           alert("success");
           $('#email').val('');
           $('#qText').val('');
       },
       error: function(){
           alert("Fail")
       }
   });
   e.preventDefault(); // could also use: return false;
 });
});
</script>

To be more complete, your query is vulnerable, and you might be getting an error there. Make use of the advantage of having a framework beneath you:

为了更完整,您的查询容易受到攻击,并且您可能会在那里收到错误。利用在您下方拥有框架的优势:

function insertQ(){
    $email = $this->input->post('email');
    $qText = $this->input->post('qText');
    $this->db->insert('questions', array('email' =>$email, 'text' => $text));
}

^_ I'm guessing the column names since you didn't specify them

^ _我猜测列名,因为你没有指定它们

#2


1  

Looks like you've forgotten to use $.ready. As it is, the javascript runs before the page has loaded, which means it can't find the page element from $("#submitbutton"). Try this:

看起来你忘了使用$ .ready。实际上,javascript在页面加载之前运行,这意味着它无法从$(“#submitbutton”)中找到页面元素。尝试这个:

$(document).ready(function() {
    $("#submitbutton").click(function(){
    ....
    ....
});

#1


4  

As @David Knipe already said, you should wait for the DOM to be ready before trying to access one of its elements. Moreover, you likely have an e is undefined error, since you're not passing the event reference to the click handler:

正如@David Knipe所说,在尝试访问其中一个元素之前,您应该等待DOM准备好。此外,您可能有一个未定义的错误,因为您没有将事件引用传递给click处理程序:

<script>   //no need to specify the language
 $(function(){
  $("#submitbutton").click(function(e){  // passing down the event 

    $.ajax({
       url:'http://localhost:8888/index.php/trial/insert_into_db',
       type: 'POST',
       data: $("#myForm1").serialize(),
       success: function(){
           alert("success");
           $('#email').val('');
           $('#qText').val('');
       },
       error: function(){
           alert("Fail")
       }
   });
   e.preventDefault(); // could also use: return false;
 });
});
</script>

To be more complete, your query is vulnerable, and you might be getting an error there. Make use of the advantage of having a framework beneath you:

为了更完整,您的查询容易受到攻击,并且您可能会在那里收到错误。利用在您下方拥有框架的优势:

function insertQ(){
    $email = $this->input->post('email');
    $qText = $this->input->post('qText');
    $this->db->insert('questions', array('email' =>$email, 'text' => $text));
}

^_ I'm guessing the column names since you didn't specify them

^ _我猜测列名,因为你没有指定它们

#2


1  

Looks like you've forgotten to use $.ready. As it is, the javascript runs before the page has loaded, which means it can't find the page element from $("#submitbutton"). Try this:

看起来你忘了使用$ .ready。实际上,javascript在页面加载之前运行,这意味着它无法从$(“#submitbutton”)中找到页面元素。尝试这个:

$(document).ready(function() {
    $("#submitbutton").click(function(){
    ....
    ....
});