digestive-functors to parse multiple inputs with the same name - haskell

I'm trying to use digestive-functors to parse a form with a variable number of dynamically generated inputs, something like this:
<form>
<input type="hidden" name="object-id" value="123">
<input type="hidden" name="object-id" value="43">
<input type="hidden" name="object-id" value="467">
</form>
But am unsure on how to do this. I see there's a listOf function, but it looks like it requires the input names to have an index in them and be created all at once, which I don't want, since these inputs are being populated dynamically.
The haskell type is something like:
data Form = Form { objectIds :: [Int] }
Any ideas?

Related

Check if HTML attribute is inside quotes with Beautifoul Soup 4

Take for example this two tags:
<input type="submit" value="aValue" />
and
<input type="submit" value=aValue />
I'm trying to get a True/False if a tag has an attribute without quotes. Using Beautifoul Soup 4 find method it prints me always aValue between quotes, event if I give second tag as input.
There is a way to catch tags with attribute without quotes?

XPath Cannot locate element using text()

following code:
<label>
<input class="class1" type="checkbox" checked="checked"/>
First text
</label>
<label>
<input class="class2" type="checkbox" checked="checked"/>
Second text
</label>
<label>
<input class="class3" type="checkbox" checked="checked"/>
Third text
</label>
What I want to do is to get specific label element by containing text. I was trying:
//label[text()='First text']
and
//label[contains(text(),'First text')]
but it doesnt work.
Please, advise!
Thanks! :)
//label[text()[contains(., 'First text')]]
Your attempt
//label[contains(text(),'First text')]
does not work because the <label> in
<label>[
]<input class="class1" type="checkbox" checked="checked"/>[
First text
]</label>
has two text nodes: an empty one containing nothing but a line break, right before the input, and a non-empty one after the <input>. I've outlined them with square brackets above.
A call like contains(node-set, string) forces a conversion of the first argument to contains to string.
Converting a node-set to string gives you the text content of the first node of the set. (Try it out, string(label) will give you 'First text' with a bunch of whitespace, no matter how many labels there are.)
And in your case, that's the empty text node, so contains(text(),'First text') will never succeed.
Therefore you must test the text nodes individually, and that's done by nesting predicates like shown above.

xpath to get checkbox inside label

I have this code
<label>
<input type="radio" checked="checked" value="fOejPdlZIx83HA" name="btnRad">
Test1
</label>
<label>
<input type="radio" checked="checked" value="fdsaf4waff4sssd" name="btnRad">
Test2
</label>
<label>
<input type="radio" checked="checked" value="fg43fasd43wsat4" name="btnRad">
Test3
</label>
I wish to access the radio button depending on the label text via xpath
I already tried multiple thing:
//input[#name='btnRad]']/following::*[contains(text(),'Test3')]
//label[text()='Test3']/input[#name='btnRad']
//*[contains(text(),'Test3')]
Even the last one return me nothing, so xpath think that "Test3" is not the text of the label... anyone have an idea how to do this?
Your expression is failing because your label has more that one text node: an empty string before the input, and Test3. The way you're using contains means it will only check the first text node, ie empty string.
Two ways of solving this:
eliminating the empty strings with normalize-space():
//*[contains(text()[normalize-space()], 'Test3')]
querying each text():
//*[text()[contains(.,'Test3')]]
For a more detailed explanation, see How to search for content in XPath in multiline text using Python?.
This works also //label[contains(.,'Test3')]/input .

Is there anyway to pass parameter from snippet to chunk

I am learning modx and stuck at a point. I want to post "uid" value to next page via POST only and want to set hidden field which will contain "uid" as a value. I believe practically its not allowed to pass values to chunk.
I want to know whats the proper way so i can get POST data and use that value in chunk ??
My procedure
1) I have created resource (document) which contains call to snippet and then chunk
2) snippet contains value retrieved from POST
3) chunk contains a form and fields, I want to inject that POST value in this form.
There are several ways you could do this.
1) Return the uid value directly from the snippet (let's call it getPostData), and place the snippet call in your hidden field in the chunk like this:
<input type="hidden" name="uid" value="[[!getPostData]]" />
Note the snippet is uncached ([[! opening tag) otherwise the first form submission will be cached.
2) Place the snippet call in the chunk tag and have the value passed into a placeholder:
[[$myChunk?uid=`[[!getPostData]]`]]
...and in your chunk set an uncached placeholder for 'uid':
<input type="hidden" name="uid" value="[[!+uid]]" />
3) Recommended: Use setPlaceholders() in your snippet to output content to placeholders anywhere in your page - you can use it to output to multiple placeholders:
<?php
// please sanitise your POST values, this is just an example
$placeholders = array();
$placeholders['uid'] = $_POST['uid'];
$placeholders['email'] = $_POST['email'];
$modx->setPlaceholders($placeholders);
...and your chunk:
<input type="hidden" name="uid" value="[[!+uid]]" />
<input type="email" name="email" value="[[!+email]]" />
Documentation: http://rtfm.modx.com/display/revolution20/modX.setPlaceholders

A script to convert text in a form to query?

I'm a total newbie and want to start with php. I know some javascript already.
I want to be able to type some text in a form and convert it to a query e.g.
In my website there's this search box, I type in 'example' click submit and it gives me this=
http://www.externalsite.com/search?s=example&x=0
and pastes it to the address bar, you know like a search engine.
Any guidance will be appreciated.
Well, as you are doing PHP, you should point your form to submit to a PHP file. Then to retrieve the data, use $_GET or $_POST depending your form is posting or getting (as I can see from your exemple its a GET) so something like this :
HTML :
<form method="get" action="search.php">
<input type="text" name="q" id="q" value="" />
<input type="submit" value="Submit" />
</form>
On the PHP side :
<?php
$query = $_GET['q'];
header('Location: google.com/search?q=' . $query . '%20term');
die();
?>
Basically you're typing your search term into a form, which then posts (via GET) to a search page, which queries its database for records matching that string. A simple example of this follows:
index.php
<form method="get" action="search.php">
<p><input type="text" name="terms" /></p>
<p><input type="submit" value="Search" /></p>
</form>
When you submit that, it will direct you to search.php?terms=[terms here]. Our code found within search.php follows:
search.php
mysql_connect($host, $user, $pass) or die(mysql_error());
$terms = $_GET["terms"]; // you'll want to sanitize this data before using
$query = "SELECT col1, col2, col3
FROM tablename
WHERE col1 LIKE '%{$terms}%'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
print "We've found results.";
} else {
print "No results found.";
}
This is a very simple example (don't copy/paste this into production). Essentially you're pulling the submitted value(s) into a query, and then showing any results. This should be enough to get you started, but feel free to visit us here if/when you have more specific questions in the future.
Best of luck!

Resources