How to create a simple html form in symfony2 without using formbuilder - forms

I have a (hopefully) simple problem:
I want to use a simple html form in a sympony2 project. The Problem is, that noting is sent by get through the form.
The Url after sending the form is: localhost:8888/de/search_art?
you see... there is no string after the ? !!!
The form looks like this:
<form class="navbar-form" role="search" method="get" action="{{ path('func_search_art') }}">
<input type="text">
<button type="submit" class="btn btn-default">Send</button>
</form>
This is my routing:
func_search_art:
path: /search_art/
defaults: { _controller: FuncSearchBundle:Search:SearchArt }
I don't want to do something within a controller for it, because it is in the navbar and for this it has to appear in every page. I don't want to initialize a form variable in every controller of the page for a simple navbar search field.
Is there a possiblity to create a simple form like this in symfony2 without using a form builder and so on ... ?
Greetz Michael

Yes, you can safely keep your form written in twig in the form you presented it. No need to create a form builder in every controller and send the form to twig. The only place you will have to build it is the SearchArtAction method from SearchController, where you have the handling functionality for this form.
Also you need to give a name to your text input.
<form class="navbar-form" role="search" method="get" action="{{ path('func_search_art') }}">
<input type="text" name="q">
<button type="submit" class="btn btn-default">Send</button>
</form>

Because your input lacks the name attribute , which is the relevant attribute to create the post data/query string.

You can't it will give you: "The CSRF token is invalid. Please try to resubmit the form". Regardless of that it actually work without it. For some reason Symfony serialized the forms. I particularly hate the fact that I need the form object to use forms, in many cases I found it overkill. Your $form->isValid will also failed, but the $form->isSubmitted() will not. One of the reasons I hate the form object is how tedious it is to populate a drop down with an EntityType, instead of making simple symfony manage to make the process a damn nightmare.

Related

Polymer + form POST data

I have this
<form id="form_837299" class="appnitro" method="post" action="insert.php"> <paper-input label="Title" name="title" maxlength="255">
</paper-input>
<paper-input floatinglabel multiline label="text" name="text"></paper-input>
<li class="buttons">
<input type="hidden" name="form_id" value="837299" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
I have problem with POST data - nothing is sended in "text" and "title" (all in paper-input).
I modified the template and attribute "name" now is in one div, which Polymer created. But no data are sent.
print_r($_POST); shows me only this:
Array ( [form_id] => 837299 [submit] => Submit )
Anybody knows how use Polymer and Material UI on form?
Only elements that extend native form elements automatically get submitted with forms. paper-input extends core-input which has an input inside it as opposed to extending it. See this mailing list discussion for additional discussion and this StackOverflow post for possible solutions.
Something like this jsbin maybe?
Update: Here's the same thing in web component form.
Update: Looks like the creator of ajax-form has added this functionality.
Update: Also consider using iron-form.
According to the Polymer docs the way to do this is to just create a regular form input and wrap it in the <paper-input-decorator>
https://www.polymer-project.org/docs/elements/paper-elements.html#paper-input
I've tried it out and it works fine. Some better form support would be cool, but oh well. This stuff still kind of rocks.
UPDATE: I've built a bower package (polymer-rails-forms) to deal with forms in polymer, tailored specifically to the ActiveRecord input naming scheme but it will work with any old form really. It's still relatively new, but it covers most input types, basic validations, xhr and non-xhr submits and has a couple cool extras like image, json, and location* fields.
the location fields depend on the Google Map Places API

Form Variables are not showing up after form submit. ColdFusion

<form name="abc" id="abc" method="post" action="/test.cfm" enctype="multipart/form-data">
<input type="submit" name="btnSubmit" id="btnSubmit" value="OK" />
</form>
for some reason when I hit submit the "btnSubmit" is not showing up in the cfdump.
<cfdump var="#form#">
There aren't a lot of things that could cause it to simply not appear in your form. My short-list of culprits are:
Looking at the wrong file / server.
The page is being redirected via cflocation or otherwise (javascript location.replace() or location.href=x) -- this would cause that problem even if the redirection is returning the browser to the same page.
Form variables being stripped out somewhere further up, I would guess in onRequestStart
A local variable named "variables.form" was created and set to a structure further up - not very likely, but I suppose it's possible someone could accidentally write something like <cfset form = url />, which might cause that
Usually, when something like this has happened to me in the past, I've found that I've been viewing the wrong file in the browser. Usually it's come down to me looking at the same file on the wrong domain name, e.g. looking at the production server instead of the development server.
If you combine your code segments above into a single file like this (test.cfm):
<cfdump var="#form#" />
<form name="abc" id="abc" method="post" action="test.cfm" enctype="multipart/form-data">
<input type="submit" name="btnSubmit" id="btnSubmit" value="OK" />
</form>
That ought to give you some insight into your problem. Note that I removed the leading-slash / in the form action, so that this form will post to itself. When I first view this template, I see an empty struct (followed by the button), because I haven't put anything in the form scope yet. When I submit the form I then see two elements in the structure, fieldnames and btnSubmit. That's another good indicator, if you don't see fieldnames in the form structure, then your CFML page may not have received a form submission. If you know you're looking at the right page and you've submitted the form and you still don't have the fieldnames entry, then I'd start looking for potential browser redirection.
You might also want to add an empty Application.cfc in the same directory just to be sure that there's not an application interfering with it. It's possible that something in the onRequestStart might be stripping out form variables with the name "btnSubmit" or even any form variable with the string "submit" anywhere in the name. I wouldn't expect it though -- I'd look for other causes like cflocation tags first.

