如何将AJAX数据从VIEW发送到控制器? (PHP(MVC)+ AJAX) [英] How to send AJAX data from VIEW to CONTROLLER? (PHP(MVC)+AJAX)

查看:121
本文介绍了如何将AJAX数据从VIEW发送到控制器? (PHP(MVC)+ AJAX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有http://visiting/blog页.

控制器包含action_index和add_index方法. Action_index()返回索引页面. Add_index()调用模型的方法add_data(),该方法将数据从表单插入数据库.

Controller contains action_index and add_index methods. Action_index() return indexes pages. Add_index() call model's method add_data(), which insert data from form to the database.

我需要使用ajax-request组织我的应用程序,提交表单后该http://visiting/blog页面不刷新.

I need organize my application with ajax-request, that http://visiting/blogpage not to refresh after submitting the form.

查看

    $.ajax({
        type: 'POST',
        url: '???',   --------------------------> What should URL contain?
        data: $(this).serialize()

CONTROLLER

    function action_add() {

        $title = $this->cleanStr($_POST["title_field"]);
        $text = $this->cleanStr($_POST["text_field"]);



        if ($title!="" && $text!="") {
            $this->model->add_data($title, $text);              
        } else {
            throw new Exception("Data is empty");
        }

    }

模型

   public function add_data($title, $text) {
        try {
            $query="INSERT INTO post (title, text) VALUES('$title', '$text')";
            self::$db->query($query);
        } catch(Exception $e) {
            echo $e->getMessage();
        }
    }


查看
这是一个带有ajax请求的完整html文件. 我要处理表单,该页面不会刷新,并且数据会发送到数据库.


VIEW
It is a full html file with ajax-request. I want to handle form, that the page isn't refreshed and data is sent to the database.

  <div class="blog">
    <h1> Blog </h1>
    <form onsubmit="return validate()" id="add_form">
        <fieldset>
            <legend>Add new post:</legend>
            <label>Title:</label><br>
            <input type="text" name="title_field" id="titlef">
            <br>
            <label>Text:</label>
            <br>
            <textarea name="text_field" id="textf"></textarea>
            <br>
            <input onclick="return validate(); return false" type="submit" value="Submit">
            <input onclick="return resetclass()" type="reset" value="Reset">
        </fieldset>
    </form>

    <div class="blogposts">
        <div id='response'></div>
        <?php
            foreach ($data as $values) {
                echo "<div class=\"blog_item\">";
                echo "<h4 class=\"blog_item_title\">" . $values["title"] . "</h4>" . 
                "<div class=\"blog_item_text\">" . $values["text"] . "</div>" .
                "<div class=\"blog_item_time\">" . $values["time"] . "</div>";
                echo "</div>";
            }
        ?>
    </div>
</div>
<script>
$(document).ready(function(){
    $('#add_form').submit(function(){

        // show that something is loading
        $('#response').html("<b style=\"font-size:20px; margin:40px;\"\">Loading ...</b>");


        $.ajax({
            type: 'POST',
            url: '???',   ------------> What should be into url?
            data: $(this).serialize()
        })
        .done(function(data){

            // show the response
            $('#response').html(data);

        })
        .fail(function() {

                // just in case posting your form failed
                alert( "Posting failed." );

        });
        // to prevent refreshing the whole page page
        return false;

    });
});
</script>

推荐答案

URL应该是您要命中的控制器方法的路径.您不必在url中包含基本路径(但是您可以,如果需要).像这样:

the url should be the path to your controller method you would like to hit. you do not have to include the basepath in the url (but you can if you want to). so something like:

url: "howeverYourStructureIs/Action_index",

点击方法action_index().您可以将ajax看作是就像您要访问页面一样,但实际上并没有导航到该页面".因此,通常您会使用该方法,就是您在ajax调用中输入的网址.

to hit method action_index(). You can think of ajax like "as if you were to hit the page, but not actually navigating to that page" kinda thing. So however you would normally hit that method, is the url you put in the ajax call.

希望这会有所帮助

这篇关于如何将AJAX数据从VIEW发送到控制器? (PHP(MVC)+ AJAX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