How to parse html forms in the Go Iris framework? - forms

Sorry if this question is a bit basic, but how can you parse form inputs in the Go Iris framework?
Here is the form I am using
<form action="/" method="post">
Username:<input type="text" name="username">
Password:<input type="password" name="password">
<input type="submit" value="Login">
</form>
here is the route and the controller respectively
iris.Post("/", TestController)
func TestController(c *iris.Context){
username := c.Form.Get("username")//Doesn't work
password := c.Form.Get("password")//Doesn't work
}
how do I retrieve the values in the Post request after the form has been submitted, Thanks

Based off an example on the iris github page you could try c.PostValue("Username"). The code you have may also work but I think you need to capitalize the variable names. In the html template you can see the name value is lowercased, however your context is more likely going off those the variable names to the left of the actual html like Username.

Related

Inserting data into a database using forms in web2py

I want to store my data in a database using forms.
How can I do it without using SQLFORM
like in php we use $var = $_POST['var_form']
I created a table in modul file called Produit
db.define_table('Produit',`enter code here`
Field('Nom', requires = IS_NOT_EMPTY()),
Field('Poid', 'float', requires = IS_NOT_EMPTY()),
Field('Prix', 'float', requires = IS_NOT_EMPTY()),
Field('Expiration', 'datetime'),
auth.signature)
And created a form like
{{extend 'layout.html'}}
<form action="create" method=post>
<fieldset>
<legend><h1><center><em>Ajouter un produit</em></center></h1></legend>
Nom:<br>
<input type="text" name="nom" required/>
<br>
Poid:<br>
<input type="text" name="poid" required/>
<br>
Prix:<br>
<input type="text" name="prix" required/>
<br>
Date d'expiration:<br>
<input type="date" name="exp" required/>
<br>
<br><br>
<input type="submit" value="Ajouter">
Use URL helper to specify action of the form.
<form action="{{=URL('create')}}" method=post>
...
</form>
Then in create controller, you can access submitted values using request.vars
db.Produit.insert(Nom=request.vars.nom, Poid=request.vars.poid,
Prix=request.vars.prix, Expiration=request.vars.exp)
This answer has the right idea, but to make things a little easier, you should use HTML field names that exactly match the database table field names. In that case, you can simplify the insert to:
db.Produit.insert(**db.Produit._filter_fields(request.post_vars))
Beware, though, that your manual approach lacks any protection against CSRF attacks (which the web2py form system would provide). As an alternative, note that you can still use SQLFORM even if you want to generate a custom form layout in the view. This approach is covered in the documentation here and here.

How to create a form that uses "=" instead of "&" with HTTP GET?

We have a form:
<form name=input action=https://www.foo.com method=get>
<input type=hidden name=foobar>
<input type=hidden name=ticket>
<input type=text value=bar>
<input type=submit value=IN>
</form>
then in the end we will get:
foobar=&ticket=
but we need
foobar=ticket=
...what is the syntax for creating an HTML form that will put "=" instead of "&" in given places?
Replace
<input type=hidden name=foobar>
<input type=hidden name=ticket>
with
<input type="hidden" name="foobar" value="ticket"/>
The bit after the = is the parameter's value, so this is saying "submit a parameter called foobar with the value ticket."
The & comes from the "application/x-www-form-urlencoded" enctype of the form, which is the default. The other options available are described in the HTML standard, but the short story is that the encoding you want is not possible with a standard HTML form.
If Javascript is an option, you could try overriding the submission process and read the form values, building your own string according to your requirements, and direct the browser to the resulting URL.
I have a feeling this problem would be better solved by making the server at the "action" URL take the parameters in the standard & way, though...

AngularJS form inputs: how to set default values based on $scope but bind to new form model

