How do I copy the values of one form element to another? - jquery-ui

This is a follow up to this questions:
How can I manipulate a form / inputs to be ignored when a form is submitted
I have the info displayed, the form, the show()s and hide()s, etc. Most everything seems to be progressing fairly well. Where I could use some input - no pun ontended - is on how to take the new values in the form and copy those to the display only div as part of the ajaxSuccess.
I can copy type=text and textarea but what about checkboxes, multi-selects and radios.
In short, once the new values in the form are submitted, I need to update the page (not as a form) with those new values. Perhaps I need to write some for each form element type? Even so, I'm not quite sure where to start.
Pardon me if my working isn't clear. If that's the case just ask what you'd like me to clarify.

The way that will make most sense is to have the script that is processing your form return the form contents in the ajaxSuccess data. Here's an example:
The javascript:
$.ajax({
url: 'ajax/process.php',
success: function(data) {
// Load returned data into page
$('#result').html(data);
}
});
The php:
var $input1 = $_POST['input1'];
var $input1 = $_POST['input1'];
// DO SOMETHING WITH VARS AND IF SUCCESSFUL RETURN
if($success){
echo "Thanks! Here is your info: ".$input1." and ".$input2;
}
Once the data has been returned you can manipulate it anyway you like. For example if you needed to separate out the different values from the form you could post a comma separated string back from the PHP and use the javascript .split() method to create an array from that string.
Hope that's helpful :)

I'd say that your best bet is to take a different approach altogether, and reload your content from the database after it's been updated - in this case via an AJAX call. The reason being that your posted content from the form may not match what ends up being saved to the database, as some of your fieldtypes may process and alter the data they're passed (for example, stripping tags, formatting text, etc). This way the refreshed content exactly matches what is displayed when the same content comes from the Channel Entries call.

Related

When to perform additional processing on the form in Rails?

I'm writing a form where some additional processing is required before sending the values. The image below depicts the simplified example.
where test_date and remind_date are saved on the server as a string of format YYYY/MM/DD.
However, the text field shown above allows users to enter an integer. The value from this field needs to be processed (basically, test_date - form_value).
I'm currently doing this conversion before sending the value to the server, within a saveForm() function that is triggered when the Submit button is pressed.
I feel like this might not be the ideal way, and wanted to get your opinions. Would it be better to handle this within the controller, by allowing a new param?
For a simple field like this you can just create another instance variable in the controller.
For more complex calculations or additional fields you could use a form object, eg: using the https://github.com/andypike/rectify gem or Reform.

Update param and call action in controller on click?

I have been banging my head over this for the past 3 days (No kidding!!!)....It seems like a very simple thing but I am just unable to do it. So I'm putting the question out here, and am open to any method which would work.
BACKGROUND : An advanced search form on submission used to generate an URL too large for the server. So the form had to be submitted via POST. So far, so good. Form submitted successfully, and the result was displayed. Only remaining problem was pagination.
As it's a POST call, it ruled out will pagination. While will-pagination merges param page to the existing params, but the problem is that it shows on url which results in the same error.
QUESTION: So is there any way, if the user clicks a link NEXT, I can
a) Update the page param
b) Keep the existing params
c) While not changing the URL
d) Transfer control back to the action in controller?
The only solution so far suggested was have a hidden form, repopulate it's value and submit again. The form's pretty complex, so if there is a cleaner way I'd like to know.
I see what you want from your comment. So editing my reply accordingly. You want the information as to which column is being selected in the sort to be available to the controller without having that information available in the url string, and you want to use GET for this not POST
The challenge you have is that you want to preserve state between requests and you need a mechanism for doing this. POST preserves that information by sending it in the body of the POST request. GET does this by allowing you to populate the query string. The other method for preserving state is to use a cookie.
You can manipulate the url with javascript on the page. There are tutorials online for that.
But if you just want a simple controller hack as you say in your comment then the simplest one I can think of is to allow the user to make the GET request to the url with the query params in it, then handle this request in two steps - step one saves the query information to the cookie, step two redirects them to the url with that query string data stripped, and you look on the cookie to see if it has those fields before building your data for that page. It's hacky but it works.
def sort
session[:sort] = params[:sort]
redirect_to url_without_the_query_string
end
There is also the new html 5 feature:
window.history.replaceState(“Replace”, “Page Title”, “/mypage”);
from this stackoverflow answer here: How to remove query string from url using javascript
However I'm not sure I'd recommend messing with the url after load like that (I don't know enough about that solution so I'd recommend you read that answer and see if that fits). So, if you MUST lose that data from the url string, because you need to somehow pass it between requests you either have to use POST, or use the session cookie/redirect method I describe above.
Does your html <form> have an action attribute? If not, add one that points to the page.
If you want to preserve the current GET params so that results from the POST can use , you will also need to modify the javascript triggered on the heading links so that as well as (or instead of) modifying the query string, they write the same data to hidden form fields (which of course then get posted in the body of the request). Your JS library may already include helpful functions for that.

