form action result to javascript

asking-fox
asking-fox used Ask the Experts™
on
Is there a easy way to get the form variables into javascript? Background: Instead of loading a new site ajax should be used.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
erikTsomikSystem Architect, CF programmer

Commented:
try this pattern
document.getElementById('idname').value

Author

Commented:
Yes, that's one solution. But I searched for an easier one where I only have to redirect the form data.
Software Engineer
Commented:
erik is correct (very brief, but correct).

You can have javascript look for form elements, and identify values to be saved.
The question is, to where do you save them?

One possibility is if you have some "hidden" fields (outside of your form), you could copy the valuable information there.

For example:

  If the "hidden" input field has a type of "hidden" then it won't display.
Then, when the submit button is pressed, a routine is called to copy whatever important information you need to save into as many hidden fields as are necessary to save your data...

How's this sound?
<html>
<body>
 
<form action=''>
  Valuable data:
  <input type='text' id='important'><br>
  <input type='submit' value='Submit' onclick='return validate()'>
</form>
Hidden: <input type='text' id='saved'>
 
<script type="text/javascript">
  function validate() {
    var data = document.getElementById( 'important' );
    var save = document.getElementById( 'saved' )
    save.value = data.value;
    return confirm( 'Are you sure?' );
  }
</script>
 
</body>
</html>

Open in new window

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

HonorGodSoftware Engineer

Commented:
> an easier one where I only have to redirect the form data

  Not without coding on your part.  You might be able to find some code on the web that will look for all of the input fields in a form and save all the data, perhaps into a cookie, I don't know.

  But, I am not familiar with any general code of this sort just sitting around.

Author

Commented:
So redirection is not possible, but there is no worry about.

Now a little question about changing the state of a checkbox. following code doesn't work:
html: <input type="checkbox"  id="checkbox[0]"/>
javascript: document.getElementById["checkbox[" + $a + "]"].checked = $state_checked; //$a = 0

@ HonorGod
Thank you for the detailed answers.
I normally save data into cookies or variables, using hidden fields is an interesting alternative.



Commented:
I guess it is because of the variable name you are using.
Try to declare the name of the checkbox in a variable first, eg.
var itemname = "checkbox" + $a;
document.getElementById[itemname].checked

Author

Commented:
there where 2 faults:

1. the name
2. "[]", instead: "()"
HonorGodSoftware Engineer

Commented:
Using brackets '[]' or parenthesis '()' in the name or id attribute is not a best practice.

Are you all set now?

Author

Commented:
I did mean the bracktes in the function getElementById.
Now it works.

Thank you.
HonorGodSoftware Engineer

Commented:
If you are all set, please select a solution, and close this question.

Thanks
HonorGodSoftware Engineer

Commented:
Thank you for the grade & points.

Good luck & have a great day.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial