Jump to content
rizwansyed

ajax formdata not submitting button data

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

Share this post


Link to post
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)

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...