提交表单并使用opencart在alertbox中显示结果

时间:2017-07-24 08:13:26

标签: php jquery opencart

我需要在opencart中生成警告框。这意味着一旦我提交数据,我需要在alertbox中显示结果

这是我的view.tpl文件

<script type="text/javascript">
    $('#submit').submit(function() { 
    $.ajax({ // create an AJAX call...
        data: $(this).serialize(),
        type: $(this).attr('method'),
        url: $(this).attr('action'), 
        success: function(response) {
            $('#submit').html(response);
        }
    });
    return false; 
});
</script>

<form action="<?php echo $action; ?>" method="post">
<input type="text" name="your_name"><br>
<input type="text" name="email"><br>
<input type="submit" value="submit" name="submit">
</form>

这是我的控制器文件

public function index() 
{ 
    $this->load->model('test/test');
    $this->data['action'] = $this->url->link('test/test');
    $data['footer'] = $this->load->controller('common/footer');
    $data['header'] = $this->load->controller('common/header');     
    $this->response->setOutput($this->load->view('test/test', $data));  
    if(isset($_POST['submit']) )
    {
        $your_name = $_POST["your_name"];
        echo $your_name;
        $email =$_POST["email"];
        echo $email;
    }

1 个答案:

答案 0 :(得分:0)

您需要在控制器中以JSON身份返回。

e.g return json_encode($var);

像这样的东西。

public function index() { 
    $this->load->model('test/test');
    $this->data['action'] = $this->url->link('test/test');
    $data['footer'] = $this->load->controller('common/footer');
    $data['header'] = $this->load->controller('common/header');

    $this->response->setOutput($this->load->view('test/test', $data));  
    $result = [];
    if( isset($_POST['submit']) ){
        $your_name = $_POST["your_name"];
        $email =$_POST["email"];
        $result['yourname'] = $your_name;
        $result['email'] = $email;
    }
    return json_encode($result);
}

然后,你的js中console.log(response);