Get data attribute value in Symfony controller - php

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.

Related

Detect type value fields from post form

I want know, if it´s possible detect when use POST in form, the value of type for each input field.
For example :
<input type="text" name="phone" value="Phone">
<input type="password" name="pass" value="Password">
I can get value and name for each input field, but, ..... how i can show for each field if type it´s text or password, as in this case
This it´s my question, because i can´t find some solution for this, howewer with jquery but no using php for get results from post form, and i need get this using php
Thank´s in advance
No, you don't receive this information. You'll need to find a workaround for what you're trying to achieve.
Just Using PHP you can't do that. You Need to use javascript for that
Instead of direct form submit onclick of submit you can call a javascript function
In that function create a array of json with field type, field name and field value data
Now post JSON data using ajax or javascript XMLHTTP Request to PHP
. .

How to get the value from a textbox of a form popup

How to get the value from a textbox of a form popup JQuery in PHP.
Here's my web page.
use like
$("#inline_content table").find("#makhachhang").val(); // by this get id value in your form and same rest
$("#inline_content table").find(".tdthemkhachhang").val(); // by this get name value in your form and same rest
$("#inline_content table :radio :selected").val(); // by this get radio value in your form and same rest
Try this:
$("#makhachhang").val();
If you want to get the data from textbox with PHP, you'll need put the HTML code inside a form, and send the data via GET or POST.
I recommend you to get the data via Javascript.
$("#inline_content table").find("#makhachhang");
You can use
if input is like that:
$("#IdOfYourInput").val() will return the containt
Basically, just add an action attribute to your form and name attribute to your inputs.
<form action="your php file">
<input name="email" />
<input type="submit" />
</form>
Then on your php file, get it with:
$email = $_POST['email']
I suggest you learn the basics first:
http://www.w3schools.com/html/html_forms.asp

Get data from AJAX - How to

I have a script, that takes the value from two select tags, and then do a simple math multipy to calculate a price. The thing I need, but cannot figure out is, how to get that calculated price, and use it as an php variable in a POST form.
As the calculated price is dynamic, how can I archive this?
If you want to send that value directly to a PHP script, you can use the jQuery post method:
$.post("yourScript.php", { price: yourPriceVar }, function(data) {
//Success! Do something interesting
});
Alternatively, you could set the value of a hidden input element to your calculated value, so that it can be submitted along with the rest of your form:
$("#hiddenInput").val(yourPriceVar);
HTML:
<input type="hidden" id="hiddenInput" name="price" />
to set the calculated price in the form use :
document.FORMNAME.FIELDNAME.value = CALCULATED_VALUE;
or jquery style
$('input[name="FIELDNAME"]').value(CALCULATED_VALUE);
in your javascript.
in the form add an hidden field IE.
<input type="hidden" name="FIELDNAME" value="" />
if you are using $.ajax() or $.post(), simply append a new value to data parameter
if you are using common form submit, simply create a new input with the name attribute inside the form and set the calculated price to the value

How can an id of input field can be read by 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.

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