JQuery with forms help needed |
[eluser]gigas10[/eluser]
Hi there, Let me start off saying I'm using MSSQL and IIS 6.0... Not that I had a choice. Anyhow, I have a problem with JQuery and a form, I'm using jquery so people can add another element to the field, from 0-N. It works dandy and what not, however, when you submit the form, if errors are found and it posts you back to the form with the errors. It fills in the fields, but the fields the user added are gone. I don't know how to keep the stuff the submitted through those fields they dynamically added via jquery. I tried foreaching with the set_value('field_name[]'), however it tells me invalid argument supplied for foreach. The information inside the append works fine, when you click add it shows a select box with the appropriate data supplied. Here is my code. Code: [removed]
[eluser]slowgary[/eluser]
I found your description and code difficult to read. Have you copied and pasted your code from word? It has the non-standard double quote characters. You're a microsoft junkie, I can tell. ![]() Anyways... it seems like what you're saying is that your form allows a user to "add a field", but when the user submits erroneous values, the form is repopulated minus the added fields. How can you retain the added fields? I don't have the exact answer to your problem but you could probably iterate through all post data and add those fields to your form validation and view. You really should consider cleaning up your JavaScript for the next guy in line (or yourself, 6 months from now). Also, you're using the highlightFade plugin which is obselete (as claimed by it's creator) due to the addition of custom animations to jQuery. See if you can do something like this in your controller: Code: foreach($_POST as $key => $value) Then in your view you'd just iterate through the custom_fields and echo out form elements. I haven't tested this but you should be able to get something like it to work. I'm not sure how you'd manage setting real labels for each field in the form validation, or how you'd present each form field in the view, but I guess depending on your app maybe something more generic would work. That's for you to figure out. Good luck.
[eluser]gigas10[/eluser]
Hmmm, i tried to wrap everything in the code tags, sorry about that. I realize it's hard to understand what I'm saying, but I don't exactly know how to word however I you did understand what I was saying. The javascript code is crap, because believe it or not, I could not find any examples of how to add/remove fields. I searched for a couple of hours, until trying this which works... kinda. I guess I just don't understand, if I name the form element as an array, it should post an array of data. So when it comes back, shouldn't it still be an array? PS - Of course I'm a microsoft junkie, I have to use IIS and MSSQL at work. At home, right now, I'm running Gentoo as my OS of choice :-D. As for the non-standard double quotes... idk I use netbeans with php for my IDE ![]()
[eluser]slowgary[/eluser]
My brain is on fire. I need sleep and your JavaScript is causing John Resig to turn over in his grave, and he's still alive! ;-P It's really just the inconsistent whitespace that makes it so hard to read. You really should try to stick with uniform, tabbed indents and try to write your code like you're drawing a picture - make things lined up real nice. It helps when trying to understand the code. What is this supposed to do? Code: id = (id - 1) + 2; //adds 1? id++ would be easier Also, it looks like you're using the hidden "id" input to hold a value. I used to do that back in the day before I realized that's what variables were for. Make it a global variable if you need to access it across functions. You said you're allowing the user to add a form field, but the field is a select element, is that really necessary? I think you could make things easier on yourself if you went back to the drawing board on your UI. Since each select element is going to offer the same options, why not have just one select element and a button that adds the selection to a list? Make sense? It would look like this: Code: ,-------------------------,---, ,--------, I would do it with something like this: Code: < script type='text/javascript' >
[eluser]gigas10[/eluser]
fyi i didn't write any of that javascript, google gave that to me, i was also wondering why the hell the author of it would do id=id-2+1... that makes no sense. I was just too lazy to change it.
[eluser]slowgary[/eluser]
At least be honest. The function names in your JS match the context of your PHP. Have you read my post? Did you take my revised UI into consideration? If not, copy and paste it into a page and see it in action. I think you'll find it better than creating a new select element for each field. Let me know what you think.
[eluser]gigas10[/eluser]
This is me being honest, http://mohdshaiful.wordpress.com/2007/05...ng-jquery/ All I did was change the name of the function and div's.... because I have more then 1 type of field that are going to be like this. I changed the function names and stuff, but I never touched the id increment, I don't exactly know what half these $ and (.' are for or do, so I figured the author must have had good reason to do that id increment like that. Yes, I did try your code, but its not what I need because it grabs the value, which for me is an ID, which does nothing for the user, if theres a way to display the name, I could also use a hidden field for the ID. If you know a way to display the name, im guessing its something to do with $('.cause_select').val()
[eluser]slowgary[/eluser]
Ouch, what a nasty blog post. There's some real bad advice to be gotten out here on the interweb. Hopefully the advice I've given is much better, but everyone makes mistakes. As a quick jQuery primer, the framework encourages that you write your javascript in a "select something, do something" sort of way. the dollar sign is a function that's used for (among other things) the selecting part, and the method that follows usually takes care of the doing. As an example: Code: // v- select something When I wrote the revised UI, I just did a quick and dirty, assuming that the option value and caption could be the same. If you need them to be unique, try changing that long append line to this: Code: $('#causes').append("<div class='cause'><input type='hidden' name='cause_" + num_causes + "' value='" + $('#cause_select').val() + "'/><span>" + $('#cause_select:selected').text() + "</span><span class='remove'>[remove]</span></div>");
[eluser]gigas10[/eluser]
I also don't understand why you are doing name='cause_"+num_causes ... why not just make the name an array, name='root_causes[]' and on the recieveing page, foreach through it. Also #cause_select ![]()
[eluser]slowgary[/eluser]
Sure, you could do the array thing to. Then you'd just get rid of the iterator altogether. The reason the " ![]() Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
Welcome Guest, Not a member yet? Register Sign In |