Symfony will not populate a form with large string after bind()

I am having issues trying to override the value of a form field after submission. Currently the form includes a textarea and file upload input, but only one is used. If the user uploads a file it is parsed into text. I want to use the data that is parsed from the file as the value for the textare when the page reloads, rather than what was in the text box (empty). The content can not be determined until after the bind.
What I did was just bind the data again to manually set the value of the textarea after the document was parse. It works great if I hard code a value for the value, however when I use the full contents of the file, nothing is put in the textarea. There isn't some sort of length limit or something that could interfere with the population is there? I have tried short strings and they work fine, but these files are about 4k in length and won't populate in the text area.
Works
$this->form->bind(array('text'=>'1234'), $request->getFiles('profile_generate') );
Does not work, even though the value text is actually being set.
$this->form->bind(array('text'=>$largeString), $request->getFiles('profile_generate') );
FIX:
I suppose whatever function that Symfony uses to populate the fields from the bind() values has a problem with non-standard characters. Unfortunately it does not complain about it or let on to what the issue really is. After cleaning up the data, it worked fine.
$largeString = preg_replace('/[^(\x20-\x7F)\x0A]+/',' ', $largeString);
$this->form->bind(array('text'=>$largeString), $request->getFiles('profile_generate') );

how to get html tags of page without URL

I want to save the HTML tags of whole page that is going to (render) client side in the database table with a unique id so next time I can fetch that page directly from database with id and without any complicate logic execution. All this without any URL means I don't want to pass a URL to any method that will get the HTML tag. I want to get HTML tags before even rendering the page because I have some status system once status changed that URL will not work.
Sorry if my question is not clear, but I tried to mention all details. I simply want HTML tag of my view before sending it to client so that I can save those tags with a unique id in database table and next time I can just fetch those tags from the database table with that unique id. My need is that the view contains many complex queries that I don't want to run each time.
It's a kind of certificate in that once it is printed, it cannot be changed, it can only be viewed by that unique ID in a faster way.
What you want to do is called output caching, use that instead.
If you need to save the HTML output, you need to add an action filter and intercept the Result and save the text in database.

Fill a rails form with a hashmap

I have a difficult situation.
I let the the user create a form through a Rich Text Editor and then I save this.
So for example, I save this literally into my DB:
http://pastebin.com/DNdeetJp (how can you post HTML here? It gets interpreted, so now I use pastebin...)
On another page I wrap this in a form_tag and it gets presented as it should be.
What I want to do is save this as a template and save the answers as a hashmap to my DB.
This works well, but the problem is I want to recreate what checkbox/radiobutton/... is selected when the user goes back to the page. So I want to fill the form with the answers from the hashmap.
Is there a way to use a 'dummy' model or something else to accomplish this?
Thanks!
Since you're pasting in raw HTML which is not properly configured as a template, it is more difficult to enable the proper options based on whatever might be stored in your DB.
The reliable approach to making this work is to use Hpricot or Nokogiri to manipulate the bit of HTML you have and substitute values accordingly. This isn't too hard so long as you can define the elements in that form using a proper selector. For example, create a div with a unique id and operate on all input elements within it, comparing the name attribute with your properties. There may even be a library for this somewhere.
The second approach is to use JavaScript to enable the options in much the same fashion. This seems like a bit of a hack since the form itself will not have a proper default state.

Resources