Building a CMS with node, mongoDB & angularjs. I have an edit form with inputs that I want to bind to the $scope so their values are populated with existing data. Ex:
<form ng-submit="editArticle(article._id, article)" class="edit-form">
<label for="hed">Hed: <input type="text" name="hed" ng-model="article.hed" value="{{article.hed}}" /></label>
<label for="dek">Dek: <input type="text" name="dek" ng-model="article.dek" placeholder="{{article.dek}}" /></label>
<input type="submit" value="Save Changes"></input>
</form>
This appropriately fills in the input values with the current data from my mongo database. However, I would like to bind to a new form scope, so it doesn't try to pass in every value in the article scope, just the new scope within the form. (Article scope currently consists of every field in the article document in Mongo. If I pass all of this data in, I get an error about invalid schema because it is trying to pass in the __v field as well.)
So ideally I want to do this:
<form ng-submit="editArticle(article._id, form)" class="edit-form">
<label for="hed">Hed: <input type="text" name="hed" ng-model="form.hed" value="{{article.hed}}" /></label>
<label for="dek">Dek: <input type="text" name="dek" ng-model="form.dek" placeholder="{{article.dek}}" /></label>
<input type="submit" value="Save Changes"></input>
</form>
So on submit, it only passes form.hed and form.dek to my database. However, now these input fields are blank because they're not bound to the original article data. The user would have to re-fill in each of the fields with the original values. Is there a way to bind to multiple models orrrr bind to a current model, but on submit only send the updated form data?
EDIT:
It looks like I have to add the ng-change attribute to each field, to populate the input fields with the original scope, then on change, set the form scope equal to the changed value. Eg.
<form ng-submit="editArticle(article._id, form)" class="edit-form">
<label for="hed">Hed: <input type="text" name="hed" ng-change(form.article = article.hed) ng-model="article.hed" /></label>
<label for="dek">Dek: <input type="text" name="dek" ng-change(form.dek = article.dek) ng-model="article.dek" placeholder="{{article.dek}}" /></label>
<input type="submit" value="Save Changes"></input>
</form>
Not sure how thrilled I am about this solution, but it works. If anyone knows a more efficient way to accomplish this please let me know!
Do a copy of the original object (or part of it) upon receiving it from DB and bind your from to it.
Coping objects before edit is a very good pattern since it allows you to easily provide additional functionality like revert edits and disable save buttons if no edit was made.

Codeigniter: set_value not re-populating correctly array form field

I have a form with CI validation that uses arrays as input names. The view initially has this code:
<input type="text" name="feed_urls[]"
value="<?php echo set_value('feed_urls[]', ''); ?>" >
which when loaded into the browser translates correctly to this:
<input type="text" name="feed_urls[]" value="">
Then through Jquery the user may add more identical <input>'s to the DOM before submitting, so in the end what is POSTed could be e.g.
<input type="text" name="feed_urls[]" value="">
<input type="text" name="feed_urls[]" value="">
<input type="text" name="feed_urls[]" value="">
Now, if the submitted data passes validation, all is fine and gets stored in DB. But if validation fails, the controller sends back to the view but I don't see the N <input>'s of the POSTed form. I only see one and it's empty, which is understandable because I don't supply a 2nd argument to set_values(), but then again what was expected was to see the inputs be re-populated through the $this->input->post(feed_urls) array with the POSTed data that was invalid.
I do verify at the controller that $this->input->post('feed_urls') has the POSTed content (invalid or not) just fine.
I've read the CI user guide docs on using validation with arrays as field names
Any ideas on what's the correct use of set_value()? By correct I mean that on validation failure, I get the N inputs that were POSTed, correctly re-populated one by one.
You can use:
<input type="text" name="feed_urls[]" value="<?php echo set_value('feed_urls[0]'); ?>">
I've tested it on CI 2.0 but it should work also in previous versions.
Cheers!
As you supply only a small bit of your code, I can only give you a hint on what I think is your main problem.
The n element of input array can be populated by using jQuery as follows:
$("[id^=feed_urls]").eq(0).val(someURL);
$("[id^=feed_urls]").eq(1).val(someURL);
$("[id^=feed_urls]").eq(2).val(someURL);
and so on.

add groovy code to grails input form

Is it possible to add groovy code to grails form?
I have a form:
<g:uploadForm controller="document" action="save" method="post">
<input type="file" name="dataFile" />
<input type="submit" id="addDocument" value="<g:message code=messages.document.save"/>">
</g:uploadForm>
I need to add code that puts the URL segments to the parameter value.
You're using a POST (because it's an upload and that's correct) method in your form, so you will not see the params in the URL. The params will get there (to the controller you redirect the request to), but won't show at the URL. In any case, you should go with hidden inputs in your form. Like:
<input type="hidden" id="foo" value=""/>
In your controller, you can get the parameters set in your input hidden fields simply by accessing the params map:
params.foo
Use hidden fields inside the form.