I have a scenario where I need to send form data via a GET (not a POST). I'm submitting the form to the same page as the form is on (form action="#).
The problem is that the controller for the site can't handle array data-- it looks for strings or json only. So when I have any array data from a row of text boxes that gets sent, I get a 500 error.
I am currently doing something like this:
$('#submit').on("click", function(e){
e.preventDefault();
$form.serialize();
$form.submit();
})
But this doesn't get the job done. The URL params look like this:
index.php?condition%5b%5d%3dtest
How can I access the actual form data and "rewrite" the data to be a valid json string for the text box values prior to sending the form?
I could POST the data but then I'd need a different route, and I could send the form via ajax but that would be overkill.
Thanks
NOTE: I know how to serialize the data already, as is explained in my code sample. The problem is I want to replace the URL params in the default form submission with the newly serialized / json-ified versions, and the suggested duplicate question does not address this.
Related
I have a html project with PHP and I want to be able to have a button that when I click on him, a POST request to be executed. The problem is that I want to be able to also send some parameters through the request but I don't want to have input fields.
I am afraid to say that it is impossible when you only use HTML each element of HTML have their usage and you can not send parameters like what you described the only thing you can send by a button is its name and value solutions to this situation is using hidden input type or sending these parameters by AJAX or socket as JSON for example. any sub-element of form tag in HTML will be received to the server as post global array and they generate by input fields inside the form so you can not have another input in the server if you don't create it in HTML unless you don't use HTML for requesting to the server I mean it's possible but not with HTML
Is there any way of sending post data via an anchor tag??
for GET data you just have to write it directly in the URL
but can we still do it for POST?
i need the link to be without any parameters and pass name=john via POST. can this be done?
Not with plain HTML.
You could use JavaScript to cancel the default behaviour of the link and submit a form (creating the form if needed).
jQuery('a').on('click', function (evt) {
jQuery('form').submit();
evt.preventDefault();
});
… but you shouldn't do that because links and submit buttons have different affordances and you'd be sending misleading messages to the user while adding an unnecessary dependancy on JavaScript.
If you want to make a POST request, then do it for the right reasons and do it with a form and a submit button.
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")
i'm doing a form submit. i've got these 2 files.
1) php html form
2) php file which receives post data from #1
the process will have to be like this:
1) user go to php html form, key in some data and click submit to post data.
2) the php file will receive the post data and process it.
3) after processing the data, i would need the php file to return the post data back to the php html form and populate the textbox.
how can i do this? i understand we can normally post to PHP_SELF and process it in the same php html form but i would like to use a separate php file to receive and process post data.
Since you're using a seperate PHP file to process the post, and using redirects to get back to the original page, you'll need some way to pass the data. You've got 2 main options:
Posting directly to the seperate processing page, and redirecting back to the form page.
Posting via AJAX and returning the textarea data as an AJAX response
Option 1 is the easiest and quite doable, but you'd need to store the data somewhere while redirecting. Storing it in the $_SESSION array is the best bet. You could stuff it into a cookie or pass it via query argument, but both are hideoulsy bad ideas (just mentioning them for completeness).
Option 2's more involved, and you need to build some infrastructure into your page to handle the AJAX responses, page updates, and how to handle error conditions and display appropriate message ("YOu didn't fill out this field", "that's not a valid credit card", etc....), but saves you having to redirect and store the data between the requests.
it would use javascript to change object values
for example:
input1.value = ``
I have used jQuery Form plugin to submit the whole form data to server while Ajax. When the server code (php) returns, the retrieved data is in JSON format. What is the best method to populate the form with jQuery.
Here is what I want to do:
The user enters the data on the form
The user submits the form data (I use ajax to submit)
The server code returns.
The user has the ability to retrieve the entered data.
So for the step 4, I want to know what the best method to use to populate a form?
Thank you
If you can ensure that the returned JSON object contains property names that match your form fields, it can be quite simple to populate the form.
Of course, you need to first evaluate the retrieved JSON, either using eval, or using a JSON library such as this for added security checks.
Then, you can just iterate over the object like this:
var obj = eval(json); /* or JSON.parse(json); */
for (var field in obj)
$('#' + field).val(obj[field]);
If your form contains non-textual fields (such as a select) you could add an additional check to set the selected index instead.