简体   繁体   English

关于Symfony的Fullcalendar,通过模态添加/修改事件,并将Json文件发送回数据库

[英]Fullcalendar on Symfony add/modify events through modal and send back Json file to Database

I am working on a project with Symfony 2.8. 我正在使用Symfony 2.8进行项目。 My main goal is to create a dynamic calendar based on Fullcalendar library. 我的主要目标是基于Fullcalendar库创建动态日历。

I add my events called "dispos" (avalabilities in English) and "Rdvs" (appointments" in English) through a Json request and ajax. This works fine. 我通过Json请求和ajax添加了名为“ dispos”(英语)和“ Rdvs”(英语)的事件。

Now, I would like to transform availabilites into appointements (which are both considered as events in Fullcalendar). 现在,我想将可用职位转换为任命(在Fullcalendar中都被视为事件)。

Eg : When someone clicks on one availability a modal shows up, then the person fills the form in it and clicks "save" button. 例如: 当某人单击一个可用性时,将显示一个模式,然后该人在其中填写表格并单击“保存”按钮。 When the "save" button is clicked, all informations entered in the form are sent and saved (through a Json request) into my Database and the appointment is taken --> all events of the current should be reloaded through ajax, the event should be displayed with the title of the event entered (name of the patient) and the modal should contain all informations given/wrote before "save" action. 单击“保存”按钮后,将在表单中输入的所有信息发送并保存(通过Json请求)到我的数据库中,并进行约会-> 当前的所有事件应通过ajax重新加载,该事件应显示带有输入事件标题(患者姓名)的信息,并且模态应包含“保存”操作之前给出/编写的所有信息。

I tried to do it but my ajax is not working since events do not reload after saving everything else is working. 我尝试这样做,但是我的ajax无法正常工作,因为在保存所有其他内容之后, 事件不会重新加载

Anyway, I think I did it wrong somewhere. 无论如何,我认为我在某处做错了。 The code I will show you in my Controller returns a view because I didn't manage to return a response (+ I think routing or something is bad but don't know how to fix it...) 我将在Controller中显示的代码返回一个视图,因为我没有设法返回响应(+我认为路由或某些问题很糟糕,但不知道如何解决...)

Any clue or advice woud be really appreciated :) 任何线索或建议将不胜感激:)

So here is my code : 所以这是我的代码:

  1. TakeRdvAction in my controller : 我的控制器中的TakeRdvAction:

    /* ----------------- / / --- TAKE RDV ---- / / ----------------- */ / * ----------------- / / ---接受RDV ---- / / ----------------- * /

     public function takeRdvAction(){ $request = $this->get('request_stack')->getCurrentRequest(); parse_str($request->getContent(), $myArray); /*$request->isXmlHttpRequest()*/ if (1) { $dateHeureDispo=$myArray['heureDispo']; $dateDispo= new \\DateTime($dateHeureDispo); $heureDispo = $dateDispo->format('H:i'); $dateDispo=$dateDispo->format('dm-Y'); $civilite=$myArray['civilite']; $nom=$myArray['inputNom']; $prenom=$myArray['inputPrenom']; $naissance=$myArray['birth']; $email=$myArray['email']; $tel=$myArray['tel']; $telFixe=$myArray['telFixe']; $adresse=$myArray['adresse']; $cp=$myArray['cp']; $ville=$myArray['ville']; $pays=$myArray['pays']; $medecin_traitant=$myArray['medecin_traitant']; $ame=$myArray['ame']; $cmu=$myArray['cmu']; $takeRDv="http://connect.mysite.com/nameofapi2/takeappt?center=13&motive=238&prenom=".urlencode($prenom)."&nom=".urlencode($nom)."&email=".urlencode($email)."&birthdate=".$naissance."&address=".urlencode($adresse)."&cp=".$cp."&city=".urlencode($ville)."&country=".urlencode($pays)."&tel=".$tel."&titre=1&source=1&origine=1&daterdv=".$dateDispo."&time=".$heureDispo."&slot=1%E1%90%A7&civilite=".$civilite."&origin=smwh&referer=1"; $streamContext = stream_context_create([ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]); $json = file_get_contents($takeRDv, false, $streamContext); $response = new jsonResponse(); $response->setContent($json); return $this->indexAction(); } else { return new response("Ajax request failed"); } 

    } }

