【问题标题】:store form data in mysql database using ajax in codeigniter在codeigniter中使用ajax将表单数据存储在mysql数据库中
【发布时间】:2015-02-23 16:26:15
【问题描述】:

编辑:

这是 localhost 的问题,我认为是 htaccess 文件。尽管我无法在 localhost 中运行,但该脚本在 Web 主机上运行良好。


我想在 codeigniter 中使用 ajax 将表单数据存储到数据库中。问题是一切都很好,除了我收到 500 服务器内部错误。

我的控制器:

public function order()
{
    $order = $this->main_model->order($_POST);
    if($order)
    {                
        return true;
    }  
    else 
    {  
        return false;
    }
}

我的模特:

function order($options = array())
{

    $options = array(

    'client_Name' => $this->input->post('oName'),

    'client_Phone' => $this->input->post('oPhone')

    );

    $this->db->insert('md_orders', $options);

    return $this->db->insert_id();
}

当然我正在使用 stepsForm 脚本,这是我拥有的 js 代码:

var theForm = document.getElementById( 'theForm' );
new stepsForm( theForm, {
    onSubmit : function( form ) {
        var form_data = {
            oName: $('#oName').val(),
            oPhone: $('#oPhone').val(),
        };

        $.ajax({
            url: "<?php echo base_url() . 'main/order/'; ?>",
            type: 'POST',
            data: form_data,
            success: function(msg) {
                  alert(msg);
            }

       });

    }
} );

这是 HTML 代码:

<form id="theForm" class="simform" autocomplete="off">
    <ol class="questions" id="questions">
        <li>
            <span><label for="oName">Your name:</label></span>
            <input class="finput" id="oName" name="oName" type="text"/>
        </li>
        <li>
            <span><label for="oPhone">Your Phone Number:</label></span>
            <input class="finput ltr" data-validate="number" id="oPhone" name="oPhone" type="text"/>
        </li>
    </ol>
</form>

我得到的错误是:

 POST http://localhost/123/main/order/ 500 (Internal Server Error)

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

并且没有任何内容存储在数据库中。我做错了什么?!

【问题讨论】:

  • 您的 js 代码是在单独的 .js 文件中还是在您的视图中?
  • 它在视图内

标签: php jquery mysql ajax codeigniter


【解决方案1】:

尝试在控制器中检查您的输入,然后再将其发送到模型,尽管这不是强制性的。在您的模型中,您只需要输入数组即可。所以你可以只设置参数的名称。 你的模型是这样的:

public function order($options) {
    $data = array();
    $data['client_Name'] = $options['oName'];
    $data['client_Phone'] = $options['oPhone'];

    $this->db->insert('md_orders', $data);
    if($this->db->affected_rows() > 0) {
        return $this->db->insert_id();
    else {
         return false;
     }
 }

你的控制器是这样的:

public function order() {
    $order = $this->main_model->order($this->input->post());
        if( $order !== false ) {                
            echo "Inserted!";
        } else {
            echo "Not inserted!";//These are your ajax success function msg parameter
        }
}

另外,我在 onSubmit 属性的function( form ) 中看不到form 参数的逻辑。也许你可以删除它?

【讨论】:

  • 没有任何效果?在降级我帮助你的努力之前,你应该给出一些解释。
【解决方案2】:

在您看来,请将以下代码放入您的脚本中,请...

 one_field      = $('#one_field').val();
    second_field   = $('#second_field').val();
    $.ajax({
         type:"post",
         data:{one_field:one_field, second_field:second_field},
         url :'<?=base_url("directory/controller_name/your_method_name")?>',
         success: function(data)
         {
            alert("success");
         }
     });

现在将以下内容放入您的控制器中。

function your_method_name()
{

    $your_data = array(
        "table_field_1" => $this->input->post('one_field'),
        "table_field_2" => $this->input->post('second_field')
    );

    $last_id = $this->your_model->insert_data_function($your_data);
    if(isset($last_id)
    {
     //your code
    }
    else
    {
      //your code
    }
  }

在您的模型中,放置以下内容。

public function insert_data_function($your_data)
{
    $this->db->insert("your_table",$your_data);
    return $this->db->insert_id(); //will return last id
}

【讨论】:

    【解决方案3】:

    不要在你的 JS 中使用 base_url(),你应该尝试使用 site_url()

    您的 CodeIgniter 网站通过 index.php 文件运行。

    base_url() 函数会将 URL 返回到您的基本目录。

    site_url() 会将 URL 返回到您的 index.php 文件。

    您的表单在尝试访问“http://localhost/123/main/order”时却尝试访问“http://localhost/123/index.php/main/order

    希望这会有所帮助。

    编辑

    您还应该看看表单助手。

    https://ellislab.com/codeIgniter/user-guide/helpers/form_helper.html

    【讨论】:

    • 我不知道,即使您听说过使用 htaccess 文件删除 index.php 并修改 $config['index_page'] = '';。当然,你说的解决方案不是我要找的那个。
    猜你喜欢
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多