繁体   English   中英

提交表单并使用opencart在警报框中显示结果

[英]Submit a form and display the result in alertbox using opencart

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

这是我的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;
    }

您需要在控制器中以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);
}

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

暂无
暂无

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

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