将数据从java脚本传递到codeigniter

时间:2015-05-21 05:59:37

标签: javascript php mysql codeigniter

我使用javascript php和mysql开发了一个事件管理系统。它在纯PHP中完美运行,但现在我需要将它迁移到codeigniter,并且需要一些关于如何在codeigniter中将数据从js传递到php的建议。

我的前端java脚本功能就像这样

// event creating
                dp.onTimeRangeSelected = function (args) {
                    var name = prompt("New event name:", "Event");
                    dp.clearSelection();
                    if (!name) return;
                    var e = new DayPilot.Event({
                        start: args.start,
                        end: args.end,
                        id: DayPilot.guid(),
                        resource: args.resource, //Change to classroom name
                        text: name //Change to event name
                    });
                    dp.events.add(e);

                    args.text = name;

                    DayPilot.request(
                        "backend_create.php", 
                        function(req) { // success
                            var response = eval("(" + req.responseText + ")");
                            if (response && response.result) {
                                dp.message("Created: " + response.message);
                            }
                        },
                        args,
                        function(req) {  // error
                            dp.message("Saving failed");
                        }
                    ); 
                };

处理create函数的php文件就像这样

<?php
require_once '_db.php';

$insert = "INSERT INTO events (name, start, end, resource) VALUES (:name, :start, :end, :resource)";

$stmt = $db->prepare($insert);

$stmt->bindParam(':start', $start);
$stmt->bindParam(':end', $end);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':resource', $resource);

$received = json_decode(file_get_contents('php://input'));

$start = $received->start;
$end = $received->end;
$resource = $received->resource;
$name = $received->text;
$stmt->execute();

class Result {}

$response = new Result();
$response->result = 'OK';
$response->message = 'Created with id: '.$db->lastInsertId();

echo json_encode($response);

?>

现在,在迁移到codeignitor时,我已将backend_create.php文件隔离到模型和控制器中,它看起来像这样。

控制器部分

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class TimecalCon extends CI_Controller {

    function __construct()
    {
        parent::__construct();
         $this->load->model("Timecal_model");
    }
public function insert()
    {
        $received = json_decode(file_get_contents('php://input'));

        $start = $received->start;
        $end = $received->end;
        $resource = $received->resource;
        $name = $received->text;        
        $this->Timecal_model->InsertDetails($name, $start, $end, $resource);       

    }

模型

<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Timecal_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    function InsertDetails($name, $start, $end, $resource)
    {

        $insert = "INSERT INTO events (name, start, end, resource) VALUES (:name, :start, :end, :resource) ";       
        $query = $db->prepare($insert);
        $stmt->bindParam(':start', $start);
        $stmt->bindParam(':end', $end);
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':resource', $resource);
        $stmt->execute();
        class Result {}
        $response = new Result();
        $response->result = 'OK';
        $response->message = 'Created with id: '.$db->lastInsertId();
        return json_encode($response);
    }

问题是当我在视图页面中更改javascript并像这样使用它时

.....
 DayPilot.request(
                        "TimecalCon/insert", .......

功能中断,我无法将事件插入数据库。在这种情况下,我应该如何将数据从js传递给控制器​​?

2 个答案:

答案 0 :(得分:1)

我们可以使用Ajax将值从javascript发送到控制器。我有一些可以帮助你的代码。

 function deleteEmp(empid){

        var base_url = '<?php echo site_url(); ?>';
        var r=confirm("Do You Really Want to Delete? ")
        if (r==true)
        {
            objPost= new Object();
            objPost.empid   = empid;
            $.ajax({
                url:"employee_registration/deleteEmp?empid="+empid,
                type:"POST",
                data:objPost,
                beforeSend:function(data){
                },
                error:function(data){
                },
                success:function(data){
                    alert(data);
                    result=JSON.parse(data);
                    alert(result);
                    if(result.status == 'success'){
                        alert('Deleted Successfully ');
                        window.location.reload();
                        return false;

                    }

                }

            });
        }else{
            return false;
        }
    }

正如你所看到的,我已经使用ajax将我的视图中的empid传递给控制器​​,这使得我的结果返回变量。在这种情况下是json。

答案 1 :(得分:0)

试试这个

DayPilot.request("<?php echo base_url().'TimecalCon/insert';?>",...)

您必须在配置文件夹下的“autoload.php”中添加“url”,然后检查正在加载的网址是否正确。尝试修改base_url()有点像添加或删除url中的“index.php”部分。

希望这会有所帮助