Store the content of an HTML element into a PHP variable? - php

I want to store the content of a span element into a PHP variable. I want to use this in a relationship to the editable content tag. So that I finally have something like an input field. (In my project, I don't want to use normal input fields)
Thank you

"something like an inputfield"
You can use this:
<input type="hidden" name="yourname" value="thevalue" />
Hidden property sets this will not be visible
http://webdesign.about.com/od/htmltags/p/input-hidden-tag.htm

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.

POST a custom Tag?

I have the following input in a form
<input name="email" type="text" id="email"size="50" english="Email address" />
I have a custom tag called english, My question is can I send this as post data and can I recover it on my new page ?
Any help would be much appreciated , Thanks
If you use JavaScript to submit your form, you can read you custom tags' values ad append them to the form data to send. Otherwise, clean HTML form just submits only input tags value.
The best method I can think of right now is to have hidden field with the label as value. Like
<input name="email_label" type="hidden" id="email_label" value="Email address" />
The short answer is: no. The post data received from the HTML in your question will be an array with email as the key, and whatever the user typed as the value.
The solution depends on the problem you're trying to solve. Consider using a hidden input tag instead. For example:
<input name="language" type="hidden" value="English" />
Alternatively, a neater solution would be to store the language in the session (assuming that does what you need). You should never rely on the front end of a website "telling" the back end stuff like this, at least to a certain degree. The back end should just "know".

jQuery autocomplete on multiple input fields

I am using jQuery auto-complete. I have download this script from http://code.google.com/p/jquery-autocomplete/. I want to use this on multiple fields. Please help me thanks.
$("#input").autocomplete("samefile.php");
$("#input").autocomplete("samefile.php");
thanks
the hash mark means you are using IDs to select elements. there should however never be more than one element in your page with the same ID. for instance,
<input id="test" /><input id="test" />
is invalid HTML.
The second problem, is that it appears you are trying to find tag names, which means you should simply leave out the hash mark from your code, and JQuery will apply your methods to all of the tags with that tag name,
$("input").autocomplete("samefile.php");
will apply autocomplete to all input tags on your page.
Third, I would use classes instead of tag names incase you ever want to have an input on your page that does not use the same auto complete. So your html would look like this,
<input class="auto" /><input class="auto" />
and your JQuery would look like this.
$(".auto").autocomplete("samefile.php);
I also wonder where you are calling your JQuery from?
You should use a less specific selector to mark multiple fields as autocomplete in one statement.
Maybe you can assign a class of type ".autocomplete" and then use that.
<input type=textbox" name="txt1" class="autocomplete"/>
<input type=textbox" name="txt2" class="autocomplete"/>
$(".autocomplete").autocomplete("samefile.php");

Remove text from fields on input

How do I make the text disappear like the way some fields work here on stackoverflow once I start putting in text? I tried out the different 'on'-actions in HTML but that didn't really do what I wanted.
Thanks.
You can do it with onfocus/onblur events. For example:
<input type="text" value="search" onfocus="if(this.value=='search')this.value=''"/>
If you click on this input field, the default text "search" will disappear. The onfocus event handler checks if this input field has the value of "search" and if so then clears it, in other cases (user has already typed something in) leaves everything as is.
Presumably you mean "Labels which appear inside the input".
If you want to do this in a sane, accessible, semantic way — use a <label>, if JS is available then position it under the element, and onfocus/onblur change classes around based on the value of the element.
I knocked up a simple example at http://dorward.me.uk/tmp/label-work/example.html using jQuery (all the source that isn't part of the jQuery library is embedded in the HTML of that document for easy reading).
jQuery would make this easy work;
http://www.jsfiddle.net/TshDN/
If you are using HTML 5 you could use the placeholder attribute.
http://dev.w3.org/html5/spec/Overview.html#the-placeholder-attribute
use the onFocus() in javascript
<input type="text" onfocus="if(this.value == 'value') { this.value = ''; }" value="value" />

Pass html variables to PHP using "ID" not "Name"

I have HTML code like below
<input type = "textarea" id="sentence1"> Here is my sentence </textarea>
<input type="hidden" name="sentence2" value="This is another sentence">
PHP:
$_POST['sentence1'] //shows nothing
$_POST['sentence2'] // works fine
I want to get the value of sentence1 also But i have such a scattered code that for textarea i can't change "id" to "name" otherwise i'll have to make lot of changes in different files.
I have to transfer both sentences to PHP so please help me how can i do that?
There is no input type="textarea". You cannot use the id attribute for the name attribute. This is not how it will get transmitted and there is no way to change that from plain HTML. Form <input> elements are transmitted with their name and value attributes.
Please refresh you knowledge about HTML Form elements and attributes
Your code is wrong. You should start using a browser with HTML validation, that would've caught it.
<textarea name="sentence1" id="sentence1"> Here is my sentence </textarea>
<input type="hidden" name="sentence2" value="This is another sentence" />
Change
<input type = "textarea" id="sentence1"> Here is my sentence </textarea>
into
<textarea id="sentence1" name="sentence1"> Here is my sentence </textarea>
and your PHP will work.
I'm assuming you've corrected the <input type="textarea"> mistake. If I understand your needs correctly, you know that the form only passes the value of the name attribute, and not the id, but you can't change that easily.
My suggestion would be to add a script to either change the value of the name attribute into that of the id, or copy the entire attribute so it's available under both the name and the id:
<script>
var inputs = document.getElementsByTagName('input');
for (var i=0; i<inputs.length; i++)
inputs[i].parentNode.appendChild(inputs[i].cloneNode(false));
</script>
and add it at the end of your pages. do the same for textareas.
You can't (at least not using plain HTML). The W3C recommendation regarding form handling and processing states
A control's "control name" is given by
its name attribute. The scope of the
name attribute for a control within a
FORM element is the FORM element.
and
A form data set is a sequence of
control-name/current-value pairs
constructed from successful controls
See here.
You can use Javascript to add the name-attribute dynamically to all form elements without the respective attribute.
But i have such a scattered code that for textarea i can't change "id" to "name" otherwise i'll have to make lot of changes in different files.
Then probably that's where you should start. (Note: A input/textarea element may have both a name and an id attribute.)

Categories