简体   繁体   English

表单需要在codeigniter中将数据发布到控制器

[英]Form needs to post data to a controller in codeigniter

I have a form, 我有一张表格,

<form>
<input id="amount"  type="text" placeholder="Give amount"  name="myamount"/>
<button class="btn btn-lg" type="submit" id="btn1" >FirstBTN</button> 
<button class="btn btn-lg" type="submit" id="btn2">SecondBTN</button>
</form>

Which needs to post data to a function inside the controller. 哪个需要将数据发布到控制器内的函数。 I am trying to make two buttons, post data to two different controller functions. 我正在尝试制作两个按钮,将数据发布到两个不同的控制器功能。 And this is my jquery code written in html file (view): 这是我在html文件(view)中编写的jquery代码:

<script type="text/javascript">
$('#btn1').click(function() { 
        $.ajax({
            url: 'helloworld/firstmoney',
            type: 'POST',
            data: {'submit' : true ,myamount: $("#amount").val()},
            success: function (result) {

            }
        });  
 });

</script>

<script type="text/javascript">
    $('#btn2').click(function() { 
            $.ajax({
                url: 'helloworld/secondmoney',
                type: 'POST',
               data: {'submit' : true ,myamount: $("#amount").val()},
                success: function (result) {

                }
            });  

    });

    </script>

Here is the controller but I am not getting any values in my controller.. 这是控制器,但我的控制器中没有任何值。

public function firstmoney(){
        $this->load->helper('form');

        $Money = $this->input->post("myamount");
        $data =  array(
        'Money' => $Money
         );
        $this->load->view("page1", $data);

    }

public function secondmoney(){
            $this->load->helper('form');

            $Money = $this->input->post("myamount");
            $data =  array(
            'Money' => $Money
             );
            $this->load->view("page2", $data);

        }

my controller name is helloworld and I need to assign values in $money but I cant get it working. 我的控制器名称是helloworld,我需要以$ money分配值,但我不能让它工作。 I am a beginner programmer so please do tell me what steps I should follow. 我是初学程序员,所以请告诉我应该遵循的步骤。

First, your AJAX should look like this one. 首先,你的AJAX看起来应该是这样的。

    $('#btn2').click(function() { 
        $.ajax({
            url: 'helloworld/secondmoney',
            method: 'POST',//not type
            data: { myamount: $("#amount").val() }//no need for submit variable (although you can use it) since this will be submitted on click; no quotes over variables
        })
          .done(function(result)) {
              //do your stuff on response
          });
    });

Second: no need for ajax and you should use classic form with post method because you are making redirection in controller/method where data is posted to. 第二:不需要ajax,你应该使用经典形式和post方法,因为你在控制器/方法中进行重定向,其中数据被发布到。 AJAX requires JSON formated response, and it is not in this case. AJAX需要JSON格式化响应,在这种情况下不是这样。 So wether change view to have form with method and action, wether change controller/method to return JSON string back to AJAX. 因此更改视图以具有方法和动作的形式,更改控制器/方法以将JSON字符串返回到AJAX。

 var base_url= '<?php echo base_url() ?>';


 url: base_url+'helloworld /firstmoney',

You can try another way to do that 你可以尝试另一种方式来做到这一点

Try below code 试试下面的代码

form 形成

<form action="" method="post" id="myform">
<input id="amount"  type="text" placeholder="Give amount"  name="myamount"/>
<button class="btn btn-lg" type="submit" id="btn1" >FirstBTN</button> 
<button class="btn btn-lg" type="submit" id="btn2">SecondBTN</button>
</form>

Then you can add jquery to change the form action on submit button clicks like below 然后,您可以添加jquery以更改提交按钮单击时的表单操作,如下所示

<script type="text/javascript">

$('#btn1').click(function() { 
        $('#myform').attr('action', 'Your domain/Your controller/firstmoney'); 
 });

$('#btn2').click(function() { 
        $('#myform').attr('action', 'Your domain/Your controller/secondmoney'); 
 });

</script>

Now your form will call firstmoney() if you click #btn1 and it will call secondmoney() if you call #btn2 现在你的表单会调用firstmoney(),如果你单击#btn1,如果你调用#btn2它会调用secondmoney()

And you can get form data using 您可以使用获取表单数据

$this->input->post('myamount');

If you only want to print $_POST data then apply this code for btn1. 如果您只想打印$ _POST数据,请将此代码应用于btn1。

$(document).ready(function(){
    $('#btn1').click(function() { 
    event.preventDefault();

        $.ajax({ 

            url: "<?php echo base_url(); ?>" + "helloworld/firstmoney",         
            type: 'POST',
            data: {'submit' : true ,myamount: $("#amount").val()},
            success: function (result) { 
                $("body").html(result);
            }
        });  
    });
});

In your helloworld controller 在你的helloworld控制器中

public function firstmoney(){

    $Money = $this->input->post("myamount");
    $data =  array(
    'Money' => $Money
     );
    $data = $this->load->view("page1", $data, TRUE); //it will return that page and save it in $data 
}

In you page1.php view write the following code 在page1.php视图中编写以下代码

<?php
echo $Money;
?>
<script type="text/javascript">
$('#btn1').click(function() { 
        $.ajax({
            url: 'helloworld/firstmoney',
            type: 'POST',
            data: {'submit' : true ,myamount: $("#myamount").val()},
            success: function (result) {

            }
        });  
 });

Keep the id and name value as myamount in above input tag and change in the jquery code also 将id和name值保存为上面输入标记中的myamount,并更改jquery代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM