Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manipulate a form / inputs to be ignored when a form is submitted

I'm using ExpressionEngine and SafeCracker along with Ajax (plugin: jquery.form.js - http://jquery.malsup.com/form/).

Best I can tell, SafeCracker will only allow for updating a single entry at a time. However, the UI / UX necessitates that a list be displayed. I've proof of concept'ed an entry by entry on-demand form. That is, click a particular edit link next to each entry and a snippet of jquery creates a form along with displaying a submit button. Click submit and that single entry updates. The inputs don't exist until the Update link is clicked

What I would prefer to do, if possible, is to create the non-form and form versions of each entry as the page is renbered and use some sort of toggle to display one or the other. Again, doable. Then, when I click the Edit link I'd add the necessary attributes to the input so that entry's form elements will be read but the other (display: none) elements for the other entries will be ignored. I'm thinking (out loud) that if I add the attr("name", some-value) that would work. That is, an input with no name will be ignored.

Yes, I can test this and I will. However, even if it works I'm not sure if it's a best practice and/or there's a more ideal way of accomplishing my ends. I'm here looking for validation and/or additional expertise and input.

Thanks in advance.

like image 746
Chief Alchemist Avatar asked May 27 '12 15:05

Chief Alchemist


People also ask

Which attribute prevents the data entered into a field from being submitted with the form?

The disabled Attribute The value of a disabled input field will not be sent when submitting the form!

How do I not submit an input?

To stop an input field in a form from being submitted with HTML, we add an input without the name attribute. <input type="text" id="in-between" />; to add a text input without the name attribute. The field's value won't be submitted without the attribute.

How do you control what is sent to the server when submitting a form?

The names and values of the non-file form controls are sent to the server as name=value pairs joined with ampersands. The action value should be a file on the server that can handle the incoming data, including ensuring server-side validation.

How do you prevent a form from clearing fields on submit in Javascript?

You can use preventDefault method of the event object.


2 Answers

Just set disabled property to inputs and they will excluded from Form submission, whatever input fields are hidden or visible. Different jQuery methods, like submit() and serialize() follow specification of HTML 4 and exclude all disabled controls of a forms. So one way is to set

$('your_input').prop('disabled', true);

or ,

$('your_input').attr('disabled', 'disabled');

Check following link:

http://www.w3.org/TR/html401/interact/forms.html#successful-controls

Also, you may use a general button instead of a submit, as result you can handle click event on it and within that event you can make exclusion, validation, manipulation on values and what ever you like.

like image 165
thecodeparadox Avatar answered Nov 15 '22 07:11

thecodeparadox


You can put a disabled attribute on them server side or set the property via jQuery:

$(".hidden input").prop("disabled", true);
like image 40
Ricardo Souza Avatar answered Nov 15 '22 06:11

Ricardo Souza