Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  57] [ 5]  / answers: 1 / hits: 17164  / 7 Years ago, thu, august 17, 2017, 12:00:00

I am trying to get values from form and append it to a form and then pass that value as parameter



Script



$(document).ready(function() {
$('#buttonClick').on('click', 'button',function firstCall(){

var form = new FormData();
form.append(name, test);
form.append(phone, 2245201905);
form.append(url, this_url);
var settings = {
async: true,
crossDomain: true,
url: url_path,
method: POST,
processData: false,
contentType: false,
mimeType: multipart/form-data,
data: form
}

$.ajax(settings).done(function (response) {
console.log(response);
});});
});


HTML



<form method=post id=formSample>
<div class=form-group>
<label for=name>Name</label>
<input type=text id=name placeholder=Fullname>
</div>
<div class=form-group>
<label for=phone>phone</label>
<input type=number name=phone placeholder=number>
</div>
<div id=buttonClick>
<button >Submit</button></div>


In my script I have hardcoded the value of name, phone and url and it is working by I am not able to append the value from form..
so I am looking for



form.append(name, (document.getElementById('name').value);


Also on button click I am not able to pass data as I have placed alert but I dont my code enter function



NOTE: I haven't worked on url part yet


More From » jquery

 Answers
42

To create a FormData with valued from the actual HTML you can pass that form to form data as parameter



<form id=myForm>
<input type=text id=name name=name placeholder=Fullname>
<input type=text id=phone name=phone placeholder=Phone>
</form>
<script>
var myform = document.getElementById('myForm');
var form = new FormData(myform);
// form will have name and phone
form.append(url, window.location.href);

$.ajax(settings).done(function (response) {
if (response.success) {
// Only do something if the response data has success key.
}
});});
</script>

[#56725] Tuesday, August 15, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
patienceannel questions
Fri, Mar 11, 22, 00:00, 2 Years ago
Tue, Oct 20, 20, 00:00, 4 Years ago
Wed, Jul 24, 19, 00:00, 5 Years ago
;