Using jQuery:remove() to delete/remove form elements off a page - php

I'm trying to remove a form element generated through Ajax calls, but when I submit the form and var_dump the POST request, I can still see the form field key=>value. I need to be able to completely remove the field through jQuery in order to validate correctly.

You can remove the element first before submitting it.
$(this.element).remove();

You can also not set the name attribute, and the field won't get posted

Related

Is there a way to choose either GET or POST method for every input tag under <form>?

Instead of defining METHOD attribute with either POST or GET in a FORM tag, is there any method to choose POST or GET method explicitly for each INPUT tag in a FORM?
As you can only set the form's method, there is no "obvious" way of doing so.
A solution would be to alter the form action with the input's values when pressing the submit button.
If you want to post the content to /submit.php with input1=first, input2=second and input3=get, you would update the action to /submit.php?input3=get before submitting the form.
You would have to define a new form tag around each element to do this. There is no way to set the method for an input element.
You can see available attributes for the input tag here:
http://www.w3schools.com/tags/tag_input.asp

Remove textbox name from URL if empty

I am using a PHP website.
I have 2 text box, and both values are going through GET to next page, so it look like
http://mywebsite.com/?word=&word2=hello
I want if word is empty, don't show in the URL
like
http://mywebsite.com/?word2=hello
Please help in this..
You will have to use some javascript prior to submitting the form to remove the form elements if they are deemed to be empty - then submitting the form.
You can write a function which is triggered by a faux-submit buttom. This validates the elements, makes changes as needed - then submits the form as the final action.
You can use the Javascript window.location function.
store the values of the textbox into variable

Store form data into hidden field using Jquery?

I have a form that has a popup page with another form. In that popup form I want to take all fields filled out and store it in a single field in the main parent form via a hidden field. So that when the parent form gets submitted I can get all the fields via the hidden fields via php. How could I do that with jquery? Can I take all the fields from the popup form and store it as a json string in the hidden field? Then in php be able to turn that jquery string into an object so I can get easy access to all the form values? If so then how would I take all the fields from the popup form and turn it into a json string? Or is there a better/easier way?
To capture the form into an input for posting:
You want to .serialize() the form.
$('input').val( $('form').serialize() );
Then, in PHP, you just do a parse_str() to split it back up into an array.
Also, keep in mind that there may be a better solution than passing field data around like this, but if you're hellbent on that implementation, this is probably the way to go.
To open a form as a modal, then collect the data in PHP:
// You can set this to not open by default and bind the opening to a button, or a link, etc...
$('form').dialog({
modal: true
});
In PHP, your form will be contained within $_POST as normal.
print_r( $_POST );
One idea would be to use jQuery to create fields like this:
$(".innerForm input").each(function() {
$(".parentForm").append("<input type='hidden' name='"+$(this).attr("name")+"' value='"+$(this).val()+"'");
});
If you add the values to your page this way then you would just be able to access them as though they were normal parts of the $_POST
$_POST("hidden_field_name")

How do I exclude certain form fields upon submission of the form without disabling the field

If I have a form with the following 3 fields:
First Name Last Name Date Of Birth
When the user fills in all 3 fields and submits you will get the URL
http: //fakeURL.php?firstName=Fred&lastName=Flintstone&DateOfBirth=2/5/1952
However, if the user only fills in First Name and Date Of Birth you will get
http: //fakeURL.php?firstName=Fred&lastName=&DateOfBirth=2/5/1952 (where lastName has no value)
How do I achieve
http: //fakeURL.php?firstName=Fred&DateOfBirth=2/5/1952 (where lastName and value are removed from the URL)
I don't want to disable the input field upon using onsubmit. Is there a better way than disabled the input field?
Please help...
Just remove the "name" attribute from the input element.
// using jQuery
$('#inputId').removeAttr('name');
// native Javascript
document.getElementById('inputId').removeAttribute('name');
You must either:
Remove or disable the field from the form before submitting it
Don't submit the form, instead redirect to a URL you construct from the form yourself
Make an AJAX request instead of leaving the page
Aside from those options, you can't submit this form via GET without all the inputs becoming part of the URL. That's part of the HTML and HTTP specifications.
if you are working on angular and using (ngModel), Remove the name attribute in the input field and add
[ngModelOptions]="{standalone: true}"
You should use forms with the GET method only when the new page is supposed to be bookmarked and passed around.
Since you are talking about you taking input from the user (and I presume you also store that input in a database or some similar permanent storage), you should be using POST instead.

filter search results with another form

so here's the problem
I have one form, it outputs search results after submit, with this URL
http://localhost/thi/search/results?keyword=barma&search=1&minprice=nomin&maxprice=nomax&minroom=nomin&maxroom=nomax&minyear=nomin&maxyear=2010
Now I have another form, which is supposed to filter the results from the previous form by getting the current url of the search results and appending to it the GET variables submitted by this form.
I tried filtering before, but that is with anchor links. How am I supposed to do it with forms? Anny approaches?
Just populate the form with the existing data. If it is something that can be changed, then use a text input, a select, or whatever and set the default value to whatever is in the URL. If it can't be changed, use a hidden input.

Categories