I need to pass an id along with a form field e.g
<input name="__field_name" value="1234" />
this only passes the name and value as a key => value pair. i need to keep the name (dynamically entered by the user) and value intact for later use, but i also need to pass an ID along with this var.
how can i do this cleanly? i was thinking putting it in the name and doing a regex to seperate it e.g.
__field_name__ID
although this seems messy...
points to consider:
there are allot of post variables that are generated by the CMS (wordpress) that i wont use
name must be retained in original format along with value
Why not submit the data as an array?
Instead of calling your field __field_name__id or some mess, use the facilities PHP provides: Call your input field field_name[id] and when the form is posted back to the server, PHP's $_POST array will have a sub-array called field_name which contains the key=>value mappings you'd mentioned.
If you have two such fields you want to tie together, use the following:
<input type="text" name="myFields[id]" />
<input type="text" name="myFields[name]" />
And on postback, PHP will provide you with a $_POST['myFields']['id'] and $_POST['myFields']['name'].
You could add a hidden field which contains the input field name value.
<input type="text" name="field_1" />
<input type="hidden" name="field_1_name"/>
You need to add a hidden form field which contains the Id of the first field. You can name it as field1_ID or something.
<input type="text" name="first_field" value="As_Entered_By_User"/>
<input type="hidden" name="first_field_id" value="id_first_field"/>
Or if you are familiar with Javascript, You can post it using javascript with single form field putting the id as an attribute.
<input type="text" name="first_field" id="first_field_id" value="as_enteredBy_user"/>
<script>var id_to_post=document.form1.first_field.id;</script>
here form1 is the name of the form containing the input box.
Related
I have a form that contains dynamically generated fields. There can be an arbitrary number of fields in the submitted form, depending on how many fields are added by the user. Here is a JSFIDDLE that you can refer to know what kind of fields i am generating dynamically. LINK TO JSFIDDLE
the only difference in this JSFIDDLE and my project is that instead of generating 2 dropdownsand 2 input fields in one set i am generating 3 dropdowns and 1 input fields in one set. number of sets that can be generated is maximum of 15 by now that the user can select from the dropdown in jsfiddle.
What is need to know is if a user generated 7 sets, how would i know the name of each field as i have to use PHP to process the data of this form and submit to a database.
have a look at this. It adds a variable to the end of each input, so you can keep track of the inputs.
Edit1: the names will be as such,
row1 - input1_1, input2_1, input3_1, input4_1
row2 - input1_2, input2_2, input3_2, input4_2 ...and so on
Edit2: this jsfiddle removes the latest row(s) added.
Your jQuery code is already explicitly naming the fields. Eg:
$("<input>").attr("name", "a").appendTo(newDiv);
This will create an input named a:
<input name="a" />
Your jQuery gives each dynamically created input the same name. If you submit the following form with 3 fields having the same name, the server would receive a=value1&a=value2&a=value3.
<form>
<input name="a" value="value1" />
<input name="a" value="value2" />
<input name="a" value="value3" />
<input type="submit" />
</form>
This works with most server technologies, but not with PHP. PHP overwrites existing values with the last value. So, if you examined the value of a in PHP, you would only see value3. But, the good news is that you can change your input names slightly and have PHP recognize all the values. In PHP, when you append square brackets to your input names (name="a" becomes name="a[]"), PHP parses the values into an Array. So for the following slightly modified form:
<form>
<input name="a[]" value="value1" />
<input name="a[]" value="value2" />
<input name="a[]" value="value3" />
<input type="submit" />
</form>
When you look at the value of a in PHP, you will get an array of values containing value1, value2, and value3. So, just change your jQuery code to use square brackets at the end of the name:
$("<input>").attr("name", "a[]").appendTo(newDiv);
hi Iam trying to create a wordpress plugin option page.I need to create a form with input fields having default values in it and when I change the value and save it, the changed value should be reflected in the input filed and also in the variable which are assigned to store the value.
let me be more precise
When i change the value of the input field and save it it should be stored in the assiged variable value( permanently till i again change it myself).
Iam a bit poor in form validation and php.help me out guys.
If you're OK with it only working in newer browsers, you could use the placeholder attribute on the text field, to supply a placeholder that will disappear when you focus and reappear when you blur.
<input type="text" name="txtfield" value="" placeholder="input your text" />
If you want it to work in older browsers, do it in javascript:
<input type="text" name="txtfield" onfocus="if(this.value=='Enter Email')this.value=''" onblur="if(this.value=='')this.value='Enter Email'" >
Then you can use <?php $value = $_post['txtfield'] ?> to assign the input value to a variable.
I have an HTML input field, and I have every input field specified with some id, now how can I use this input field id to identify the field.
Example:
<input type='text' id='float' name='attributename' value='' maxlength='30'/>
I need to verify the id of input field for float and then insert the input field's value in to db in particular field.
Please help me out..
You can't. When POSTing data to the server, the name attribute is used. The id has nothing much to do with PHP.
It depends which method you use for transferring the data (specified by the method attribute of the form element).
E.g. with:
<form method="POST">
<input type='text' id='float' name='attributename' value='' maxlength='30'/>
<input type="submit" />
</form>
you can access the data in PHP via $_POST['attributename'] when the form is submitted. $_POST is an associative array that holds the data send via a POST request.
Note: The name of the input element is the important property, not the ID. The ID is not transferred to the server, it plays only a role in the DOM. Only the name and the value is send to the server. So if you probably want to set name="float".
Further information: Variables From External Sources. You also might want to read about $_POST and $_GET (GET is the other method. This is used to send data via the URL).
When PHP gets the data by GET or POST, it doesn't know the id of the input that was associated with a given piece of data. You do, however, know the name.
The simplest approach is to make the name and id match, or be able to derive the id from the name, like so:
name: float
id: float_id
name: address
id: address_id
The reason is that the DOM uses the id, but it has nothing to do with PHP's execution. PHP only gets the name. Since you have control of both server-side and client-side code, you can determine what the id will be by choosing a naming convention as I suggested above.
When you submit the form you would use the input name="" attribute to pull the value from:
$_POST['attributename'];
First of all, an id should be unique for each element.
You can suffix field name with [] to create an array so that you can process them later like:
<input type='text' id='float1' name='attributename[]' value='' maxlength='30'/>
<input type='text' id='float2' name='attributename[]' value='' maxlength='30'/>
<input type='text' id='float3' name='attributename[]' value='' maxlength='30'/>
Now from PHP, you can use foreach to access each field value like this:
foreach($_POST['attributename'] as $value){
echo $value . '<br>';
}
If it is a single field though, you can access it just by its name:
echo $_POST['attributename'];
In PHP, you only have access to the name of the input, after the form has been submitted using method="get" or method="post", through $_GET['attributename'] and $_POST['attributename'], respectively.
Maybe you mean to target the field using javascript; you can do that by using document.getElementById('float').
Yes, deceze put it best.
Basically, everything in PHP (and all of web programming) requires key = value pairs. So, if you have no key, you in turn have no value. If the form element has only an ID and no NAME, then the variable that is passed to PHP will not exist.
The best way to test, is to have your HTML form element test with a "GET" instead of a "POST", this way, you will see the key=value pairs in the address bar when the form is submitted to the method by the action. All then you must do is use $_GET instead of $_POST to pull the variables.
What would be the best way to intercept multiple fields via PHP? Currently I have a login.php file which is pretty simple:
<form method="POST" action="auth.php">
Code:<br />
<input type="text" name="code" />
<input type="submit" id="submit" value="Submit" />
<br />
Pass:<br />
<input type="text" name="pass" />
<input type="submit" id="submit" value="Submit" />
</form>
Then in the auth.php I get the value via the POST:
$value = $_POST['code'];
The problem with this is that I would have a quite amount of fields, and every field would have a submit button assigned. Then I would need a if condition for every field name avaible, which I don't want to. What would be the best way to handle this?
Thanks
Just use a single submit button. There's no reason to have more than one here.
If you have multiple related fields you can use array naming:
Primary email: <input type="text" name="email[]" >
Additional email: <input type="text" name="email[]">
and access from php using
$emails = $_REQUEST['email'];
However, you should not use arrays like this for unrelated parameters just because you're too lazy to use multiple field names. If you do you're just writing terrible, unmaintainable code.
There's a couple of ways you can simplify this problem.
Here's one approach:
$fields = array('field1','field2','field3','field4','field5'); // Add all your field names to an array
$data = array();
foreach ($fields as $field) {
if (isset($_POST[$field])) {
$data[$field] = $_POST[$field];
// If you wanted it assigned to a local variable instead,
// you could do it like this, although this pattern is
// generally frowned upon:
${$field} = $_POST[$field];
}
}
The danger of assigning it as a local variable is that it can overwrite a variable that already exists. It could potentially overwrite something crucial to the rest of the application. However, because the field names are explicitly defined in the array, you do at least maintain control over this.
You would definitely NOT want to iterate through the $_POST array and assign each array key to a local variable -- doing so would leave you wide open for hackers.
You have a bad problem with HTML.
In HTML you don't need more than one submit button.
A submit does not submit a field, but a whole form.
You don't even need it, as most user-agents will submit a form if you press Enter on a textfield.
Anyway, your fields will have an attribute name, unique within the form, and shall have an id attribute unique within the document.
When they are sent, all data can be accessed in PHP as an array.
The superglobal $_POST in your case, or $_GET if that method was used to submit it.
There, you do your magic.
is there a way to create custom post variables when a user presses submit, like this:
$_POST['var'] = 'hi';
In order to set post values on the page with the form you should use hidden input tags.
i.e.
<input type="hidden" name="var" value="hi" />
It will be invisible and your receiving script will see that key/value passed along.
Variables POSTed by the browser to your PHP script will only correspond to the fields of the form that was used in the browser -- which means you have to put your custom data in that form.
If you don't want them displayed, you can use a hidden input field :
<input type="hidden" name="var" value="hi" />
But note that the data will still be sent by the browser -- which means you have to escape/filter/protect it, like any other value that comes from the user ; and it cannot be trusted : anyone can pretty easily modify the value of that form field, even if it's not visible.
while $_POST variable is an array, you can also define var like this
$_POST['var'] = 'hi';
it is same like hidden field. :)