Help with relay_-script buliding

Winterbay

Active member
I'm trying to build a help script for WHAM that lest you define what skills and items the script shall not use in case you have specific demands.

Since I suck at html I am using Jason's wonderful htmlform.ash and I've managed to build the attached script. When run together with the also attached data-file the result is as shown in the top of the attached image. After clicking the button everything turns into what is shown in the bottom of the image.
None of the parameters in the script have changed in such a way that it would explain this, form what I can see with sporadically placed print-commands. Anyone with a better idea of how html and ash works together that may help me figure this out?

I would also like for the script to add a new empty line if the user fills in the present one and presses the button instead of having to rerun the script, I've not managed to figure out that either...

relayproblems.JPG
 

Paragon

Member
To add elements to your form requires modifying the dom. The easiest way to to do this is to use a lib like jquery, but there is a way to do it without a lib.
You can use
var v = document.createElement('<Elementtype>') to generate a new element node,
the use v.setAttribute('<element attribute name>','<element attribute value');

once you've done that you have a node ready to be inserted into the dom, so... find the spot in the dom you want to add the element

elem = document.getElementById('<ID of existing node>')

then you can add it as the last node in that element with elem.appendChild(v)

or if have a child element from within that node you can use
elem.insertBefore(<newnode>,<existingchildnode>)
-or-
elem.insertAfter(<newnode>,<existingchildnode>)

um... links:
http://jquery.com/
http://www.javascriptkit.com/javatutors/dom2.shtml
http://reference.sitepoint.com/javascript/Node
 

Paragon

Member
True, but they Write out a pages html, which can include javascript... and it sounded like winterbay wanted the web page to add a new set of inputs when the user used all the current inputs... which would be something that you would need to do via the browser... via javascript.
 

Catch-22

Active member
I interpret what you're after about adding extra fields in the same way that Paragon does. If you can fix the errors Jason pointed out, it will make things easier for us to help you out.

You'd need something similar to what Paragon has above, like:

PHP:
var newRow = document.createElement("tr");
with (newRow) {
  //Declare all the newRow properties.
}
document.getElementById("yourTableId").appendChild(newRow);

Edit: I suggest you first give your table a unique ID so that you can reference it in the DOM (<table id="myTableId">).
 
Last edited:

StDoodle

Minion
If you're doing a relay script, you really don't need to use javascript to do all of your conditional writing... unless I'm missing something huge, it sounds like people are over-complicating this.

Edit to add: so this post isn't completely unhelpful... just do what JH said, and make sure everything has unique names. But you can do that in ash just fine, no need to bust out js.
 

Catch-22

Active member
I think instead of Winterbay wanting the user to add to the list one by one, with the user submitting the page each time, he wants a dynamic form that adds elements to the form and submits only once. That is what the javascript is for, but yes it can be done without javascript if he doesn't want a dynamic HTML form.
 

Winterbay

Active member
You're giving all of your fields exactly the same name. They have to have unique names in order to tell which one is which.

As for:... pressing the button IS rerunning the script.

Well, looking at the output of form_fields() it appears that the names given to the fields are, in case of the new one, "new", "new_", "new__" and "new__" so they do appear to have unique names. I can get the data file to update just fine, it's just that the display goes weird after pressing the button.
Would it be better if I assigned those underscores in the script?

And I know that pressing the button is rerunning the script, what I mean was that if I fill in the last row and press the button the datafile is updated with a 4th entry but the page shows the same three entries plus the empty one (or as is the case above all filled out with "skill" or "use") and not a fifth empty one which I would expect.
 

Winterbay

Active member
Ok, so adding those underscores to the script myself does make it not put skill/use in all the fields. That's nice.
Now, why does it not become 5 fields (up one from 4) when I enter something in the last row and click the button? The data file updates just fine, and if I clicka second time I get my new row...
 

Catch-22

Active member
The data file updates just fine, and if I clicka second time I get my new row...

If I had to guess, I'd say the data file is being reloaded before KoLmafia has a chance to process the form. That's just a guess though.

Edit: I just looked at your code, and that's what is happening. Your file_to_map() occurs in the global space, which is always processed before the main(). You'll need to shuffle your code around so that if (test_button("Update")) { ... } occurs first.

I'd suggest you put your not_ok declaration inside build_table() and you shift your
PHP:
	int j;
	if (test_button("Update")) {
		...
	}
code block to above write_header().
 
Last edited:

Winterbay

Active member
That was indeed the problem, thanks everyone. Now I go and try to implement deleting of entries and then I think it's ready to be posted :)
 
Top