I have a form and a button ... when the button is pressed in the form of adding the following fields:
<input type="text" name="address" />
<input type="text" name="city" />
and if once again pressed the button in the form is again added field
I do not need to add in the estate []
<input type="text" name="address[]" />
<input type="text" name="city[]" />
to get an array, because each field additions are stored in another table
how to sava all data in database
and it's all on laravel framework
if you want to use the same Name element, then you'll need the brackets []
You can add an index $i like :
<input type="text" name="address[$i]" />
<input type="text" name="city[$i]" />
And then when you receive your data, you can use the same loop to get store your data in an array. Let me know if you need more details.
Related
I need to add 2 input types for 1 form field in my html form viz. type=email and type=number for the html form field Email/mobile. How do I get this done? Below is the code:
<label for="modlgn-username">Email/mobile</label>
<input type="email" name="email" class="inputbox" size="18" required />
Is it possible to add input type=number as well in the above code for the same field namely [Email/mobile]?
I'm a beginner and will apprecaite help in the matter a lot
No it is not possible but you can take input type="text" for email/number.
Just to clarify, are you asking if you can assign two input types to the same field?
I have never come across this and pretty sure it isn't possible. The best you can do to capture an email address or a phone number in the same field would be to make the field input type text. However, this would mean that your validation would get a bit harder.
Also, a side note. To ensure your form fields are accessible to users using assistive and adaptive technology (see https://www.w3.org/WAI/tutorials/forms/) you want to make sure that your label "for" and your input "id" values match exactly. In the example below "firstname" is used to do this.
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname">
This will help the screen reader associate the label with the form field.
Cheers,
Choppie
All HTML inputs Types
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Check Zedxtra.com.ng for how to use HTML tags
I'm working on a form that begins with a search auto-complete (ajax, jQuery UI) field that allows users to select a record that already exists in the database. When a record is chosen, fields in the rest of the form are completed for the user. This works well.
Within the form there is a group of fields that together describe a 'widget.' This is an example of the markup that exists before an auto-complete is fired;
<fieldset id="widgets">
<div id="widget_1">
<input type="text" name="widgets[0][name]" />
<input type="text" name="widgets[0][color]" />
<input type="text" name="widgets[0][shape]" />
<input type="text" name="widgets[0][size]" />
</div>
</fieldset>
I've structured the name attributes in this manner hoping that I'll get a nice structure on the back-end when the form is submitted. This markup exists when the page loads to allow users to add a widget to a record if none currently exist.
I'm imagining that when additional widgets are added the structure will become something like this. I should note that there should always be a group of widget fields empty at the bottom in order for users to add a new widget to an existing record.
<fieldset id="widgets">
<div id="widget_1">
<input type="text" name="widgets[1][name]" value="Alpha" />
<input type="text" name="widgets[1][color]" value="Red" />
<input type="text" name="widgets[1][shape]" value="Triangle" />
<input type="text" name="widgets[1][size]" value="Large" />
</div>
<div id="widget_2">
<input type="text" name="widgets[2][name]" />
<input type="text" name="widgets[2][color]" />
<input type="text" name="widgets[2][shape]" />
<input type="text" name="widgets[2][size]" />
</div>
...
</fieldset>
The auto-complete ajax returns a nice JSON structure that includes an array for every record with all the widgets to parse through.
What's the best way to handle adding grouped widget fields to this structure? Is adding an incrementing id to the wrapper a good way for me to keeps tabs on what's-in-which row (or would data-attributes be better)?
I'm just trying to get a better handle on how I should tackle this.
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
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 "!
I am trying to enter a record in the database. This record consists of the following :
1) Unique facebook id
2) name
3) Favorite car of the person
While the first parameter I get using the graph API from facebook, the other two are entered by the user using HTML forms using some code like this :
<form action="insert.php" method="post">
Name <input type="text" name="Name" /></br>
Fav car: <input type="text" name="car" /></br>
<input type="submit" />
</form>
Now the "insert.php" script will insert the record with the database. How can I pass the first parameter, i.e. the ID, which is something that insert.php needs, but it is not a user-entered parameter.
Use a hidden parameter as follows (I assume your PHP variable containing the ID is $facebookId):
<form action="insert.php" method="post">
<input type="hidden" name="facebook_id" id="facebook_id" value="<?php echo $facebookId?>"/>
Name <input type="text" name="Name" /></br>
Fav car: <input type="text" name="car" /></br>
<input type="submit" />
</form>
You could also use pure Javascript (I assume you stored the Facebook GraphID response in facebookResponse)
<script type="text/javascript">
$(document).ready(function() {
document.getElementById("facebook_id").value = facebookResponse["id"];
}
</script>
Pass it as a hidden input from within your form.
<form action="insert.php" method="post">
Name <input type="text" name="Name" /></br>
Fav car: <input type="text" name="car" /></br>
<input name="fbID" type="hidden" value="{value_to_pass_here}" />
<input type="submit" />
</form>
This way the fbID input won't be exposed in the front end of your website but will get POSTed to your server side script along with other input parameters, and you can access it in index.php at $_POST['fbID'];
The value you want to pass, in your case the FB Key, should be in the value attribute of this input field.