How can an id of input field can be read by PHP? - php

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.

Related

Get data attribute value in Symfony controller

In my View, I have this hidden field
<input type="hidden" id="logoID" name="logoID" class="logoID" value="123">
I add additional data to it using data attribute via jQuery like this
$('.logoID').data('fileName', 'xyz.jpg')
// adds data attribute to input element -> <input ... data-fileName='xyz.jpg' />
Now I need to retrieve this fileName inside my controller. I know I can get value of this hidden field by
$form['logoID']->getData(); // 123
But I also need the value of the data-attribute (fileName). How can I go about it? Any leads are much appreciated.
data-attributes are not available directly by the PHP scripts. You need to send that value using another hidden input or retrieve the value with Jquery later and post it as data using Ajax.

Hidden input (html form confirmation) - Server side script? (php)

I'm writing a site in PHP.
And I have a simple registration form.
<form>
<label...></label> <input .../>
</form>
I want to add a confirmation field:
<input type='hidden' name='hiddeninput' value="jn3kjnv3kjvn35">
But how does this code look on the server side?
Do I need to save every hidden value to the database whenever registration form is loaded?
I'm trying to make sure the form is not being filled in by bots.
That's why I need a random hidden value that is unique for every form submission.
Suppose every time I generate the registration page - I generate the unique value for "hidden" field.
When the user submits the form - how do I compare the submitted value to the one that was generated (as once it's generated - it's not stored anywhere in the site).
Basicly , you should use a function that generates a random string (hash for example)
and sessions to "remember" this string.
Aftet submitting the form , you'll check the INPUT's value and cross it with the SESSION's value.
For instance:
Form.php
<?php
$hash = md5(time());
$_SESSION['form_xx'] = $hash; //in case you have more than one form.
?>
<form method='post' action='do.php'>
..
..
<input type='hidden' name='secret_key' value='<?=$hash?>'>
</form>
do.php
if($_POST['secret_key'] == $_SESSION['form_xx']) //Just make sure your making the posted value SAFE
//He's ok.
else
die("arggg...those hackers");
You need to add a name to the field, too. It works like pretty much any other HTML field.
I now use Ruby on Rails framework that solves it out of the box.

Passing extra data in $_POST vars

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.

Can I include a forms value into the action redirect in php?

Is it possible in php to include a forms value into the action redirection?
For example:
<form method='POST' name='Select' action='customer.php?CID=xxxxx'>
<input type=text width='5' name='searchVal' />
where xxxxx is the value entered into the form.
I've tried a number of different ways and I'm just not figuring it out! (Still sort of new to php) Any help would be appreciated.
It was looking like I would have to use $_POST and $_GET. A little more information might be in order... customer.php displays a list of customers in order by ID, name, etc. The user currently clicks on the customer ID that they want to display the details for. I'm trying to add a box where they can just enter the customer number to get to the details quickly, but I still want to have the listing displayed. From what it is sounding like, I will have to do this as two separate programs...is that true?
How about this:
<form method='POST' name='Select' action='customer.php'>
<input type='hidden' value='xxxxx' name='CID' />
<input type=text width='5' name='searchVal' />
...
</form>
You are free to add as much hidden values as needed.
Note, that you can even use PHP-like array notation_
<input type='hidden' value='xxxxx' name='CID[1]' />
<input type='hidden' value='yyyyy' name='CID[2]' />
At the PHP-side, access those values using this syntax:
$_POST[ 'CID' ][ 1 ]
$_POST[ 'CID' ][ 2 ]
UPDATE-1
Ah, you want to use a user-entered values to the Action URL just before the form gets submitted?
In this case you need to use JavaScript. Access the DOM to change the Action URL.
But let me ask, why you need to post a form value additionally as a parameter of the Action URL?
UPDATE-2
You wrote: 'From what it is sounding like, I will have to do this as two separate programs...is that true?'
No, actually not. You can still use one customer.php which checks at its beginning, if it was called using a linked customer in the table element or a searched customer in the search field.
In other words: You don't need to prepare two scripts, but two forms for two purposes which call the same script customer.php.
You can include the required value in a hidden field in your form:
<input type="hidden" name="CID" value="xxxxx" />
The reason this is required is that you are submitting the form to your server via POST, but appending parameters to the URL requires submission via the GET method.
Not without a post to the server. The value in the form is filled in client-side, so it has to return to the server before you can add it to the action. (at least, if you want to use php).
You can either
add it after posting (might not be usefull)
use javascript
just not use the GET CID, but get it out of the POST in your customer.php script.
I got it finally! It's actually very simple!
In the body of the code I put this:
<form action='#_SELF' method='GET' name='Projected'>
<input type=text size=5 name='CID' value='' title='Enter Customer number to display' />
<a href='#' onclick='document.Projected.submit();' title='Enter Customer number to display'>Go</a>
And at the top of the code I just do a:
if (!isset($_GET['CID'])) { ...
It works exactly the way I wanted it to!
Thanks everyone for the help! I appreciate it! (And I'm learning more and more about PHP everyday!)
Im pretty sure you cant do that unfortunately

php custom post variables

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. :)

Categories