Jump to content

ajax formdata not submitting button data


rizwansyed

Recommended Posts

Hi Guys,

I am using ajax form data to send a data to server.

In that i have 1 textbox and 2 buttons  which uses event.preventdefault();

On my server , only text box value is received , button values are not received.

Instead of 

<input name="subject" type="submit" id="pingfromws1" value="Start" onclick="pingg3(event)">
<input name="subject" type="submit" id="pingfromws2" value="Stop" onclick="pingg3(event)">

i gave button also..still the value of pressed button is not submitted to server.

<form action="/pingfromws.txt" method="post" id="pingfromws" >
<label><strong>Ping:</label></strong><input name="ip" id="ip"  type="text"
min="0" max="255">

<input name="subject" type="submit" id="pingfromws1" value="Start" onclick="pingg3(event)">
<input name="subject" type="submit" id="pingfromws2" value="Stop" onclick="pingg3(event)">  
<p id="pin1" style="display:inline;font-size:17px;"></p>


</form>
<script>
 function pingg3(event) 
 {
var myForm  = document.getElementById('pingfromws');
   event.preventDefault();
formData = new FormData(myForm); 
     var xhttp = new XMLHttpRequest();
     xhttp.onreadystatechange = function() {
       if (this.readyState == 4 && this.status == 200) {
         document.getElementById("pingfromws").innerHTML =
this.responseText;
       }
     };
     xhttp.open("POST", "pingfromws.txt",true);
     xhttp.send(formData);
     return true;
   }
</script>

 

am using a formdata, so only form data is getting submitted and not button data.

If so, Then how can i send a button data along with above code....i need text value and button value to be submitted at once without submit redirection to other page.

How can i do that

Please provide the suggestions

 

Thanks and Regards

Rizwan Syed

Link to comment
Share on other sites

FormData() does not store submit button details because through FormData() its not registered as a submitter of a form like it would be normally. You would have to create a variable to store the reference of element that triggered onclick event,

 onclick="whichbutton = this"

this variable is sent as a second argument OF onsubmit attribute attached to form element

onsubmit="pingg3(event, whichbutton)"

which is refereed in function parameters as

function pingg3(e, clickedsubmit)

You then append the submit data to the current formdata

formData.append(clickedsubmit.name, clickedsubmit.value)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...