I have this HTML:
<form name="some_form" method="POST">
<input type="checkbox" name="formcheckbox[0]" value="true">Some label to get it out</br>
<input type="checkbox" name="formcheckbox[0]" value="true">Some label to get it out 2</br>
</form>
So how can I get out the labels DOM gives me only the checkboxes but no text to indicate a label.
EDIT 1
HTML is as is, since it is used from external service, so I cannot edit him, but I would still like to get labels!
There are no <label>s there. It is very poor HTML (it also has end tags for br elements but no start tags).
Presumably, you mean "The text node next to the input"
The only thing you can do is to find the input and then get its next_sibling.
$elements = $dom->get_elements_by_tagname("input");
$element = $elements[0];
$text_node = $element->next_sibling();
(Since you have multiple, you'll want to use a loop instead of hard coding 0).
Related
There is an HTML file (whose contents I do not control) that has several input elements all with the same fixed id attribute of "search_query". The contents of the file can change, but I know that I always want to get the second input element with the id attribute "search_query".
I need an XPath expression to do this. I tried //input[#id="search_query"][2] but that does not work. Here is an example XML string where this query failed:
<div>
<form>
<input id="search_query" />
</form>
</div>
<div>
<form>
<input id="search_query" />
</form>
</div>
<div>
<form>
<input id="search_query" />
</form>
</div>
Keep in mind that that the above is merely an example and the other HTML code can be quite different and the input elements can appear anywhere with no consistent document structure (except that I am guaranteed there will always be at least two input elements with an id attribute of "search_query").
What is the correct XPath expression?
This is a FAQ:
//somexpression[$N]
means "Find every node selected by //somexpression that is the $Nth child of its parent".
What you want is:
(//input[#id="search_query"])[2]
Remember: The [] operator has higher precedence (priority) than the // abbreviation.
This seems to work:
/descendant::input[#id="search_query"][2]
I go this from "XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition" by Michael Kay.
There is also a note in the "Abbreviated Syntax" section of the XML Path Language specification http://www.w3.org/TR/xpath/#path-abbrev that provided a clue.
There is an HTML file (whose contents I do not control) that has several input elements all with the same fixed id attribute of "search_query". The contents of the file can change, but I know that I always want to get the second input element with the id attribute "search_query".
I need an XPath expression to do this. I tried //input[#id="search_query"][2] but that does not work. Here is an example XML string where this query failed:
<div>
<form>
<input id="search_query" />
</form>
</div>
<div>
<form>
<input id="search_query" />
</form>
</div>
<div>
<form>
<input id="search_query" />
</form>
</div>
Keep in mind that that the above is merely an example and the other HTML code can be quite different and the input elements can appear anywhere with no consistent document structure (except that I am guaranteed there will always be at least two input elements with an id attribute of "search_query").
What is the correct XPath expression?
This is a FAQ:
//somexpression[$N]
means "Find every node selected by //somexpression that is the $Nth child of its parent".
What you want is:
(//input[#id="search_query"])[2]
Remember: The [] operator has higher precedence (priority) than the // abbreviation.
This seems to work:
/descendant::input[#id="search_query"][2]
I go this from "XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition" by Michael Kay.
There is also a note in the "Abbreviated Syntax" section of the XML Path Language specification http://www.w3.org/TR/xpath/#path-abbrev that provided a clue.
The page I'm currently working on has an "add another" option, which inserts a fieldset containing a title and a list of 3 input boxes. Each of these input boxes, for form submission sake, needs to be given a name with an incremented value. ("field1_1", "field1_2" etc)
I know that I could add the fields by dynamically creating elements with $('<input/>', {...}); but for more than a few elements this makes for code that's hard to read and difficult to maintain.
I'm currently using jQuery's .load() function to pull in the file but through searching I can't find a way to pass variables to the response, and therefore can only use preset name attribute values.
Is this possible, or is the method above the only way?
Put your template into a dummy script, with unknown type to avoid errors:
<script id="template1" type="text/template">
<h2>{title}</h2>
<input type="text" name="field{id}_1">
<input type="text" name="field{id}_2">
<input type="text" name="field{id}_3">
</script>
Then use replace (with "g" global option as replace usually replaces first only) to insert your values.
var template = $('#template1').html();
template = Template.replace(/\{title\}/g, mytitle);
template = Template.replace(/\{id\}/g, nextId);
$('#somewhere').append(template);
You can of course concatenate the replaces, but going for readable here.
This method allows complex HTML without messing up the code with loads of string manipulation. Your template looks like the final result so no mental translation required. Very low-maintenance technique.
The placeholders can just be simple names (without braces) to avoid regex delimiters, so long as the names will not match anything else in the template. I just use braces so the placeholders stand out in the template (again for maintenance purposes).
given a form like the following
<form action="/page" method="POST>
<input type="hidden" name="input" value="12" />
<input type="hidden" name="input" value="24" />
</form>
Using Google Chrome 31.x and PHP 5.5, /page now has a $_POST variable for input of 24
This happens because when the $_POST array is created, The value is over written in the array. And the latter value is the value which is preserved.
Most browsers Ive tested this is the case, But Is there any HTTP spec / browser spec which says that form inputs should be sent in the order they are defined ? Or could an update in the future (or an old browser) send these updates in the reverse order for example ? or a random order ?
Edit:
to give more context, It will not be used like the above in all cases. only in a certain case.
The first form element is a SELECT box, But depending on the options chosen, Javascript will be able to change the value, Without changing the Select box value
Regardless of the order in which HTTP sends your two values, PHP can only have one value for $_POST['input'].
To solve this, use array notation:
<form action="/page" method="POST>
<input type="hidden" name="input[]" value="12" />
<input type="hidden" name="input[]" value="24" />
</form>
Now you'll have an array $_POST['input'] with both values.
To answer your question about the spec, see this page:
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
See the bullets for the application/x-www-form-urlencoded default content type.
The control names/values are listed in the order they appear in the document. The name is separated from the value by '=' and name/value pairs are separated from each other by '&'.
To my knoweledge there is no specification as to the order in which a browser should parse the from before submitting.
But I would say that you can pretty much assume that the form fields will be parsed from top to bottom, because the whole dom is parsed like this.
Here is a little bit additional information as to how a form submit is processed/handled.
http://www.w3.org/TR/html401/interact/forms.html#successful-controls
Steve
I have a textarea and users can type in it. I want to store what they type in a MySQL database but I want to output it with the right amount of spaces like HTML's <pre></pre>. How can I save their input in a database without ruining these spaces?
My code so far just looks like:
<form action='index.php' method='POST'>
<textarea id='area'></textarea>
<input type='submit' value='submit'>
<?php
$input=$_POST['input'];
//mysql query goes here ()
It has been a while since I did this, but here's what I remember: the database doesn't remove the spaces. Two things control the spacing: the textarea's wrap command and the way you show the data later when you retrieve it from the database.
For my forms (that do this successfully) I use textarea wrap="soft"
Then when you take it out of the database you can use pre tags to show it or if you are using php you can use nl2br() on the text to change newlines to html break tags.