在没有刷新页面的情况下保存蛋糕php中

时间:2017-08-22 09:42:14

标签: cakephp

有人请帮助我"如何在没有刷新页面的情况下在cakephp中保存数据?"你可以帮助我另一个例子。实际上我一直试图在博客上发表评论。

// My index.ctp page code is like this . please suggest the modifications.

<?php
echo $this->Form->create('Test', array('url' => array('controller' => 'tests', 'action' => 'index')));
echo $this->Form->input('comment', array('type' => 'text'));
echo $this->Form->submit('submit', array('id' => 'btPrice'));
echo $this->Form->end();
?>

<div id="dsg">

</div>

<?php echo $this->Html->script('jquery-3.2.1.min'); ?>
<script>
    $(document).ready(function () {
//do sth when click on #btPrice
        $('#btPrice').click(function () {
            $.ajax({
                type: "POST",
                //the function u wanna call
                url: "<?php echo $this->Html->url(array('controller' => 'tests', 'action' => 'index')); ?>",
                /* data you wanna pass, as your $param1 is serverSide variable, 
                 you need to first assign to js variable then you can pass: 
                 var param1 =      '<?php echo $param1; ?>';*/
                data: {myVal: 'Success!'},
                success: function () {
                    alert('AjaX Success')
                },
                error: function () {
                    alert('AjaX Failed')
                }
            });
        });
    });
</script>

My controller page code looks like this

public function index() {

        if($this->request->is('post')) { //pr($this->request->data);exit;
           $myVal = $this->request->data;
            $data = $_POST['myVal'];
            pr($myVal);exit;
        $this->Test->save($data);
        $this->autoRender = false;
        }
    }

我以前没有这样做过。我使用的代码是来自某个地方的副本

2 个答案:

答案 0 :(得分:0)

第1步

data: {myVal: 'Success!'},
//Get your input field `comment` and pass in above line.

第2步 获取此comment变量值并在控制器中处理。

第3步 成功时,您可以打印成功或失败

第4步 你可以添加条件,如果成功然后警告 - 成功否则一些错误。

我希望这可以帮助你做到可行。

答案 1 :(得分:0)

<script>
    $('#btPrice').click(function () {
        var comment = $("#comment_input_id").val();
        $.ajax({
            type: "POST",
            //the function u wanna call
            url: "<?php echo $this->Html->url(array('controller' => 'tests', 'action' => 'index')); ?>",
            data: {comment: comment/* you can send additional data as post id etc to be saved */},
            dataType: 'json',
            success: function (response) {
                if (response.success) {
                    // do something after success
                } else {
                    // show response.message
                }
            },
            error: function () {
                alert('AjaX Failed')
            }
        });
    });
</script>

此处的控制器代码

<?php

function index() {
    $this->autoRender = false;

    $response["success"] = false;
    $response["message"] = "Server error";
    if ($this->request->is('post')) {
        $data = array();
        $data["comment"] = $this->request->data["comment"];
        if ($this->Test->save($data)) {
            $response["success"] = true;
            $response["message"] = "Saved successfully";
        }
    }

    echo json_encode($response);
    die;
}
?>