【问题标题】:couldnt store data into db with ajax request and symfony 3无法使用 ajax 请求和 symfony 3 将数据存储到数据库中
【发布时间】:2017-06-29 12:11:23
【问题描述】:

我想在输入文本中输入文本并获取此值并将其存储在我的数据库中,并在我的视图中使用包含输入数据的 jquery 在我的视图中添加一个 div

这是我的输入表单

    <div class="panel rounded shadow">
    <form method="post">
                    <textarea id="txt" class="form-control input-lg no-border" rows="2"
                              placeholder="What are you doing?..."></textarea>
    </form>
    <div class="panel-footer">
        <button class="btn btn-success pull-right mt-5" id="btnpost">POST</button>
        <ul class="nav nav-pills">
            <li><a href="#"><i class="fa fa-map-marker"></i></a></li>
            <li><a href="#"><i class="fa fa-camera"></i></a></li>
            <li><a href="#"><i class="fa fa-smile-o"></i></a></li>
        </ul><!-- /.nav nav-pills -->
    </div><!-- /.panel-footer -->
</div>

我的输入文字是这样的

这是我的脚本

   <script>
            $(document).ready(function() {
                $("#btnpost").click(function () {
                    var text = $('#txt').val();
                    $.ajax({
                        type: 'POST',
                        url: '{{ path('group_addpub') }}',
                        data: {desc: text},
                        complete: function () {
                            $("#publication").prepend('<div class="panel panel-success rounded shadow" style="text-align: left;margin-bottom: 5px;">' +
                                    '<div class="panel-heading no-border">'+
                                    '<div class="pull-left half">'+
                                    '<div class="media" style="text-align: left;">'+
                                    '<div class="media-object pull-left" style="margin-top: 35px;">'+
                                    '<img src="http://bootdey.com/img/Content/avatar/avatar2.png" style="width: 40px;height: 40px;">' +
                                    '</div>'+
                                    '</div>'+
                                    '</div>'+
                                    '<a href="">test profile</a>'+
                                    '<span class="text-white h6" style="display: block; color: black;">on 8th June, 2014</span>'+
                                    '<br>'+
                                    '<span style="color: black;margin-bottom: 10px;word-break: break-all  ">'+text+ '</span>'+


                                    '</div>'+
                                    '</div>'+
                                    '<div class="panel-footer">'+

                                    '<form action="#" class="form-horizontal">'+
                                    '<div class="form-group has-feedback no-margin">'+
                                    '<div style="text-align: right;margin-top: 32px;">'+
                                    '<a href=""><img src="{{ asset('Groupe/img/like-icon.png') }}" style="width:5%;"></a>'+
                                    '<a href="" ><img src="{{ asset('Groupe/img/Unlike-icon.png') }}" style="width:5%;"></a>'+
                                    '</div>'+
                                    '<input class="form-control" type="text" placeholder="Votre commentaire ici..." style="width: 95%;margin-left: 10px;">'+
                                    '</div>'+
                                    '</form>'+
                                    '</div>');


                            $('#txt').val('');

                        }
                    });

                });
            });
        </script>

这是我的控制器操作

public function addpubicationAction(Request $request)
{

    if ($request->isXmlHttpRequest() && $request->isMethod('post') ) {
        $publication = new Publication();
        $des = $request->get('desc');
        var_dump($des);
        $publication->setDescription($des);
        $em = $this->getDoctrine()->getManager();
        $em->persist($publication);
        $em->flush();
        $this->redirectToRoute('group_group_membres');
    }


}

我的 jquery 脚本运行良好,它可以正确获取输入并将其添加到 div 元素 ]3

但我的问题是我的数据没有存储到数据库中,当我刷新页面时,jquery 前面的所有内容都消失了

【问题讨论】:

  • 你得到$request->get('desc')中的值了吗? ?
  • 尝试使用查看源代码来检查您的模板是否按预期呈现。

标签: php jquery ajax symfony


【解决方案1】:

您应该将 id="txt" 添加到您的 &lt;input type="text"&gt; 标记。

你应该改变这个 $request-&gt;isMethod('post') 有了这个 $request->isMethod('POST')

最好的问候。

【讨论】:

  • 你说的是哪个标签?
  • 在我的代码中它是真的我已经设置了它,我的脚本正在从输入中获取数据,但它没有将数据发送到控制器以将它们存储到 db
  • 你的action方法有注解吗?你能把它完整地发布吗?
  • 脚本是它发布的方法
【解决方案2】:

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2021-02-28
    • 2022-07-28
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多