I have a form that looks like so:
<label for="fullpath"><span class="required">*Full Path of folder to change access:</span></label>
<input name="fullpath" id="it10" type="text" size="50" maxlength="50" />
<br />
<small>Example: g:\A\Folder or j:\Your\Folder</small><br />
<div class="bgdiff">
<label for="userpermissiongroup">User Permission Group to be changed:</label>
<input name="userpermissiongroup" type="text" id="it11" size="50" maxlength="50" />
<small>If Known...</small></div>
<br />
<label for="addreadaccess">Additional users requiring read access:</label>
<input name="addreadaccess" type="text" id="it12" size="15" maxlength="15" />
<br />
<small>AD Username</small><br />
<div class="bgdiff">
<label for="addauthoraccess">Additional users requiring author access:</label>
<input name="addauthoraccess" type="text" id="it13" size="12" maxlength="12" />
<br />
<small>AD Username</small></div>
<br />
<label for="removeaccess">Users to be removed from access:</label>
<input name="removeaccess" type="text" id="it14" size="12" maxlength="12" />
<br />
<small>AD Username</small><br />
<div class="bgdiff">
<label for="supervisor"><span class="required">*Data Steward, Program Manager, Project Lead, or Supervisor who can authorize access changes:</span></label>
<input name="supervisor" type="text" id="it15" size="30" maxlength="30" />
<br />
<small>AD Username</small></div>
<br/>
<label for="phoneapprover"><span class="required">*Phone number of approving official: </span></label>
<input name="phoneapprover" type="text" id="it16" size="30" maxlength="30" />
<br />
<small>999-999-9999</small><br />
</fieldset>
</div>
I would like to give users the option to add all of this info to this form more than 1x before submitting. (say 10x max) I have run a couple ideas through my head. 1 is using Javascript to create the new fields and then parse them with my php script somehow. 2 is put say 10 code snips just like the form above in the code and hide them until the user clicks ADD ANOTHER.
Each input needs to be unique as I am submitting this info thought a simple $_REQUEST php script. I understand how to do this with 1 input and a for each loop, but am not sure how to make it work with such a large amount of inputs, labels, etc...
<?php
foreach($_POST['newdata'] as $value) {
echo "$value <br />";
}
?>
Anyone have some suggestions on the best way to go about this? I am not sure adding his form via JS is the best idea, so just displaying the new info from a hidden div seems quicker and easier...
If you append [] to your form field names, PHP will take those fields and turn them into an array, e.g.
<input type="text" name="field[]" value="first" />
<input type="text" name="field[]" value="second" />
<input type="text" name="field[]" value="third" />
would produce the following $_POST structure:
$_POST = array(
'field' => array(
0 => 'first',
1 => 'second',
2 => 'third',
)
);
The alternative is to append incrementing numbers to each field name, as you duplicate the existing field sets for each new block. This provides a nice separation between blocks and allows you guarantee that related fields have the same numerical tag, but it does complicate processing.
It's not so difficult: main idea is to use IDs for each iteration, so your inputs will have unique names and will be processed without problems
for ($i=0;$i<10;$i++){
echo "<input name='removeaccess' type='text' id='it14_{$i}' size='12' maxlength='12' />";
}
So, you take your code of current set of inputs with lables and add to input names IDs, formed on each circle iteration. Be carefull about ' and "!
Related
I am feeding my forms preset data values from another file in a MVC application. One of the data set attributes, the ID, is to remain fixed and cannot be updated. Only the names, phones numbers etc can be updated. My issue is that I need to have something set with this that I can submit, just like the other preset data; however unlike the other preset data I cannot put this in a form as a user may change it.
<form action="crud.ctrl.php?act=update" method="post">
<label>ID: <?=$data1["id"]?> <br /><br>
<label>First Name:</label> <br><input type="text" name="fnameUP" id="fnameUP" value="<?= $data1["fname"] ?>"> <br />
<label>Last Name:</label> <br><input type="text" name="lnameUP" value="<?= $data1["lname"] ?>""> <br />
<label>Phone:</label> <br><input type="text" name="phoneUP" value="<?= $data1["phone"] ?>""> <br />
<label>Email:</label> <br><input type="text" name="emailUP" value="<?= $data1["email"] ?>""> <br />
<label>Location:</label> <br><input type="text" name="locationUP" value="<?= $data1["location"] ?>""> <br />
<label>MC:</label> <br><input type="text" name="mcUP" value="<?= $data1["mc"] ?>""> <br />
<label>Position:</label> <br><input type="text" name="posUP" value="<?= $data1["pos"] ?>""> <br />
<label>Department:</label> <br><input type="text" name="deptUP" value="<?= $data1["dept"] ?>""> <br />
<input type="submit">
</form>
one way to solve this issue would be to have another unique column in the original table. For example you could 'salt' the ID and hash that or use some other form of creating a long enough string to prevent the user to guess any of the existing "IDs".
And then just include that column in the form as a hidden input field for example
<input type="hidden" name="custom_id" value="<?=$data1["custom_id"]?>">
that way even if the user does mess with the ID, there is a very small chance for him to be able to change another record. The more complex your hashing, the smaller the chance.
If that is not secure enough, my next idea would be to have another column/table in the database where you generate a hash when retreiving the data that will be shown in the form and only allow updating the records that have that value set. that way the only "editable" rows are the ones where someone requested the edit form in the last X minutes.
First off, I am a PHP NOOB.
I am currently trying to develop a PHP page that is suppose to calculate a monthly budget for you and I am having problem with one section of it.
I am having problem with my food variables.
I can't give you the syntax I have for the "foodPerMonth" array because I simply don't know how to write it. Everything I've written so far is wrong.
Anyhow. This is how I want it to work.
I have my html form. I'm making it shorter just for the food part.
You are suppose to type in how many i.e "Adult Woman" you want in your budget.
And I will have a variable for each of those that will contain the cost per month for each person.
<form action="function.php" method="post">
<label>Adult Woman: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Adult Man: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Juvenile Girl: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Juvenile Boy: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Child Girl: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Child Boy: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Baby Girl: </label><input type="text" name="foodPerMonth[]" /><br />
<label>Baby Boy: </label><input type="text" name="foodPerMonth[]" /><br />
<input type="submit" value="Submit" />
</form>
What I want to know is how do I type this kind of array that will go with the form and etc.
I would also appreciate if someone could thorougly explain how arrays and keys work and how they are used and what they are used for. Doesn't matter how much I read the fact in the manual. It wont stuck.
Thank you in advance for answering, all help will be greatly appreciated.
You're going to want:
<label>Adult Woman: <input type="text" name="foodPerMonth[adultwoman]" /></label><br />
<label>Adult Man: <input type="text" name="foodPerMonth[adultman]" /></label><br />
[note: also wrap the labels around the inputs]
Which will be accessible via:
$_POST['foodPerMonth']['adultman'];
$_POST['foodPerMonth']['adultwomman'];
Just the blank foodPerMonth[] syntax crams everything into a numbered array which skips any fields left blank making the numbering unreliable.
I hope I ask this question correctly, and if not please direct me how to repair it. I have had it deleted as a post once already...
My goal is to submit a form with one drop down, with numbers like 100, 200, 300 (for how many T-shirts you want to order)... Then depending on what is selected from the drop down have a series of text boxes (for number placement) that must add up to the selected number of shirts you want to order from the dropdown.
My idea is to capture all these text fields in an array, and send them off to a function to be added...
Can someone assist me please?
Here is the form code I know does not work, but I want it to work...
<form>
<label>
<input type="checkbox" name="PoloDesign" value="100" id="PoloDesign_0" />
100</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="200" id="PoloDesign_1" />
200</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="300" id="PoloDesign_2" />
300</label>
<br />
<input type="text" name="name[1]" id="name1" value="{$name1}"/>
<input type="text" name="name[1]" id="name2" value="{$name2}"/>
<input type="text" name="name[1]" id="name3" value="{$name3}"/>
<input type="text" name="name[1]" id="name4" value="{$name4}"/>
<input type="text" name="name[1]" id="name5" value="{$name5}"/>
<input type="text" name="name[1]" id="name6" value="{$name6}"/>
<input type="text" name="name[1]" id="name7" value="{$name7}"/>
<input type="submit" value="submit"/>
</form>
Just change each
name="name[1]"
To
name="name[]"
Then the fields are posted as an array you can iterate through in PHP
if (is_array($_POST['name']):
foreach ($_POST['name'] as $key=>$field):
// do something here
$yourKey = $key +1;
$yourValue = $field;
I have changed your code a little and tried to make it work using regular expression:
<?php
$name_array = preg_grep('/name[1-9]*/', $_GET);
?>
So, basically it checks all submitted variables and creates array from all variables that have name at start and a number at end. So, the form part should change to look like this:
<input type="text" name="name1" id="name1" value="{$name1}"/>
<input type="text" name="name2" id="name2" value="{$name2}"/>
<input type="text" name="name3" id="name3" value="{$name3}"/>
<input type="text" name="name4" id="name4" value="{$name4}"/>
<input type="text" name="name5" id="name5" value="{$name5}"/>
<input type="text" name="name6" id="name6" value="{$name6}"/>
<input type="text" name="name7" id="name7" value="{$name7}"/>
I tested on Apache2 and PHP 5.3
I'm practicing form validation with JavaScript but when I try to retrieve the data from the page it submits to I can't get it.
form.html
<body>
Hello.<br />
<form onsubmit="return validate()" action="process.php" method="POST">
Enter name: <input type="text" id="name" /><br />
Enter phone number: <input type="text" id="number" /><br />
Enter password: <input type="password" id="paswd" /><br />
Is there anything else you would like to add: <input type="text" id="anything" /><br />
<input type="submit" value="Check Form" />
</form>
</body>
process.php
<?php
echo 'Here: '.$_POST['number']
?>
Whatever index I use I get " Undefined index: line 2". What am I doing wrong?
EDIT: So I can't use the id attribute I need the name? Is there anyway to prevent coding redundancy since the value of all names will be the same as the corresponding id?
You need name attribute in your fields
<input type="text" id="number" name="number" />
$_POST looks for the name attribute in the field to capture the field values and not id
Your inputs need the name of the element.
Such as:
<input type="text" id="number" name="number" />
The Php gets the form data looking these names, not the ids.
you forgot name of input:
<input type="text" id="number" name="number" />
You need to give your form elements names.
<input type="password" id="paswd" name="paswd" />
Interestingly names and ids share the same namespace. If you don't really need the ids, leave them be. Inside a validate function you can always access all elements with the elements object of the form.
// called onsubmit
var validate = function(e) {
if (this.elements["paswd"].value.length < 4) {
alert("password needs to have at least 4 characters");
return false;
}
return true
};
I usually append the input type to my ids to differentiate them from field names
<label for="paswd-txt">Password: </label>
<input type="text" name="paswd" id="paswd-txt" />
<label for="save-cb">Remember me: </label>
<input type="checkbox" name="save" id="save-cb" value="1"/>
So like Vitor Braga said your inputs need the name of the element, but you only need this if you are using PHP to hadle the values of form in the submit, if you are using javascript to validaate like you said your were praticing you can obtain the value like this:
document.getElementById("number").value
It is easy to add more fields to a html form by jQuery. Then, we can serialize the fields, if the have the same name but what if we have a set of fields? For example
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
Now I want to add a new set of fields by jQuery as
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
and so forth. I process the form with PHP to insert movies into mysql database with three columns of (name, director, year). In the above-mentioned example, it is hard to serialize the fields to create appropriate $_POST arrays. How should I serialize jquery-added sets of movies?
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
Nothing else. On the server you will get (in case of POST) array in $_POST['movie_name'], $_POST['movie_director'] and $_POST['movie_year'];. Elements with the same index are from the same set of inputs.
What kind of problem with serialization do you have?
<form>
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<hr />
<input type="text" name="movie_name[]" />
<input type="text" name="movie_director[]" />
<input type="text" name="movie_year[]" />
<br />
<input type='button' id='serialize' value='Click me' />
</form>
and js code:
$('#serialize').click(function(){
alert($('form').serialize());
});
when you want to submit the data just write
$.post('script.php', $('form').serialize(), function() {alert('Saved');});
ps: if you are afraid to lose something, just compare count($_POST['movie_name']), count($_POST['movie_director']) and count($_POST['movie_year']).
or you can add indexes
<input type="text" name="movie_name[0]" />
<input type="text" name="movie_director[0]" />
<input type="text" name="movie_year[0]" />
<input type="text" name="movie_name[1]" />
<input type="text" name="movie_director[1]" />
<input type="text" name="movie_year[1]" />
Based on useful discussion with Cheery, I came to conclusion that the best and safest way is to use
<input type="text" name="movie_name[i]" />
<input type="text" name="movie_director[i]" />
<input type="text" name="movie_year[i]" />
where we define each i with jQuery to serialize the fields SAFELY. This way, we can be sure that serialized arrays are parallel and well matched, without misplacing.
You can do something like this:
<input type="text" name="movie1_name" />
<input type="text" name="movie1_director" />
<input type="text" name="movie1_year" />
// OTHER:
<input type="text" name="movie2_name" />
<input type="text" name="movie2_director" />
<input type="text" name="movie2_year" />
And do this to all... In Jquery, you create a function that create field as needed... I'm not the best in JQuery so I can't help you for this but the way I told you worked fine for me with PHP...