run a function and print that's output after clicking on submit button

0

0

Hi
I'm new to js and i have to use something in my project.
I have a function that is it :

  function hash_f() {
      var hash = new XMLHttpRequest();
      hash.onload = function(){
        document.write(this.responseText); 
      }
      hash.open("GET", "http://example.com/new.php", true);
      hash.send();
  }

I want to know how can i document.write(this.responseText) after i clicked on a form submit button :

<input type="submit" id="n" />

can anyone help me ? Thanks

Kavian Rabbani

Posted 2015-02-08T16:35:09.100

Reputation: 11

Question was closed 2015-02-10T03:02:16.343

1

Issues specific to programming and software development are off topic, see What topics can I ask about here?. Try [SO] but please first read How do I ask a good question?.

– DavidPostill – 2015-02-08T18:00:50.870

Answers

0

If you send the form data with a submit button, the default behavior is for the browser to follow the link that's in the form's action attribute. To send the form via ajax instead (so you stay on the same page) you need to handle the submission of the form yourself. An easy way using jquery can be found: https://stackoverflow.com/questions/21662836/send-form-data-using-ajax

You can call a function when a button is clicked by binding an onclick function to it. This way, you bind a function to a button, when the button is clicked you submit the form via ajax. To bind a function to a button you do:

<input type="button" onclick="functionName();" />

user922467

Posted 2015-02-08T16:35:09.100

Reputation: 48