简体   繁体   English

Ajax请求在CSS弹出窗口中形成

[英]ajax request to form in css popup

from France ... and sorry for my english language ... 来自法国...对不起我的英语...

I code in symfony 3. I have an entity "book" can have, according to the "type" attribute selected, one or more attributes (client, student, organizer and / or speaker) each mapped as an entity "user". 我用symfony 3编写代码。我有一个实体“书”,可以根据选择的“类型”属性,将一个或多个属性(客户,学生,组织者和/或发言人)映射为一个实体“用户”。 In my "book" form, I can select for each of the attributes related to "user" my users registered in the bdd. 在“书”形式中,我可以为与在bdd中注册的用户的“用户”相关的每个属性进行选择。 I want to add a "user" by opening a popup css, loading my user form and saving it with ajax. 我想通过打开一个弹出式CSS,加载我的用户表单并将其保存为ajax来添加“用户”。 In case of success, I recharge my list with ajax to use on my form "booking. 如果成功的话,我会在列表中添加ajax以便在“预订”表单上使用。

 function nouveauOrganisateur() { $.ajax({ url : Routing.generate('user_new'), success: function(html) { $('#NewOrganisateur').append( $(html).find('form') ); $('div#NewOrganisateur form button').replaceWith( $( '<button onclick="EnregistrerClient(event)">Enregistrer</button>' ) ); } }); } function EnregistrerClient( event ) { console.log(event.target.nodeName); var $form = $( event.target ).closest( 'form'); $.ajax({ url : Routing.generate('user_new'), type: 'post', success: function(html) { console.log(html); } }) } 
 <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <div id="UserOrganisateur" class="label"> <strong>{{ form_label(form.Stage.organisateurs, "Organisateurs ") }}</strong> {{ form_errors(form.Stage.organisateurs) }} {{ form_widget(form.Stage.organisateurs) }} <button onclick="nouveauOrganisateur()">Ajouter un Nouveau client</button> <div id="NewOrganisateur"> {{ form_errors(form.Stage.NewOrganisateur) }} {{ form_widget(form.Stage.NewOrganisateur, {'value' : false}) }} </div> </div> 

When I validate my form "user", my page is reloaded but it is not what I want. 当我验证表单“用户”时,页面会重新加载,但这不是我想要的。 And my user is not registered !? 并且我的用户未注册! I started in javascript. 我从javascript开始。 Can you help me? 你能帮助我吗?


From my form, I have a "select" for the type. 从我的表单中,我有一个“选择”作为类型。 When the "type" is selected, thanks to FormEvents and a js script I load the rest of the form. 当选择“类型”时,多亏了FormEvents和js脚本,我加载了表单的其余部分。 For example, if my "type" is "training", I load the embedded entity Training form. 例如,如果我的“类型”是“培训”,那么我将加载嵌入式实体“培训”表单。 My "training" entity contains the "organizer" attributes and "speaker", 2 multiple select which options are the "user" stored in the BDD. 我的“培训”实体包含“组织者”属性和“发言人”,有两个选择选择哪些选项是存储在BDD中的“用户”。 In this form, I add a button to record a new "user". 在此表单中,我添加了一个按钮来记录一个新的“用户”。 This button displays the registration form to use in which I replaced the submission boutton by Ajax action button . 该按钮显示要使用的注册表单,其中我用Ajax操作按钮替换了提交按钮 I just want to register my new user and recharge my list of selects ("organizer" and "speaker"), but when I click on this last boutton my page comes from where to select the "type". 我只想注册我的新用户并充值我的选择列表(“组织者”和“发言人”),但是当我单击此最后一个按钮时,我的页面就从此处选择“类型”。


Whith some change, I can registred my new user 如果有所更改,我可以注册我的新用户

 function nouveauOrganisateur() { $.ajax({ url : Routing.generate('user_new'), success: function(html) { console.log(html); $('#NewOrganisateur').append( $(html).find('form') ); $('form[name=user] button').replaceWith( $( '<button>Enregistrer</button>' ) ); $('form[name=user] button').on("click", function(){ $.ajax({ url : Routing.generate('user_new'), type: 'post', data: $('form[name=user]').serialize(), success: function(html) { console.log(html); } }); }); } }); } 

But my page is always reloaded ??? 但是我的页面总是被重新加载???

I think you are trying to stop the page from reloading or changing when the user clicks the button. 我认为您正在尝试阻止用户单击按钮时重新加载或更改页面。 There may be two solutions to this: 可能有两种解决方案:

1.) The <button> html tag defaults to type="submit" which submits the form. 1.) <button> html标记默认为type="submit" ,用于提交表单。 You can avoid this by adding type="button" 您可以通过添加type="button"来避免这种情况

or 要么

2.) In the javascript function EnregistrerClient add a line at the top of the function event.preventdefault(); 2.)在javascript函数EnregistrerClient ,在函数event.preventdefault();的顶部添加一行event.preventdefault(); this will stop the form from submitting. 这将阻止表单提交。

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

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