How to retain dropdown selected value after postback javascript? - javascript

I have a form in my site. Fiddle is here
I removed tag because it gives error without action.
Here my issue is when I have full form with action, if I click the form its post back the page. Meanwhile the drop down menu show select an option :
before the form is submitted
when I select
After form is submitted
But I want the form should remain the values which is selected before the form is submitted. How can I do that?

You could either have the form content dynamically generated on the server-side, and when you submit, you generate the new form with the data already in it.
Or you could resort to ajax instead and don't do form submits.

When you submit your form, it will reload the page. And when the page is reloaded, the select option is by default "select an option".
You have to store the value before submitting, and when the page is reloaded, check if there is something stored. You can do that with the localStorage for exemple.
Or the second solution is to load the content in ajax, with a webmethod.

Related

Disable Javascript after page is loaded should be detected on action page

I think i have searched already about tags and yes i am using them already.
But here my question or Idea is:
I have a form including input to search/insert data into database (mysql) (index.php)
form action is being performed on search.php
search box searches input data in the selected tables, if it is found, then it gives the records in result.php, otherwise it inserts the data in table(sql) and then shows it in result.php
I am validating the form/input using javascript on the same page even using tags
Now after loading the index.php page, if someone disable javascript, it shows an alert to reload the page. if someone reload the page, searchbox will hide.
but the problem is, if after loading the page, if someone disable javascript and does not reload the page, and search some restricted text using javascript regex, that text goes into mysql database and it is saved. search.php perform its action and redirect it to result.php
I want some trick in search.php that checks some condition for javascript enabled/disabled, then perform the remaining actions.
IS THERE ANY WAY, IF ACTION SHOULD BE ANOTHER PAGE, on that page is javascript is disabled then it shows nothing, and if it is enabled then redirects to search.php
Form validation on JS level is unimportant, what you need to think about is your php validation. Please read top answers of: form validation with javascript vs php
and this:
How can I prevent SQL injection in PHP?
If you need to check if JS is enabled you can simply make button for your form (but not type="submit" but just a button element). Add event to that button:
onclick="document.getElementById('FormId').submit();"
and your form will be submitted only if JS is enabled as button will use JS for submission
Use a hidden input field and set its value using javascript on form submission. Since the value will be set only using javascript, if the submitted form has an empty value for this field, it can be concluded that javascript was disabled.
Using jQuery:
In the html:
<form id="myformid" action="myactionurl.php" method="POST">
<!-- form fields here -->
<input type="hidden" id="time" name="time" value="">
</form>
In js:
$('#myformid').submit(function(ev) {
$('#time').val($.now());
});
In php:
if(!empty($_POST['time'])) {
//code
} else {
echo "Javascript must be enabled for this site to work.";
}
You don't have to use time, it can be anything.

How to know the form is submitted on page load?

I have a form and I want to set focus on the status message when the form is submitted.
I have tried the following code.
$('form').on('submit', function(){
$('.messages--status').focus();
});
But the problem is that when I submit the form the page reloads and therefore my code is not working.
Is there any way to find on page reload that the form is submitted?
So that can I set the focus on the status message based on that condition?
Append a query parameter your url, and show the message based on that.
Eg. suppose your form action url is www.foo.com/form/submit then make it, www.foo.com/form/submit?submitted,
so when the page loads you can check whether submitted is there in the URL or not.

Disabling all elements of a form in HTML except Submit button?

Is there any way to disable the elements of a form onload, except the submit button.
It means that form in JSP page will be loaded with the existing data from backend but user should not be able to edit data. User can only use the submit the button.
I can do it manually by disabling each and every field manually but I am wondering if there is some way to disable all fields at a time, since there are many fields in the form in JSP page.
You can use :not() selector
$('form :not([type=submit])').prop('disabled',true);

How to get Form data to Landing Page which is redirected from the form in eloqua?

We have a form where in processing steps we have redirected the form to landing page.
I want to access the data submitted in form on the Landing page. How can i achieve this?
The Landing page with form completely acts as back end form which is retrieving data from previous form and submit.
How can i auto submit the form on Landing page with the retrieved data?
You have to forward them to their own purl, which would be lp.com/personspurl that would be passed via the form as form data, initially using a field merge on the load to pre-populate that value as a hidden field, ideally; assuming that you already have the contact in the database. If not, then you would have to 'update contacts with form data,' then attempt to re-post the data to a secondary form which pulls in the purl value via a field merge and then redirects the user to his/her respective purl via the 'redirect to web page' -> 'send to external' & subsequent 'use value of form field' options.
Hope that makes sense.
Best,
MLH

How do I fill and submit a form remotely?

How can I fill out and submit this form remotely: http://mta-nyc.custhelp.com/cgi-bin/mta_nyc.cfg/php/enduser/ask.php
The form asks for email and then it has a pull down menu and fields for Subject and Question.
Clicking the Next button takes you to a new page and in that page you click submit.
Can I fill this form and submit with one click from my site?
I will have a form in my site where the user will fill out his email and I will have pre-populated fields for Subject and Question and when the user will click the submit button the remote form will be filled and submitted.
How can I do this (with python and javascript)?
If you are submitting the page to your python backend, check out Mechanize.
Try twill - http://twill.idyll.org/python-api.html. I't some wrapper of Mechanize.
Issue a post to ~/api/feedback with the same headers and form data that their form produces?
Open up Chrome dev tools or whatever, go to the network tab, fill out the form manually once and copy the last HTTP POST?
Depending on the type of feedback on that form, the data will change. The data I posted looks like:
feedback: {"name":{"first":"Test","last":"Test"},"email":"test#test.co","feedbackType":{"service":{"sid1":10000,"sid2":14000},"categories":{"cid1":14308}},"attributes":[{"id":"branch_line_route","value":"F"},{"id":"traincarnumber","value":"1"},{"id":"date_of_event","value":"2020-09-10T13:35:47.318Z"},{"id":"time_of_occurence","value":"15:37:37"}],"comment":"Nice train"}

Categories

Resources