How can I post a form and show the result on the same page with Play framework? - forms

I've been playing around with Play framework for a few days, and it seems really cool. However, I ran into some problems when I wanted to have a form on a page, post the form and show the results on the same page (with the form still on the page). Does anybody know if this is possible with Play, and if so: how?
EDIT:
I should have explained better. The form is a search-style form not a save-style form. I want to be able to search for something, have the results come up under the form and still have the values the user filled in in the form (as it is if you type in something that don't validate. I have tried to set values on the params object directly in the search action, but it disappears when the search action calls the new action.

Second try of an answer untested, hope this fits to your problem.
public static void search(String criteria1, String criteria2) {
....
params.flash();
}
search.gsp
<p id="criteria1-field">
<label for="criteria1">&{'criteria1'}</label>
<input type="text" name="criteria1" id="criteria1" value="${flash.criteria1}" />
</p>
<p id="criteria2-field">
<label for="criteria2">&{'criteria2'}</label>
<input type="text" name="criteria2" id="criteria2" value="${flash.criteria2}" />
</p>

Well this is quite simple. You put into the routes.conf a
GET /myPageWithForm MyController.read
POST /myPageWithForm MyController.save
In read you read the data and render your page with the form. In save you save the data and redirect to read via read();
Hope this answers your question.

Related

How to scrape a form that requires field validation by user?

I'm trying to scrape prices from this site:
https://www.pensketruckrental.com/quote/start.html
I can easily enter the form data, and I can activate the "Get A Quote" button and click it.
What I can't seem to do is get the form data to submit using a web scraper (I'm just doing it in VBA). When I input text using the scraper, the button remains grayed out, and even making a .click call on the button just displays errors on the form telling you not to leave the fields blank. Apparently it only recognizes data when you use an input device?
The code for one of the required fields, pickupLocation, is the following when I enter it manually (and thus the button works and the form can be submitted):
<input
type="text"
id="pickUpLocation"
name="pickUpLocation"
class="penskeValidateField penskeGoogleTypeAhead penskeInlineError ng-isolate-scope ng-touched ng-focused ng-dirty ng-valid-penske-err_loc_empty_sa ng-valid ng-valid-parse ng-valid-required"
aria-invalid="false"
aria-required="false"
country="rentalEntryCtrl.formItems.country"
penske-validate-field="pickuplocation"
required=""
autocompelete="off"
data-penske-placeholder="rentalEntryCtrl.activePlaceHolders.pickUpLocation"
ng-model="rentalEntryCtrl.formItems.pickupLocationSearchCriteria.address"
autocomplete="off">
And when I enter the data automatically using my scraper the tag & attributes read as follows:
<input
type="text"
id="pickUpLocation"
name="pickUpLocation"
class="penskeValidateField penskeGoogleTypeAhead penskeInlineError ng-pristine ng-isolate-scope ng-invalid ng-invalid-required placeholder ng-touched"
aria-invalid="true"
aria-required="true"
country="rentalEntryCtrl.formItems.country"
penske-validate-field="pickuplocation"
required=""
autocomplete="off"
data-penske-placeholder="rentalEntryCtrl.activePlaceHolders.pickUpLocation"
ng-model="rentalEntryCtrl.formItems.pickupLocationSearchCriteria.address"
autocompelete="off">
So of course I tried to copy the fields in the first code block into the second code block using setAttribute(), but even though I could change the attributes, I still couldn't get the form to submit properly.
I've looked at others that have dealt with something somewhat similar with autocorrect; their solutions have involved looking at the header and responses and just using the straight XHR to loop through the autocomplete queries, but the pricing information I'm scraping comes after several pages of form submissions, so that's not an option here.
I'm stuck I think; any ideas on how to populate the form and click the button/submit via my scraper?

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

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.

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

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!");
});