Rely on order of form elements on the server PHP - php

With a HTML form, can I rely on the ordering of the fields in HTML be the same as the data beingsent to the server?
For example if I have 2 inputs:
<input type="text" name="one"/>
<input type="text" class="aaa" name="two[]"/>
<input type="text" class="bbb" name="two[]"/>
When parsing the data on the server side and looping over input named 'two', can I rely on the fact that input.aaa will be first in the loop.
I am using php, but the same question applies to all major backend languages receiving form (post) data.

No.
You can manipulate the order by manually setting the index in the input's name:
<input type="text" class="aaa" name="two[0]"/>
<input type="text" class="bbb" name="two[1]"/>

Related

Use PHP to determine the type value from input

My question, it´s very short.
is it possible to detect the kind of types we send as POST from a form?
If I have some fields and one field for each input type as text, the other textarea, the other file, etc, is it possible to use Php to detect, in each case, the type from these fields.
<form method="post">
<input type="text" name="test1" value="ok">
<textarea name="text2"></textarea>
<input type="file" name="test3" value="ok">
</form>
And finally determine whether the type is text, textarea, file, etc
I know it´s possible with Jquery buy i don´t find nothing about this with php.
Thank´s for the help in advanced
No, request contains only key-value pairs. But you can add additional hidden fields that passes these data. i.e. (for larger forms I's suggest it automaticaly with JS):
<input type="hidden" name="text2_type" value="textarea">
<input type="hidden" name="test3_type" value="file">
or
<input type="hidden" name="types[text2]" value="textarea">
<input type="hidden" name="types[test3]" value="file">
Other solution
Also you can just introduce naming convention for your fields, i.e.:
<textarea name="text2_textarea"></textarea>
or
<textarea name="textarea[text2]"></textarea>
so in your PHP you can check if key ends with _texteraea or is in array $_POST['textarea'] to determine the type o field.

Multiple name values for an input item? Jquery validator uses name attribute for validation

I have a form with input elements with the following code:
<label class="label">Activity Name</label>
<label class="input">
<input type="text" name="required" name="activityname">
</label>
I tried using two name attributes since the jquery validation plugin using the name attrbute to validate the input.
I am not sure how to manipulate the inputs though once the form is set, since there could be multiple inputs with a "required" name since they are required fields and need to be validate as such.
I also looked and it is not possible to manpipulate $_POST variables by id.
Use array for multiple values for same name attribute. Your field name will look like "fieldName[]".
<input type="text" class="required" name="fieldName[]">
<input type="text" class="required" name="fieldName[]">
<input type="text" class="required" name="fieldName[]">
I found the first commenter was right. Jquery will select by class or id. I simply set the class to required.
Thanks guys,

Creating multiple form to insert number

I have a form which is super simple.
<input type="text" name="tel">
So basically user hits submit and save new record with only the Tel.
what i want is to allow the user to create many tel row in the database. I have found a few different way of doing this and would like to know which approach is the best.
1) is to duplicate my form and increment the number which means i would need to write a lot of code.
<input type="text" name="tel">
<input type="text" name="tel1">
<input type="text" name="tel2">
<input type="text" name="tel3">
submit would create 4 new rows but this method would create allot of coding for its intended purpose.
2) I found a few references to using a class but i haven't got the slightest idea where to start with classes.
is there a simpler way of achieving this or is option 1 my best bet?
Use it like this.
<input type="text" name="tel[]">
<input type="text" name="tel[]">
<input type="text" name="tel[]">
<input type="text" name="tel[]">
and on server side you can extract them as array.
foreach($_POST['tel'] as $tel=>$value)
{
echo $tel.' - '.$value;
}

How to automatically add something to value $address?

I have this:
echo '<input type="text" id="address" name="address" value="'.$address.'" />';
When you enter your adress (my+adress) in search form and hit button "Search" I want to search automatically add cityname and country in link, to link look like .../index.php?address=my+adress+citiname+county.
Thanks in advance!
So just use a method="get" in the post field and it will dump the fields to the URL?
<form action="" method="get">
<input type="text" name="myAddress">
<input type="text" name="city">
<input type="text" name="county">
<input type="submit" value="Go Search">
</form>
If you don't want to add in the fields, you will need to parse the myAddress field and use a meta redirect to populate the data you parsed from their address.
It would be much more straightforward to either:
add cityname and country in index.php, if those are known in _SESSION
or:
add a (possibly hidden) field in the form with cityname and address, even if you will receive two different values from your form and require merging them
To do exactly what you want, you need to modify the query just before it is submitted; it is easiest to do this, e.g., using jQuery.

Posting jquery appended input boxes to MYSQL

We have a very large form and we are posting the data to mysql using $_POST, but we have 2 complex sections in the form where we need your help.
HTML markup of form
<label>Customer Name</label>
<input type="text" size="50" name="name" />
<label>Education</label>
<input type="text" size="50" name="class" />
<input type="text" size="50" name="board" />
<input type="text" size="50" name="subjects" />
<input type="text" size="50" name="aggregate" />
<label>Payment Plan</label>
<ul id="fields"></ul>
We are appending following to #fields using jquery
<li><input type="text" size="30" name="date" /> <input type="text" size="30" name="amount" /> </li>
Now we would like your help with the following problems...
How to post education details to mysql table? - using php serialise?
how to post appended input boxes to mysql's tbl.paymentplan?
If you've populated the fields using jquery (date & amount) and it's on the form, a simple "Submit" button which posts to a PHP page that handles the insert into the database should work fine.
Nothing complex about it. I use jquery to pre-load form data all the time like that.
If you want to avoid doing a postback entirely, and want to do that through jquery as well, you can. Once the data is loaded into the DOM and the form displays it you can do with it what you want.
Algorithm
use mysql_fetch_field to know all attributes name from your table
if attribute does not exist in current table then update table via php like below
mysql_query("ALTER TABLE paymentplan
ADD date CHAR(30) AFTER subject,
Add payment CHAR(30) AFTER date");
and then insert the data the way u want.
Reference
mysql fetch field

Categories