dios231 dios231 - 1 year ago 43
Ajax Question

How to select all input fields that sits in a table

I have a form that "sits" inside a table

<div class="row">
<div class="col-lg-12">
<div class="ibox float-e-margins">
<div class="ibox-content">
<form id="form">
<input type="submit" value="send" class="btn btn-w-m btn-primary" style="float: left;">Add transaction</input>
<table class="table table-striped table-bordered table-hover " id="editable" >
<th>Name<br>(Last name, Firstname,<br>
or Business name)
<th>Price with VAT</th>
<th>Transaction type</th>
<td><input type="date" name="date"/></td>
<td><input type="text" name="name"/></td>
<td><input type="text" name="address"/></td>
<td><input type="text" name="phone"/></td>
<td><input type="text" name="price_with_vat"/></td>
<td id="vat">25%</td>
<td class="exclude"><select>
<option value="value1">Value from database</option>
<option value="value2">Value from database</option>
<option value="value3">Value from database</option>
<td class="exclude"><select>
<option value="value11">Yes</option>
<option value="value23">No</option>
<td class="exclude"><select>
<option value="value21">Euro</option>
<option value="value22">USD</option>
<option value="value23">Pound</option>

What I want is to select all input values and send an Ajax request to a php end. The problem is that in my jquery function (code following) i cannot gather all the inputs. Also altough i have a
page still get refreshed.

var request;

if (request){

var form = $(this)

var $inputs = $form.find("input, select, button, textarea");
var serializedData = $form.serialize();
$inputs.prop("disabled", true);

Answer Source

Try this code

For a form element's value to be included in the serialized string, the element must have a name attribute.

$(document).ready(function() {
  //Form submit event
  $('#form').on('submit', function(e){

    // validation code here
    if(!valid) {

    //Serialized data 
    var datastring = $("#form").serialize();

    //Ajax request to send data to server
        type: "POST",
        url: "your url.php",
        data: datastring,
        success: function(data) {
            alert('Data send');