If I put if ($request->isXmlHttpRequest()), the controller goes directly to "else" end returns "Ajax request failed" 如果我把if($ request-> isXmlHttpRequest())放进去,则控制器直接转到“ else”端,返回“ Ajax请求失败”

  1. Ajax.js file (It's the last ajax function we are talking about): Ajax.js文件(这是我们正在讨论的最后一个ajax函数):

     $(document).ready(function () { /* TakeRdvs */ $("#monBouton").click(function(){ if (nom.value != "" && prenom.value != "" && email.value != "") { $.ajax({ url: "{{ path('takeRdv') }}", method: 'POST', data: {}, success: function(data) { $("#calendarModal").modal('hide'); $("#calendarModal").on('hidden.bs.modal', function (e) { $('#calendar').fullCalendar('refetchEvents'); }); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert('Error: ' + errorThrown); } }); } else if (nom.value == "") { alert('Veuillez renseigner le Nom'); return false; } else if (prenom.value == "") { alert('Veuillez renseigner le prénom'); return false; } else if (email.value == "") { alert("Veuillez renseigner l'adresse mail"); return false; } }); 

    }); });

Other ajax functions work just fine, I made them after trying to take an appointment on an availability. 其他ajax函数也可以正常工作,我在尝试可用性后就做了它们。 When I implemented FosJsRouting, I thought it would be easier to try to make my takeRdvs action work. 当我实现FosJsRouting时,我认为尝试使takeRdvs操作更容易。 But the truth is, I don't know how to do it since it's a different action from the others and I am lost now :'( 但事实是,我不知道该怎么做,因为这是与其他行动不同的行动,我现在迷失了:'(

  1. My modal showing up when a event is clicked (got cut in several part sorry could not fix it): 单击事件时我的模态显示(切成几部分,抱歉无法修复):

    × close ×关闭

      <div class="form-group"> <div class="col-sm-12"> <h4 id="modalTitle" class="modal-title modify"></h4> </div> </div> <div class="col-sm-4 form-group"> <label for="motif"> Motif de la consultation : </label> <select class="form-control" id="motif" data-placeholder="Choisissez un motif" style="width: 100%;" name="motif"> {# multiple data-max-options="1" #} <option value="238"> Bilan de la vue</option> <option value="Visite de controle"> Visite de contrôle</option> <option value="Chirurgie réfractive"> Chirurgie réfractive</option> <option value="Rééducation visuelle"> Rééducation visuelle</option> <option value="Urgences"> Urgences</option> </select> </div> <div class="form-group create"> <div class="col-sm-2"> <label class="control-label" for="civilite">Civilité</label> <select class="custom-select" id="civilite" name="civilite"> <option value="Mme">Mme</option> <option value="M">M.</option> </select> </div> <div class="col-sm-5"> <label class="control-label" for="inputNom">Nom</label> <input name="inputNom" type="text" class="form-control" id="inputNom" placeholder="Doe" required > </div> </div> <div class="form-group"> <div class="col-sm-5 create"> <label class="control-label" for="inputPrenom">Prénom</label> <input name="inputPrenom" type="text" class="form-control" id="inputPrenom" placeholder="Jane" required > </div> </div> <div class="form-group"> <div class="col-sm-6"> <label class="control-label" for="email">Email</label> <input name="email" type="email" class="form-control" id="email" placeholder="jane.doe@example.com" required > </div> </div> {# fin de la condition #} <div class="form-group"> <div class="col-sm-6"> <label class="control-label" for="naissance">Date de naissance</label> <input name="birth" type="text" class="form-control" id="naissance" placeholder="01-01-2001" required> </div> <div class="col-sm-6"> <label class="control-label" for="tel">Mobile</label> <input name="tel" type="tel" class="form-control" id="tel" placeholder="0607080910" required> </div> </div> <div class="form-group"> <div class="col-sm-6"> <label class="control-label" for="telFixe">Téléphone fixe</label> <input name="telFixe" type="tel" class="form-control" id="telFixe" placeholder="0101010101"> </div> </div> <div class="form-group"> <div class="col-sm-5"> <label class="control-label" for="adresse">Adresse</label> <input name="adresse" type="text" class="form-control" id="adresse" placeholder="1 Bd de Strasbourg 83000 Toulon" required> </div> </div> <div class="form-group"> <div class="col-sm-3"> <label class="control-label" for="cp">Code postal</label> <input name="cp" type="text" class="form-control" id="cp" placeholder="83000" required> </div> </div> <div class="form-group"> <div class="form-group"> <div class="col-sm-4"> <label class="control-label" for="ville">Ville</label> <input name="ville" type="text" class="form-control" id="ville" placeholder="Toulon" required> </div> </div> </div> <div class="form-group"> <div class="form-group"> <div class="col-sm-4"> <label class="control-label" for="pays">Pays</label> <input name="pays" type="text" class="form-control" id="pays" placeholder="France" required> </div> </div> </div> <div class="form-group"> <div class="col-sm-6"> <label class="control-label" for="medecin_traitant">Médecin traitant</label> <input name="medecin_traitant" type="text" class="form-control" id="medecin_traitant" placeholder="Dr Bicharzon" required> </div> </div> <div class="form-group"> <div class="col-sm-6"> <label class="control-label" for="ame"> Bénéficiare de l'AME ? </label> <select class="form-control" name="ame" title="ame" id="ame" required> <option value="oui">Oui</option> <option value="non">Non</option> </select> </div> </div> <div class="form-group"> <div class="col-sm-6"> <label class="control-label" for="cmu"> Bénéficiare de la CMU ? </label> <select class="form-control" name="cmu" title="cmu" id="cmu" required> <option value="oui">Oui</option> <option value="non">Non</option> </select> </div> </div> <input title="heureDispo" class="visually-hidden form-control" name="heureDispo" type="text" id="heureDispo"> <div class="form-group boutonsModale col-sm-6"> <button type="button" class="btn btn-default btn-danger" data-dismiss="modal">Annuler</button> <button type="submit" class="btn btn-primary" id="monBouton">Enregistrer</button> </div> </form> </div> {#{% endfor %}#} <div class="modal-footer paddingTop"> {#<button type="button" class="btn btn-default btn-danger" data-dismiss="modal">Annuler</button>#} {#<input type="submit" class="btn btn-primary">Enregistrer</input>#} {#<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>#} </div> </div> </div> </div> </div> 

  2. Routing.yml : Routing.yml:

    Take RDV 以RDV

    take_rdv: path: /prise-rdv defaults: {_controller: RdvProBundle:Default:takeRdv} methods: [POST] options: expose: true take_rdv:路径:/ prise-rdv默认值:{_controller:RdvProBundle:Default:takeRdv}方法:[POST]选项:暴露:true

I don't know how to change the route if I need to... + I would like the route no to show like the other routes I created but as it's coded now, it's shown... 我不知道如果需要的话如何更改路线... +我希望路线no像我创建的其他路线一样显示,但是如现在所编码,它已经显示出来...

I am junior junior as dev so I a sorry if my code is not clean :s Thank you in advance for all the help you will provide. 我是一名初中生,所以很抱歉,如果我的代码不干净:s预先感谢您提供的所有帮助。

It is huge. 很大 I'm not sure about your problem(s?) but if I understand : 我不确定您的问题,但是如果我理解:

First problem : 第一个问题:

ajax is not working since events do not reload Ajax无法正常工作,因为事件不会重新加载

If your #button is replaced in your page after your first call, the attached event is destroyed. 如果您的#button在第一次调用后被替换为页面中的附件,则事件将被销毁。 Change your listener : 更改您的听众:

$("#monBouton").click(function(){

by 通过

$('body').on('click', '#monBouton', function () { will solve the problem. $('body').on('click', '#monBouton', function () {将解决此问题。

Second problem : 第二个问题:

If I put if ($request->isXmlHttpRequest()), the controller goes directly to "else" 如果我输入if($ request-> isXmlHttpRequest()),则控制器将直接转到“ else”

I suggest to pass $request as argument of your action and just put your condition within an if statement : 我建议传递$ request作为操作的参数,并将条件放入if语句中:

public function takeRdvAction(Request $request)
{
    if ($request->isXmlHttpRequest()) {

        [...]
    }
}

Thirdly : 第三:

To use FosJsRouting, you exposed your route in your yaml. 要使用FosJsRouting,您需要在Yaml中公开路线。 That's good. 那很好。 To use it in javascript, you have to include the given script in your base.html.twig and use Routing.generate just as defined in the doc : 要在javascript中使用它,您必须在base.html.twig 包含给定脚本 ,并按照doc中的定义使用Routing.generate

$.ajax({
        url: Routing.generate('take_rdv', {/* $(yourform).serialize() ?*/}),
        method: 'POST',
        data: {},
        success: function(data) {
            $("#calendarModal").modal('hide');
            $("#calendarModal").on('hidden.bs.modal', function (e) {
                $('#calendar').fullCalendar('refetchEvents');
            });
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Error: ' +  errorThrown);
        }
    });

Update 更新资料

With my suggestions, you've to change how you use $request in your action : 根据我的建议,您必须更改在操作中使用$request的方式:

$myArray = $request->request->all();

$civilite=$myArray['civilite'];
[...and so on...]

Bonus : Symfony is a powerfull framework. 奖励:Symfony是一个功能强大的框架。 I suggest you to learn about using this framework and especially, in your case, about Forms 我建议您学习使用此框架的知识,尤其是有关表单的知识

enjoy :) 请享用 :)

UPDATE 2 更新2

if ($request->isXmlHttpRequest()) { is never true cause you are not doing an ajax call. if ($request->isXmlHttpRequest()) {永远不会为true,因为您没有进行ajax调用。 I just see that, but your button is of type submit then, your browser send a basic HTTP request. 我只看到,但你的按钮式的submit ,然后,您的浏览器发送一个基本的HTTP请求。

Add this to your js code : 将此添加到您的js代码中:

$('body').on('click', '#monBouton', function (event) {
    event.preventDefault();

    [...$.ajax blablabla]
});

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

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