0

I have some old code with document.write in it.

<script type="text/javascript" language="JavaScript1.2">
document.write('<form name="InvGenPayDonation" action="'+PostURL+'" onsubmit="return validateForm();" method="POST">');
</script>

How can I replace action with a javascript variable that has been determined when the page was loaded? Here is that code, which is not part of a function. (I've substituted names, so the substr indexes are not correct, but I'm sure you get the point.)

// Check where we came from so that we can go to the right spot when the
// form gets posted from outside of our HTML tree.
var PostURL = '/event.php';

if (window.location.href.substr(0, 21) == 'http://10.10.10.10:80')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 14) == 'http://webapps')
  PostURL = 'http://10.100.0.11:8580/event.php';
else if (window.location.href.substr(0, 7) == 'webapps')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 10) == 'http://www')
  PostURL = 'https://secure.french.toast.new_zeland.france/event.php';

I'd like PostURL to go into the form's action. If I can do that without document.write, that would be great. I just need some help. Thanks.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • 1
    Vote to close this question since this exact code (and this issue) was covered in this question: http://stackoverflow.com/questions/12396219/why-does-onsubmit-function-seem-not-to-execute/12396359#12396359 – Jeremy J Starcher Sep 12 '12 at 21:42
  • See http://stackoverflow.com/questions/5361751/how-to-use-javascript-change-the-form-action – user123444555621 Sep 12 '12 at 21:42

4 Answers4

1

Some other DOM0 code should do the trick:

// Your code from the question here
document.forms[0].action = PostURL;

Note that this assumes that your form is the first form on the page - if it is not, replace [0] with the zero-based index of the form.

However, you can (and probably should) use a more modern solution that is not dependent on the layout of the page. The simplest is document.getElementById - simply add an ID attribute to your form (make sure the ID's value is unique across the entire page) and then do the following:

document.getElementById("yourFormID").action = PostURL;
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
1

Well, you can create your form in html, give it and id and do it like this:

document.getElementById('myform').setAttribute('action', post_url);
elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

I would do it using jquery. You will also need to include the jquery file if you did not know already.

$(document).ready(function() {
var PostURL = '/event.php';

if (window.location.href.substr(0, 21) == 'http://10.10.10.10:80')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 14) == 'http://webapps')
  PostURL = 'http://10.100.0.11:8580/event.php';
else if (window.location.href.substr(0, 7) == 'webapps')
  PostURL = 'http://10.10.10.10:8580/event.php';
else if (window.location.href.substr(0, 10) == 'http://www')
  PostURL = 'https://secure.french.toast.new_zeland.france/event.php';


  $("#search-form").attr("action", PostURL);
});

<form action='' id="search-form">
</form>
James
  • 702
  • 2
  • 15
  • 39
  • 1
    You don't need jQuery for this. – gen_Eric Sep 12 '12 at 21:46
  • You don't have to but if he is trying to update old js it may be beneficial to upgrade to the jQuery system since many of new plugins make life much much easier. – James Sep 12 '12 at 21:55
0

You can use jQuery for your purpose. This code will set the form's action without document.write:

$("form[name=InvGenPayDonation]").attr("action", PostURL);