Change the url for search input

I have a search form on a Drupal page (A custom view) that searches all my articles:
URL: www.site.com/articles
<form>
<input type="text" placeholder="Search Library" class="searchbox">
<button type="submit" id="edit-submit" name="op" value="Search" class="form-submit">Search</button>
</form>
When a user submits this form I get a string appended to the end of the url like this:
www.site.com/articles?search_fulltext=SEARCHTERM&op=Search
The problem is that it's not working, and to get it to work I have to search from a different page like here:
www.site.com/search?search_fulltext=SEARCHTERM&op=Search
So ultimately I would like to have my search box on a page, but when a user submits I want it to reference another url:
www.site.com/search?search_fulltext=SEARCHTERM&op=Search
I basically want to have a search box on a different page that is being searched. If that makes any sense. :)
Use the <form> tag's action attribute to submit the form to the search page like:
<form action="http://www.site.com/search">
I also just realized that you are probably only looking to search specific content types so you may wish to look into the Custom Search module (http://drupal.org/project/custom_search).

jQuery form processing

Does anybody know of a simple jQuery form processing tutorial that actually works?
I have a form I want to process via jQuery/Ajax nothing difficult in PHP but in jQuery and AJAX can I get it to work - no, all the tutorials are based round sending e-mails (or just lists of more lists of more lists of tutorials - hate those)
All I want to do is learn how to send a form via jQuery and AJAX to another page, save the data in a DB without having to leave the first page. I have tried all sorts but nothing works properly. Here is my form:
<form id="form">
<input type="text" name="abc" />
<input type="text" name="def"/>
<input type="text" name="ghi"/>
<input type="submit" name="try" id="try" />
</form>
Now what do I actually do? Sounds silly I know (and I guess I'll get another -1 star for this question) but I will be honest a GOOD simple tutorial would be really useful not just to me but to the others. I know php but jQuery/Ajax - just don't know/understand. I will not be alone
This is one of the good tutorials on how to submit forms using ajax and php.
This link is a reference teaching how to submit forms via jQuery/AJAX. Have the form post to a PHP page to handle the form data.
In short, your jQuery code would look similar to this:
$("#form").submit( function()
{
// Handle validation or any extra data here.
// Return true if validation passed and the data should be posted
// Return false if the form should not be submitted
// You can also do any extra work here that you like before returning,
// including changing something on the page or showing the user a message.
}
There's a cracking plugin for this:
http://plugins.jquery.com/project/form/
It's as easy as:
$('#myForm').ajaxForm(function() {
alert("Thank you for your comment!");
});

How to open a new window on form submit

I have a submit form and want it to open a new window when users submits the form so i can track it on analytics.
Here is the code I'm using:
<form action="http://URL at mailchimp subscriber URL.com" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" onclick=window.open(google.html,'','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');>
<label for="name">Your Name</label><input type="text" value="" name="FNAME" class="required" id="mce-FNAME">
<br/>
<br/>
<label for="email">Your Email </label><input type="text" value="" name="EMAIL" class="required email" id="mce-EMAIL">
<br/>
<br/>
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="submit">
</form>
</div>
No need for Javascript, you just have to add a target="_blank" attribute in your form tag.
<form target="_blank" action="http://example.com"
method="post" id="mc-embedded-subscribe-form"
name="mc-embedded-subscribe-form" class="validate"
>
In a web-based database application that uses a pop-up window to display print-outs of database data, this worked well enough for our needs (tested in Chrome 48):
<form method="post"
target="print_popup"
action="/myFormProcessorInNewWindow.aspx"
onsubmit="window.open('about:blank','print_popup','width=1000,height=800');">
The trick is to match the target attribute on the <form> tag with the second argument in the window.open call in the onsubmit handler.
For a similar effect to form's target attribute, you can also use the formtarget attribute of input[type="submit]" or button[type="submit"].
From MDN:
...this attribute is a name or keyword indicating where to display the response that is received after submitting the form. This is a name of, or keyword for, a browsing context (for example, tab, window, or inline frame). If this attribute is specified, it overrides the target attribute of the elements's form owner. The following keywords have special meanings:
_self: Load the response into the same browsing context as the current one. This value is the default if the attribute is not specified.
_blank: Load the response into a new unnamed browsing context.
_parent: Load the response into the parent browsing context of the current one. If there is no parent, this option behaves the same way as _self.
_top: Load the response into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same way as _self.
onclick may not be the best event to attach that action to. Anytime anyone clicks anywhere in the form, it will open the window.
<form action="..." ...
onsubmit="window.open('google.html', '_blank', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');return true;">
The code you have given, needs to be corrected. In form tag you have to enclosed the onClick attribute value in double quote:
"window.open('google.htm','','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');"
You also need to take care that first parameter of window.open should also be enclosed using quotes.
I generally use a small jQuery snippet globally to open any external links in a new tab / window. I've added the selector for a form for my own site and it works fine so far:
// URL target
$('a[href*="//"]:not([href*="'+ location.hostname +'"]),form[action*="//"]:not([href*="'+ location.hostname +'"]').attr('target','_blank');
i believe this jquery work for you well please check a code below.
this will make your submit action works and open a link in new tab whether you want to open action url again or a new link
jQuery('form').on('submit',function(e){
setTimeout(function () { window.open('https://www.google.com','_blank');}, 1000);});})
This code works for me perfect..
window.open doesn't work across all browsers, Google it and you will find a way of detecting the correct dialog type.
Also, move the onclick call to the input button for it to only fire when the user submits.