ASP-Classic Get information from dynamic field - debugging

I have an old ASP-page that contains a form but for some reason, a part of the code doens't work anymore.
The code was working before because I write the content of the form in a database and there I have values from the past.
A short description of my page:
I have a [select multiple]-field and when you select one (or more) option I create another input field (for every selected option a new fied). The ID of this field is FSRow_ + an edited value of de selected option (no spaces, commas, ...).
When I submit the form I want to read the content of my new fields so I can write this information to my database.
An example:
Suppose I have two selected options, to make it easy: "OptionA" and "OptionB", then I create two new fields (from the source code):
<input name="FSRow_OptionA" id="FSRow_OptionA" type="text">
<input name="FSRow_OptionB" id="FSRow_OptionB" type="text">
When I submit my form, I have this code:
if request.form()
...
getSelectedValues = split(request("OptionList"), ",")
for each li in getSelectedValues
li = replace(li, "'", "''")
li = replace(li, "||", ",")
fsrow = trim(request("FSRow_" & replace(replace(replace(replace(replace(trim(li), " ", ""), "&", ""), "+", ""), ",", ""), "|", "")))
if fsrow <> "" then
[write to DB with dynamic field]
else
[only write selected item]
end if
next
When I look in my database. I have only the information of the selected items (so, my i'm going in my else-code). Not the content of my dynamic field. So I suppose the variable "fsrow" is always empty, even if my new field contains some text.
My questions:
How can I get the content of my dynamic input field?
Any idea what could be wrong because it was working in july 2017 and didn't change anything.
[Not needed anymore, found the solution] I've tried to set an alert or write to the console to debug but this isn't working. How can I debug when I'm in my "for each"-loop?
Edit/Update 1:
As commented by Ricardo Pontual I can use "Response.End" in combination with "Response.Write" before.
If I do this:
Response.Write(li)
Response.Write("## FSRow_" & li & " ## ")
Response.Write("->" & request("FSRow_" & li))
I get this result:
OptionA## FSRow_OptionA ## ->
But in my form/input field I wrote "test" in the field with id "FSRow_OptionA"...
Edit/Update 2:
With a non-dynamic input field I can perfectly read and write the value in my database.
So it's realy a problem with the dynamic field...

Found the problem. This was very old code, from years ago that wasn't written by me, and the problem was the position of < form> and < /form>
Old code (NOT WORKING)
<table>
<form>
<tr>
...
</tr>
</form>
</table>
Changed the code by this:
<form>
<table>
<tr>
...
</tr>
</table>
</form>
I changed the order of < table> and < form> and it works now!

Related

How to select from between header elements in Ruby

I'm working on a Ruby script that uses Nokogiri and CSS selectors. I'm trying to scrape some data from HTML that looks like this:
<h2>Title 1</h2>
(Part 1)
<h2>Title 2</h2>
(Part 2)
<h2>Title 3</h2>
(Part 3)
Is there a way to select from Part 2 only by specifying the text of the h2 elements that represent the start and end points?
The data of interest in Part 2 is a table with tr and td elements that don't have any class or id identifiers. The other parts also have tables I'm not interested in. Something like
page.css('table tr td')
on the entire page would select from all of those other tables in addition to the one I'm after, and I'd like to avoid that if at all possible.
I'd probably use this as a first attempt:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<h2>Title 1</h2>
(Part 1)
<h2>Title 2</h2>
<table>
<tr><td>(Part 2)</td></tr>
</table>
<h2>Title 3</h2>
(Part 3)
EOT
doc.css('h2')[1].next_element
.to_html # => "<table>\n <tr><td>(Part 2)</td></tr>\n </table>"
Alternately, rather than use css('h2')[1], I could pass some of the task to the CSS selector:
doc.at('h2:nth-of-type(2)').next_element
.to_html # => "<table>\n <tr><td>(Part 2)</td></tr>\n </table>"
Once you have the table then it's easy to grab data from it. There are lots of examples how to do it out there.
According to "Is there a CSS selector for elements containing certain text?", I'm afraid there is no CSS selector working on element text. How about first extract "(Part 2)", and then using Nokogiri to select table elements inside it?
text = "" //your string, or content from a file
part2 = text.scan(/<h2>Title 2<\/h2>\s+(.+)?<h2>/ms).first.first
doc = Nokogiri::HTML(part2)
# continue select table elements from doc
(Part 2) can not contain any h2 tag, or the regex should be different.
If you know that the tables will be static, and the data you require will always be in the second table. You can do something like:
page.css('table')[1].css('tr')[3].css('td')
This will get us the second table on the page, access the 4th row of that table and get us all the values of that row.
I haven't tested this, but this would be the way I would do it if the table I require doesn't have a class or identifier.
I'd probably use this as a first attempt:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<h2>Title 1</h2>
(Part 1)
<h2>Title 2</h2>
<table>
<tr><td>(Part 2)</td></tr>
</table>
<h2>Title 3</h2>
(Part 3)
EOT
doc.css('h2')[1].next_element.to_html # => "<table>\n <tr><td>(Part 2)</td></tr>\n </table>"
Alternately, rather than use css('h2')[1], I could pass some of the task to the CSS selector:
doc.at('h2:nth-of-type(2)').next_element
.to_html # => "<table>\n <tr><td>(Part 2)</td></tr>\n </table>"
next_element is the trick used to find the node following the current one. There are many "next" and "previous" methods so read up on them as they're very useful for this sort of situation.
Finally, to_html is used above to show us what Nokogiri returned in a more friendly output. You wouldn't use it unless it was necessary to output HTML.

ASP Classic : Put a "variable" in a "Request.Form()"

I have a form that will send an array of data to an ASP page.
Let's say this array is called "matrix".
Usually, on the ASP receiving the form, I will write this out to retrieve the form inputs from the array "matrix".
Request.Form("matrix[]")(i) where i = 1, 2, 3 which are the elements in the array.
Let's say I want to do make a variable like this
a="matrix"
and I want to use this variable a and put it into the request form, instead of writing "matrix", so that it would look something like this
Request.Form(a[])(i)
How can it be done? For now, all my attempts are showing blank. e.g. when I try to make them appear on the page with response.write, nothing shows up.
Please help me or let me know if it cannot be done, I've been spending hours on this.
Request.Form("matrix[]") is taking a string value of "matrix[]" not an array of strings called "matrix".
So you need to do either
a = "matrix[]"
Request.Form(a)(i)
or
a = "matrix"
Request.Form(a & "[]")(i)
Unlike PHP which requires adding square brackets, in classic ASP you just have to give the same name to the elements you want to be combined into an array.
The HTML should be:
<input type="text" name="matrix" />
<input type="text" name="matrix" />
<input type="text" name="matrix" />
Then you can iterate over the submitted values like this:
For x=1 To Request.Form("matrix").Count
Response.Write("Value of matrix #" & CStr(x) & "is: " & Request.Form("matrix").Item(x))
Next
Note that all elements are included, even if user left them empty.

Jqgrid colNames set from a jsp hidden variable

I have a jqgrid whose column Name I wish to set from a jsp hidden variable.
<input type="hidden" name="columns" id="columns" value= "<%= finalColumns %>"></input>
This hidden id columns has the values:
value="'Master Code','Mst. Code Description','Master Code Status','Master Code Groups','Detail Code Groups','No. of Detail Codes','Length','Attribute Type','sample'" .
Now I am trying in my Javascript like
colNames:'['+$("#columns").val()+']',
But I am getting an alert box saying Length of colNames <> colModel.
I have checked, there are 9 col models and 9 values are present in the string. Where am I doing some mistake
UPDATE:SOLUTION :
I tried using
eval(('['+$("#columns").val()+']').toString()).
That solved the problem. I have to understand Why this solves the problem
Thanks anyways

How can I search for a specific text element?

How can I search for the element containing Click Here to Enter a New Password using Nokigiri::HTML?
My HTML structure is like:
<table border="0" cellpadding="20" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bodyContent" valign="top">
<div>
<strong>Welcome to</strong>
<h2 style="margin-top:0">OddZ</h2>
Click Here
to Enter a New Password
<p>
Click this link to enter a new Password. This link will expire within 24 hours, so don't delay.
<br>
</p>
</div>
</td>
</tr>
</tbody>
</table>
I tried:
doc = (Nokogiri::HTML(#inbox_emails.first.body.raw_source))
password_container = doc.search "[text()*='Click Here to Enter a New Password']"
but this did not find a result. When I tried:
password_container = doc.search "[text()*='Click Here']"
I got no result.
I want to search the complete text.
I found there are many spaces before text " to Enter a New Password" but I have not added any space in the HTML code.
Much of the text you are searching for is outside of the a element.
The best you can do might be:
a = doc.search('a[text()="Click Here"]').find{|a| a.next.text[/to Enter a New Password/]}
You can use a mix of xpath and regex, but since there's no matches in xpath for nokogiri yet, you can implement your own as follows:
class RegexHelper
def content_matches_regex node_set, regex_string
! node_set.select { |node| node.content =~ /#{regex_string}/mi }.empty?
end
def content_matches node_set, string
content_matches_regex node_set, string.gsub(/\s+/, ".*?")
end
end
search_string = "Click Here to Enter a New Password"
matched_nodes = doc.xpath "//*[content_matches(., '#{search_string}')]", RegexHelper.new
You can try by using CSS selector. I've saved your HTML as a file called, test.html
require 'Nokogiri'
#doc = Nokogiri::HTML(open('test.html'))
puts #result = #doc.css('p').text.gsub(/\n/,'')
it returns
Click this link to enter a new Password. This link will expire within 24 hours, so don't delay.
There's a good post about Parsing HTML with Nokogiri
You were close. Here's how you find the text's containing element:
doc.search('*[text()*="Click Here"]')
This gives you the <a> tag. Is this what you want? If you actually want the parent element of the <a>, which is the containing <div>, you can modify it like so:
doc.search('//*[text()="Click Here"]/..').text
This selects the containing <div>, the text of which is:
Welcome to
OddZ
Click Here
to Enter a New Password
Click this link to enter a new Password. This link will expire within 24 hours, so don't delay.

get form values other than by name in codeigniter

hi i am using codeigniter . i have a form , there i add hidden fields dynamically . so every hidden field is <input type='hidden' name='hidden' value="+$(this).attr('title')+"> so the name is equal .
the problem is when i submit the form and try to get my hiden field values i can only get one hidden field value , because the names are same
i print my form values
print_r($this->input->post());
i have 2 hidden fields but i get only one
Array
(
[hidden] => march
[textbox] => march
[mysubmit] => Submit
)
i can change the name dynamically of hidden field when creating , but then i don't know exactly the name of my hidden field ,
how can i get hidden field values with same name ?? is there any way to get form values other than by name ?? i tried and can not find an answer , please help .............
You'll need to use brackets in your name attributes:
<input type='hidden' name='hidden[]'>
<!-- ^^^^ -->
This will allow PHP to accept multiple inputs with the same name as an array of values, so in this case, $_POST['hidden'] will return an array of strings.
By default they are indexed starting at 0, so $_POST['hidden'][0] will get you the first one, $_POST['hidden'][1] will get you the second, etc., however - you can explicitly index them if it's easier for you, either with numbers or strings.
<input type='hidden' name='hidden[first]'>
<input type='hidden' name='hidden[second]'>
Or:
<input type='hidden' name='hidden[0]'>
<input type='hidden' name='hidden[1]'>
You can nest these as deep as you want like hidden[first][1][], and they will be treated similarly to a PHP array when you get the $_POST values, but you need the brackets in the HTML.
Without brackets, only the last field's value will be available in the $_POST array. This is a PHP feature, Codeigniter can't do anything about it.

Resources