Dynamically loading content through Groovy server page upon form submit - forms

I have a Groovy project (vanilla; no Grails) with an index.gsp that takes form input from the user and sends it in a POST request to a Groovy script. The form is set up like this:
<form action="somewhere" method="POST" enctype="multipart/form-data">
// some other inputs
<input type="submit"/>
</form>
Is there any way (ideally not using Javascript) to dynamically load content on the same page after the user submits? Redirecting to another GSP might also work. Just something simple, like a string containing whatever the user typed. It seems like Grails has plenty of options, but unfortunately I can't use it.

As you mentioned, Grails is capable of doing what you need without any complex code. Since you can't use it, you will have to use JQuery(Javascript) to make an AJAX call. AJAX is the he only way that I know to achive that.
Just make an AJAX call to your groovy script. JQuery.ajax has a success function to be called if the request succeeds. You can use it to update a hidden dive after the form. This success function has the data returned from the server as an argument, that data could be the string containing whatever the user typed. In that case just add the data to the hidden div and then make that div visible.
function onSucceed(data) {
$('#hiddenDivToUpdate').text(data);
$('#hiddenDivToUpdate').show();
}
You can learn about JQuery.ajax() in this link AJAX

Related

Field and Form Validation with ember.js

I've integrated Boronine's excellent field validation code for ember.js from jsfiddle. While that is wonderful, I still need to perform form level validation, to ensure that when the user submits the form, everything is okay.
What's the best way to do that? Is there a way that I can mark a field as having been validated, so that the form handler can simply walk the fields to see what has been validated or not?
MP.SignUpFormView = Em.View.extend({
submitLogin:function (event) {
// walk through object fields to perform validation here, but how?!
}
});
Edit:
For clarity, I am using Handlebars and binding, not trying to walk DOM objects or the like.
The pattern you're trying to use makes sense in applications that follow a document-scripting pattern, which Ember does not. You can force this work, but you'll find each next step in the application will get harder and harder.
In Ember display is backed by data objects so form fields in an Ember application are bound to a property on some object and as changes are made, the values are updated immediately. You don't even really need a <form> except maybe for styling.
When a user wants to take some action on this object (like persisting it to a server) the application's current state will answer the question "what happens when a user wants to take this action right now?" A user clicking a button here doesn't mean "now serialize the data in the form and do something" it means "I'm done changing the properties of this object and would like to do something else in the application now."
Your handlebars template would look something like this:
{{view Ember.Textfield valueBinding="name"}}
{{view Ember.Textfield valueBinding="age"}}
<button {{action save content}}>Save</button>
And a possible state in your application where this can be handled
Ember.Route.extend({
save: function(router, event){
if (event.context.validate()){
router.transitionTo('someNewState')
}
}
})

How do you access javascript from selenium and submit a form?

I have some html that uses JS to submit a form:
<form onsubmit="doform();return false;">
In the test script I'm doing the following based on what I found here on SO and in
the WWW::Selenium docs.
$sel->get_eval("this.browserbot.getUserWindow().doform()");
The normal form submission works fine but it does not work in the selenium code.
If I scoop the JS out of the js file and make a string in the test file it works fine.
my $js = "{ //do some javascript stuff here; }";
$sel->get_eval( $js ); // this works.
I'm under the impression that that is not the correct way to do that though. It's certainly
not desirable, especially when the function calls other functions.
What is the correct form for using selenium to submit a form?
There is a method to submit the form (which should also fire the onsubmit javascript).
From WWW::Selenium:
$sel->submit($form_locator)
Will submit the specified form. $form_locator is an element locator for the form you want to submit.

Yii - How to call external javascript on every CActiveForm validation?

I would like to make a "preview container" for form values in Yii. (so every time the user finishes entering data, the "preview container" below the form will display them, to let the user knows how the item actually looks like).
To achieve this, the only way is to call a Javascript function to update the "preview container" (using jQuery). The CActiveForm is:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id'=>'item-form',
'enableAjaxValidation'=>true,
));
?>
How do we modify it to call a javascript function each time the fields are validated?
(Note: whenever we switch between the input fields, the fields are validated dues to enableAjaxValidation=>true)
Thanks in advanced.
With jQuery you can define your own listener functions for the fields you want to update, which is probably going to be cleaner than trying to hook into the validation functions.You could monitor onchange or blur or whatever is most appropriate to your data.
The js can be loaded via Yii's registerScript function or, again, whatever is most appropriate for your app. A listener function would normally be loaded on DOM ready, i.e., with the POS_READY attribute for registerScript.
You can search the tutorials as well as this basic tutorial for more info.

can i send data to a javascript function on the server iphone

I i'm wondering if it is possible to call a javascript function from my application. The js function is on the server. Let's say i have a some inputs in the app. Then i have this submit button which calls the IBAction, from the IBAction i want to call:
function mySubmit(){
document.getElementById("myForm").submit();
}
and pass it the data the user entered and then get back a response (in my app) not in the server
Is this possible to do? if so, can you provide some useful links?
Thanks in advance and have a nice saturday ;)
The js function is on the server
Are you sure this is your case? Don't you mean that your js submits a form that is itself treated on the server side?
Javascript is normally a client-side scripting language, which is executed on client-side, contrary to e.g. PHP which is only executed server-side.
[EDIT] (As stated by #Dr.Dredel in the comments, as there exists server-side javascript, but these usages are not yet very common and I don't think it corresponds to your context)
If you need to call a javascript function from an already loaded HTML page in a WebView, you can simply use the UIWebView's stringByEvaluatingJavascriptFromString:. You can pass a string representing javascript code to this function, so you can build a string that represent a function call with string or int arguments for example without any problem.
But if you intend to load your HTML page just to call a javascript fonction which in turn submit a form that sends the form's data to your server… This is clearly the wrong way!
You should instead considering performing the NSURLRequest to your server directly in Objective-C (maybe using ASIHTTPRequest to perform a POST request and easily set the values for each keys of the form you intend to send). In addition, doing this directly in ObjC will avoid loading the HTML page for nothing and rely on the js script, and will allow you to directly get the response in the delegate method.

How to clear the form when the user doesn't provide valid username/password

I designed a form as follows:
User Name: _______________
Password: _______________
Login
I also use jQuery Form Plugin to submit the form to the server side.
It will return if the server script finds some errors. The data returned by server is in JSON format. I would like to know how I can reset the user name + password when I know the username/password is invalid in a decent way.
In other words, I can manually use jQuery to empty the username/password field if the returned result indicates a failure. In fact, I am looking for a decent way built in Form Plugin or sth else that can do this part me for automatically. The only thing I have to do is to set a flag so that if the submission is failed, then the form will be resetted.
Thank you
You cam simply do:
$('#form_id').reset();
I don't think you need a plugin for such simple task. You simply call above code based on the response.
Run this.form.reset() when a form button (e.g. Reset) is being pressed.
e.g.
<form>
...
<input type="button" value="Reset!" onclick="this.form.reset();">
</form>