HTML form, sets of data, datastructure interpretation - html

I have a form that is dynamically generated. Containing recurring lists of hidden fields, that represent selections within my application.
A form would typically look something like this:
<form>
<fieldset>
<input type="hidden" name="key1" value="value1"/>
<input type="hidden" name="key1_option" value="option1"/>
</fieldset>
<fieldset>
<input type="hidden" name="key2" value="value1"/>
<input type="hidden" name="key2_option" value="option2"/>
</fieldset>
</form>
Is there a better way to do something like this?
The main difficulty I am facing is that you lose the logical grouping of the fieldsets when you submit the form and try to interpret the input on the backend.

The logic behind the HTML structure will be lost of course, the only information transmitted is what's inside the form-tag. But I guess you already know that.
Why don't you use arrays to name your variables?
<form>
<fieldset>
<input type="hidden" name="keyset[0][key]" value="value1"/>
<input type="hidden" name="keyset[0][option]" value="option1"/>
</fieldset>
<fieldset>
<input type="hidden" name="keyset[1][key]" value="value1"/>
<input type="hidden" name="keyset[1][key]" value="option2"/>
</fieldset>
</form>
Is there a reason you group your items like that?

Related

passing information in an form? confused with some of the examples on SO

I need to pass this so a hardware engineer can do his work but it's not working. Any suggestions?
http://wfbscd13.cadence.com/cgi-bin/motd.cgi?msg=3&cmd=replace&text="
Here is my code...
<div class="messageform">
<fieldset>
<legend>Title 1</legend>
<form action=http://wfbscd13.cadence.com/cgi-bin/motd.cgi?msg=1&cmd=replace&text=" method="post">
<label for="mestext1"></label>
<input type="text" size="100" maxlength="80">
<div class="floatright">Titles can be up to 80 Characters... upload file if beyond 80 chars &nbsp<input type="submit" name="button1" id="button1" value="Replace"></div><br>
I thought I was good on this code but it's not working right, any ideas? Did I miss something?
Better like this:
<div class="messageform">
<fieldset>
<legend>Title 1</legend>
<form action="http://wfbscd13.cadence.com/cgi-bin/motd.cgi" method="GET">
<input type="hidden" name="msg" value="1">
<input type="hidden" name="cmd" value="replace">
<label for="mestext1"></label>
<input type="text" id="mestext1"" name="text" size="100" maxlength="80">
<div class="floatright">Titles can be up to 80 Characters... upload file if beyond 80 chars
<input type="submit" name="button1" id="button1" value="Replace">
</div>
</form>
</fieldset>
</div>
You need to be more careful with closing quotes and tags.
If a URL has a '?' in it, it probably means they want it as a GET not a POST.
Use hidden variables for the fixed params.
Don't repeat the text param.
The for of a <label> needs to point to the id of a tag.
If this is your entire code, then its not working probably because the code is not well-formed , i.e. there are places where you have missed to close the tags.

How to send hidden data

I have a form(like the one below for example) where I type data, but I also want to send data which are not directly entered by the user, for example a generic Id(for a user in this case)
<form name="input" action="" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
Don't know if I have been clear enough, I hope so.
try a hidden input:
<input type="hidden" value="foo" name="user_id" />
The user can't see it, but remember that such inputs can be spoofed and need to be validated on the server just like any other input.

Using HTML input element (type="submit"), how can we customize the GET field?

Using <form method="get"> element including <input type="submit"> element, we can have a way to GET a web page with some fields specified by the <input type="text" name="studentId"> elements, but can I customize those fields?
For example: I always want to add a action=true to the GET url to let the URL be something like this: http://example.com/?studentId=123&action=true?
Use <input type="hidden" name="action" value="true" />
inside your form.
You can add a hidden form field, though the name action is not a good one, as form has an action attribute and this name can conflict when scripting the form:
<input type="hidden" id="something" name="something" value="somthingelse" />
<div id="gbqffd">
<input type="hidden" value="en" name="h1">
<input type="hidden" value="d" name="tbo">
<input type="hidden" value="search" name="output">
<input type="hidden" value="psy-ab" name="sclient">
</div>
Google always has the answer; in this case I never had to do a search just look at the source :)

GET form action with URL parameter gets removed on form submission

This doesn't go to the URL http://example.com/a.php?r=1&go=1. Instead it goes to http://example.com/a.php?go=1 .
<form method="GET" action="a.php?r=1">
<input type="radio" name="go" value="1">
</form>
Even if I clear action="" it again goes to the same wrong URL. How can I include the r=1 in the URL of the form submission?
When you use method="get", you can't put values in the query string of the action. They will be replaced by the values from the form fields when you post the form.
Put the values in hidden fields instead:
<form method="GET" action="a.php">
<input type="hidden" name="r" value="1">
<input type="radio" name="go" value="1">
</form>
Passing additional parameters is done with hidden fields:
<input type="hidden" name="r" value="1" />
(sorry, i edited it; it didn't take my code on the first attempt)

POST extra values in an HTML <form>

I have a simple form which passes the value of an <input /> element:
<form action="updaterow.php" method="POST">
<input type="text" name="price" />
</form>
How can I post extra values along with the <input /> value? For example, an arbitrary string, or a variable inside the current PHP script.
I know this is possible with GET:
<form action="updaterow.php?foo=bar" method="GET">
<input type="text" name="price" />
</form>
or:
<form action="updaterow.php?foo=<?=htmlspecialchars($bar)?>" method="GET">
<input type="text" name="price" />
</form>
but I need POST.
You can include a hidden form element.
<input type="hidden" name="foo" value="bar" />
You can simply use a hidden field. Like so:
<input type="hidden" name="your-field-name" value="your-field-value" />
This field will then be available in your PHP script as $_POST['your-field-name']
As mentioned already, using hidden input fields is an option, but you can also use a session. That way you donĀ“t have to post anything, the variables remain on the server and are not exposed in the html.