HTML table multiple forms Ajax submit - html

I have a table with multiple rows and multiple forms and I am trying to send each form to my PHP script.
When not using Ajax all the forms update correctly. However with Ajax I cannot get the PHP script to work. How can I get the Ajax to work?
On HTML part, I have multiple rows which belong to one form (In the example below all the rows belong to one form and I have multiple of these). I did read that a FORM tag cannot be under TR tag and even know when I look at the code it looks pretty ugly, but I am not sure how else I can build the table.
Ajax
$(document).ready(function(){
// click on button submit
$("#submit").on('click', function(){
// send ajax
$.ajax({
url: 'post.php', // url where to submit the request
type : "POST", // type of action POST || GET
dataType : 'json', // data type
data : $(this).serialize(), // post data || get data
})
});
});
HTML
<tbody>
<tr>
<form id="form1" method="POST" action=""></form>
<input type="hidden" name="ID" value="">
<td rowspan="6">bls</td>
<td rowspan="6">qasachin.dwxmp41#mailinator.com</td>
<td>Records</td>
<td>10</td>
<td>1</td>
<td><input type="text" size="1" value="" name="Records"></td>
<td>Contact</td>
<td></td>
<td><input type="text" size="1" value="" name="Contact"></td>
<td rowspan="6"><textarea id="usrform" name="comment">Tadas</textarea></td>
<td><button type="submit" id="submit" value="approve" class="btn btn-warning">Approve</button></td>
</tr>
<tr>
<td>Centers</td>
<td>15</td>
<td>1</td>
<td><input type="text" size="1" value="" name="Centers"></td>
<td>Company</td>
<td>Tadas</td>
<td><input type="text" size="1" value="" name="Company"></td>
</tr>
<tr>
<td>Duration</td>
<td>10</td>
<td>0</td>
<td><input type="text" size="1" value="" name="Duration"></td>
<td>Address</td>
<td></td>
<td><input type="text" size="1" value="" name="Address"></td>
<td><button type="submit" formaction="" class="btn btn-info btn-xs btn-block">Invoice</button></td>
</tr>
<tr>
<td>GCP</td>
<td>0</td>
<td></td>
<td><input type="text" size="1" value="" name="GCP"></td>
<td>Zip</td><td>10</td><td><input type="text" size="1" value="" name="Zip"></td>
<td><a target="_blank" href="mailto:qasachin.dwxmp41#mailinator.com" class="btn btn-success>Email</a></td>
</tr>
<tr>
<td>RAND</td>
<td>0</td>
<td>0</td>
<td><input type="text" size="1" value="" name="RAND"></td>
<td>City</td>
<td>Amsterdam</td>
<td><input type="text" size="1" value="" name="City"></td>
</tr>
<tr>
<td>Price</td>
<td>€39</td>
<td>€18</td>
<td><input type="text" size="1" value="" name="Price"></td>
<td>Departm</td>
<td>IT</td>
<td><input type="text" size="1" value="" name="Departm"></td>
</tr>

You have invalid HTML markup.
Only td and th elements are allowed to be child elements of tr.
And you are immediately closing your form tag. Which means you don't have any fields inside your form.
You should put your whole table inside your form.
<form id="form1" method="POST" action="">
<input type="hidden" name="ID" value="">
<table>
<tbody>
<tr>
<td rowspan="6">bls</td>
<td rowspan="6">qasachin.dwxmp41#mailinator.com</td>
<td>Records</td>
<td>10</td>
<td>1</td>
<td><input type="text" size="1" value="" name="Records"></td>
<td>Contact</td>
<td></td>
<td><input type="text" size="1" value="" name="Contact"></td>
<td rowspan="6"><textarea id="usrform" name="comment">Tadas</textarea></td>
<td><button type="submit" id="submit" value="approve" class="btn btn-warning">Approve</button></td>
</tr>
<tr>
<td>Centers</td>
<td>15</td>
<td>1</td>
<td><input type="text" size="1" value="" name="Centers"></td>
<td>Company</td>
<td>Tadas</td>
<td><input type="text" size="1" value="" name="Company"></td>
</tr>
<tr>
<td>Duration</td>
<td>10</td>
<td>0</td>
<td><input type="text" size="1" value="" name="Duration"></td>
<td>Address</td>
<td></td>
<td><input type="text" size="1" value="" name="Address"></td>
<td><button type="submit" formaction="" class="btn btn-info btn-xs btn-block">Invoice</button></td>
</tr>
<tr>
<td>GCP</td>
<td>0</td>
<td></td>
<td><input type="text" size="1" value="" name="GCP"></td>
<td>Zip</td><td>10</td><td><input type="text" size="1" value="" name="Zip"></td>
<td><a target="_blank" href="mailto:qasachin.dwxmp41#mailinator.com" class="btn btn-success>Email</a></td>
</tr>
<tr>
<td>RAND</td>
<td>0</td>
<td>0</td>
<td><input type="text" size="1" value="" name="RAND"></td>
<td>City</td>
<td>Amsterdam</td>
<td><input type="text" size="1" value="" name="City"></td>
</tr>
<tr>
<td>Price</td>
<td>€39</td>
<td>€18</td>
<td><input type="text" size="1" value="" name="Price"></td>
<td>Departm</td>
<td>IT</td>
<td><input type="text" size="1" value="" name="Departm"></td>
</tr>
</tbody>
</table>
</form>
You also need to handle the submit event on your form or your page will be reloaded.
You can do that by calling e.preventDefault() inside the handler.
$(function(){
// click on button submit
$("#form1").on("submit", function(e){
e.preventDefault(); // prevent page reload on submit
// send ajax
$.ajax({
url: 'post.php', // url where to submit the request
type : "POST", // type of action POST || GET
dataType : 'json', // data type
data : $(this).serialize(), // post data || get data
})
});
});

Related

One ajax for multiple form with same input fields

I have a table with large number of rows, something like this:
Actually in every row, in "Details" column, there is a form with two inputs, the first input (which is hidden) is corresponding Music ID and the second input is "Click To View Details" which submit the forms.
So, We have 1000 forms, with the same action but different hidden inputs.
I want to write ajax for these forms, It's obvious that I can't write 1000 ajax event for every form, so what can I do? Can I write ajax by class? not ID?
(As you know these 1000 forms should have different and unique IDs but they can have same class)
I tried to make unique form, and pass desired inputs for every row to that form and then write one ajax for that form ,However, It causes lot's of complexity and not a good option for me.
Here is an example of my form with 4 row:
https://jsfiddle.net/k930haxr/
<table>
<thead>
<th>Music ID</th>
<th>Music Name</th>
<th>Music Details</th>
</thead>
<tr>
<td>1</td>
<td>Song 1</td>
<td><form action="detail.php">
<input type="hidden" name="musicid" value="1">
<input type="submit" value="Music details">
</form></td>
</tr>
<tr>
<td>2</td>
<td>Song 2</td>
<td><form action="detail.php">
<input type="hidden" name="musicid" value="2">
<input type="submit" value="Music details">
</form></td>
</tr>
<tr>
<td>3</td>
<td>Song 3</td>
<td><form action="detail.php">
<input type="hidden" name="musicid" value="3">
<input type="submit" value="Music details">
</form></td>
</tr>
<tr>
<td>4</td>
<td>Song 4</td>
<td><form action="detail.php">
<input type="hidden" name="musicid" value="4">
<input type="submit" value="Music details">
</form></td>
</tr>
</table>
Ideas are pretty simple:
Add a click event to your forms like this:
$("form").submit(function (e) {
// Your code
});
Get forms' input values by using form.elements[0].value where form is const form = e.target;
Finally call your ajax from that event in your way.
Have a look at the snippet below:
$("form").submit(function (e) {
e.preventDefault();
const form = e.target;
const val1 = form.elements[0].value;
const val2 = form.elements[1].value;
// console.log(val1, val2);
const data = {
val1: val1,
val2: val2
};
console.log(data);
$.ajax({
url: '', // your url
method: 'POST',
data: { fields: data },
success: () => {
console.log("Success");
}
});
});
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<table>
<thead>
<th>Music ID</th>
<th>Music Name</th>
<th>Music Details</th>
</thead>
<tr>
<td>1</td>
<td>Song 1</td>
<td>
<form action="detail.php">
<input type="hidden" name="musicid" value="1">
<input type="submit" value="Music details">
</form>
</td>
</tr>
<tr>
<td>2</td>
<td>Song 2</td>
<td>
<form action="detail.php">
<input type="hidden" name="musicid" value="2">
<input type="submit" value="Music details">
</form>
</td>
</tr>
<tr>
<td>3</td>
<td>Song 3</td>
<td>
<form action="detail.php">
<input type="hidden" name="musicid" value="3">
<input type="submit" value="Music details">
</form>
</td>
</tr>
<tr>
<td>4</td>
<td>Song 4</td>
<td>
<form action="detail.php">
<input type="hidden" name="musicid" value="4">
<input type="submit" value="Music details">
</form>
</td>
</tr>
</table>

how can i get value of input using jquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have file html with some input fields. i want to get value of all input when button click with jquery ?
thank for your help
<html>
<body>
<table>
<tr>
<td>No </td>
<td>SKU </td>
<td>Price</td>
</tr>
<tr>
<td>1 </td>
<td><input type="text" name="product[0][sku]" class="form-control" placeholder=""> </td>
<td><input type="text" name="product[0][price]" class="form-control" placeholder=""></td>
</tr>
<tr>
<td>2</td>
<td><input type="text" name="product[1][sku]" class="form-control" placeholder=""> </td>
<td><input type="text" name="product[1][price]" class="form-control" placeholder=""></td>
</tr>
<tr>
<td>3 </td>
<td><input type="text" name="product[2][sku]" class="form-control" placeholder=""> </td>
<td><input type="text" name="product[2][price]" class="form-control" placeholder=""></td>
</tr>
</table>
<button name="btn" id="btn">Get Product </button>
</body
</html>
jQuery
$("#btn").on("click", function() {
const contents = $("#tb [name*=sku]").map(function() {
return ({
[$(this).val()]: $(this).closest("tr").find("[name*=price]").val()
})
}).get()
console.log(contents)
})
// OR
$("#btn").on("click", function() {
const contents = $("form").serializeArray();
console.log(contents)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<table>
<tbody id="tb">
<tr>
<td>No </td>
<td>SKU </td>
<td>Price</td>
</tr>
<tr>
<td>1 </td>
<td><input type="text" name="product[0][sku]" class="form-control" placeholder="" value="sku1"> </td>
<td><input type="text" name="product[0][price]" class="form-control" placeholder="" value="1.00"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text" name="product[1][sku]" class="form-control" placeholder="" value="sku2"> </td>
<td><input type="text" name="product[1][price]" class="form-control" placeholder="" value="2.00"></td>
</tr>
<tr>
<td>3 </td>
<td><input type="text" name="product[2][sku]" class="form-control" placeholder="" value="sku3"> </td>
<td><input type="text" name="product[2][price]" class="form-control" placeholder="" value="3.00"></td>
</tr>
</tbody>
</table>
</form>
<button name="btn" id="btn">Get Product </button>
vanilla JS
document.getElementById("btn").addEventListener("click", function() {
const contents = [...document.querySelectorAll("#tb [name*=sku]")].map(sku =>
({[sku.value] : sku.closest("tr").querySelector("[name*=price]").value})
)
console.log(contents)
})
<table>
<tbody id="tb">
<tr>
<td>No </td>
<td>SKU </td>
<td>Price</td>
</tr>
<tr>
<td>1 </td>
<td><input type="text" name="product[0][sku]" class="form-control" placeholder="" value="sku1"> </td>
<td><input type="text" name="product[0][price]" class="form-control" placeholder="" value="1.00"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text" name="product[1][sku]" class="form-control" placeholder="" value="sku2"> </td>
<td><input type="text" name="product[1][price]" class="form-control" placeholder="" value="2.00"></td>
</tr>
<tr>
<td>3 </td>
<td><input type="text" name="product[2][sku]" class="form-control" placeholder="" value="sku3"> </td>
<td><input type="text" name="product[2][price]" class="form-control" placeholder="" value="3.00"></td>
</tr>
</tbody>
</table>
<button name="btn" id="btn">Get Product </button>

ASP.NET MVC HTML How can I create a button that changes the form, not submitting it

I am trying to create buttons in a form, which doesn't submit the form. I have a button to submit the form, and that's the only one I want to submit. The buttons that I wish to create should have access to all the form, without needing to reload (and loose data) the form.
My view is:
<form method="post">
<table>
<tr>
<td><label>Nº Encomenda</label></td>
<td><input asp-for="encID" readonly="readonly" style="background-color:lightgray;" /></td>
</tr>
<tr>
<td><label>Farmácia</label></td>
<td><input asp-for="farmDetails.cliNome" readonly="readonly" /></td>
</tr>
<tr>
<td><label>Contactos</label></td>
<td><input asp-for="farmDetails.cliTelefone[0]" readonly="readonly" /><input asp-for="farmDetails.cliTelefone[1]" readonly="readonly" /></td>
</tr>
<tr>
<td><label>Rua</label></td>
<td><input asp-for="farmDetails.locRua" readonly="readonly" /></td>
</tr>
<tr>
<td><label>Localidade</label></td>
<td><input asp-for="farmDetails.locLocalidade" readonly="readonly" /></td>
</tr>
<tr>
<td><label>Código Postal</label></td>
<td><input asp-for="farmDetails.locCodPostal" readonly="readonly" /></td>
</tr>
<tr>
<td>#Html.LabelFor(m => m.aSelVM.SelectedArm)</td>
<td>#Html.DropDownListFor(m => m.aSelVM.SelectedArm, Model.aSelVM.ArmList, "Seleccionar...")</td>
#Html.ValidationMessageFor(m => m.aSelVM.SelectedArm)
</tr>
<tr>
<td>#Html.LabelFor(m => m.aSelVM.SelectedArmLoc)</td>
<td>#Html.DropDownListFor(m => m.aSelVM.SelectedArmLoc, Model.aSelVM.ArmLocList)</td>
</tr>
#await Html.PartialAsync("_ProdPartialView", Model.pVM)
</table>
<div>
<input type="submit" formaction="/Order/AddProdWnd" value="Adicionar" /><!--Adicionar</button>-->
<input type="button" asp-action="RemoveProd" asp-controller="Order" value="Eliminar" />
</div>
<div>
Origem da Venda
<select asp-for="origemVenda" asp-items="Html.GetEnumSelectList<originSellType>()">
<option selected="selected" value="">Seleccionar...</option>
</select>
</div>
<div>
Prioritário <input asp-for="isPrioritario" />
</div>
<div>
<p><label>Observações</label></p>
<textarea asp-for="observs"></textarea>
</div>
<input type="submit" value="Guardar" asp-action="SendOrdForm" asp-controller="Order" />
</form>
The buttons "Adicionar", "Eliminar" are the one's that I am referring to.
I am using a very complex data structure, I know...
Could you help me?
Thank you.

Except Line Break in Input Text Fieldset

i have the following html to Capture some Data.
<fieldset>
<legend>Rezept hinzufügen</legend>
<form action="php_act/create.php" method="post">
<table cellspacing="0" cellpadding="0">
<tr>
<th>Rezept Name</th>
<td><input type="text" name="name" placeholder="Rezept Name" /></td>
</tr>
<tr>
<th>Kurzbeschreibung</th>
<td><input type="text" name="kurztext" placeholder="Kurzbeschreibung" class="kurztext" /></td>
</tr>
<tr>
<th>Kategorie</th>
<td><input type="text" name="Kategorie" placeholder="Kategorien - mit , trennen" /></td>
</tr>
<tr>
<th>Anforderung</th>
<td><select name="Anforderung">
<option value="einfach">einfach</option>
<option value="mittel">mittel</option>
<option value="schwer">schwer</option>
</select></td>
</tr>
<tr>
<th>Zeit / Nährwerte</th>
<td><input type="text" name="zeit" placeholder="Zeit in minuten" /></td>
<td><input type="text" name="KCAL" placeholder="KCAL" size="6"/></td>
<td><input type="text" name="KH" placeholder="KH" size="6"/></td>
<td><input type="text" name="Eiweiss" placeholder="Eiweiss" size="6"/></td>
<td><input type="text" name="Fett" placeholder="Fett" size="6" /></td>
</tr>
<tr>
<th>Portionen</th>
<td><input type="text" name="Portionen" placeholder="Portionen" /></td>
</tr>
<tr>
<th>Zutaten</th>
<td><input type="text" name="zutaten" placeholder="Zutaten" /></td>
</tr>
<tr>
<th>Zubereitung</th>
<td><input type="text" name="zubereitung" placeholder="Zubereitung" /></td>
</tr>
<tr>
<th>FotoliaID</th>
<td><input type="text" name="FotoliaID" placeholder="FotoliaID" /></td>
</tr>
<tr>
<td><button type="submit">Speichern</button></td>
<td><button type="button">zurück</button></td>
</tr>
</table>
</form>
</fieldset>
There are some Input Type Text. i need to capture line breaks within the textfields. Problem is that "Enter" submits by default the. Is it possible to change that. Enter should add a line break in the textfield and not submit the form.
Can't you use textarea instead of textbox?
In a textarea you can insert enters.
https://www.w3schools.com/tags/tag_textarea.asp

Get value from html input field

How I can get the value of input with id="new_amount" and put it properly in Change amount url path. What I am trying right now is obviously not correct:
<td>
<input type="button"
onclick="location.href='${pageContext.request.contextPath}/update?id=${object.id}&?amount=${document.getElementById("new_amount").value}';"
value="Change Amount" />
</td>
Can someone please help me.
<table class=goods>
<tr>
<td>Name</td>
<td>Amount</td>
</tr>
<c:forEach var="object" items="${goods}">
<tr>
<td id="name"><c:out value="${object.name}"></c:out></td>
<td id="amount"><c:out value="${object.amount}"></c:out></td>
<td id="new_amount"><p>New amount:</p> <input type="number" id="new_amount" /></td>
<td><input type="button"
onclick="location.href='${pageContext.request.contextPath}/update?id=${object.id}&?amount=${document.getElementById("new_amount").value}';" value="Change Amount" /></td>
<td><input type="button" onclick="location.href='${pageContext.request.contextPath}/delete?id=${object.id }';" value="Delete"/></td>
</tr>
</c:forEach>
<tr>
<td><a href="${pageContext.request.contextPath }/addgoods">Add new
goods</a></td>
</tr>
I have done some static workaround (for looping) with three rows. It will be helpful to you.
Give id to table, Instead of appending the value directly I am calling function and use hidden value for your object_id. Also added class to the element to find that(ID is not good idea for repeated elements).
All you need to do is, change static values with your JSTL code.
<table id="goods">
<tr>
<th>Name</th>
<th>Amount</th>
<th>New Amount</th>
<th>Actions</th>
</tr>
<tr>
<td>test1</td>
<td>100</td>
<td><input type="number" name="new_amount" class="amount" /></td>
<td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
<input type="hidden" value="1" class="goods_id" />
</tr>
<tr>
<td>test2</td>
<td>200</td>
<td><input type="number" name="new_amount" class="amount" /></td>
<td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete"/></td>
<input type="hidden" value="2" class="goods_id" />
</tr>
<tr>
<td>test3</td>
<td>300</td>
<td><input type="number" name="new_amount" class="amount" /></td>
<td><input type="button" onclick="updateObject(this)" value="Edit" /> / <input type="button" onclick="deleteObject(this)" value="Delete" /></td>
<input type="hidden" value="3" class="goods_id" />
</tr>
</table>
Use these functions to get values of clicked row,
window.updateObject = function(object){
// Get nearest text box values based on class.
var object_id = $(object).closest('tr').find('.goods_id').val();
var amount = $(object).closest('tr').find('.amount').val();
alert("ID : "+object_id+" Amount :"+amount);
}
window.deleteObject = function(object){
var object_id = $(object).closest('tr').find('.goods_id').val();
alert("ID : "+object_id);
}
See the Demo. If you required I can explain more. Hope this helps.
Update :
In HTML,
Your actual requirement is looks like below, I have done this using JSTL
<table id="goods">
<tr>
<td>Name</td>
<td>Amount</td>
<td>New Amount</td>
<td>Actions</td>
</tr>
<c:forEach var="object" items="${goods}">
<tr>
<td><c:out value="${object.name}"></c:out></td>
<td><c:out value="${object.amount}"></c:out></td>
<td><p>New amount:</p> <input type="number" id="new_amount" class="amount" /></td>
<td><input type="button" onclick="updateObject(this)" value="Change Amount" /> /
<input type="button" onclick="deleteObject(this)" value="Delete"/></td>
<input type="hidden" value="${object.id}" class="goods_id" />
</tr>
</c:forEach>
<tr>
<td><a href="${pageContext.request.contextPath }/addgoods">Add new
goods</a></td>
</tr>
In Script,
window.updateObject = function(object){
// Get nearest text box values based on class.
var object_id = $(object).closest('tr').find('.goods_id').val();
var amount = $(object).closest('tr').find('.amount').val();
//alert("ID : "+object_id+" Amount :"+amount);
location.href='${pageContext.request.contextPath}/update?id=object_id&?amount=amount';
}
window.deleteObject = function(object){
var object_id = $(object).closest('tr').find('.goods_id').val();
//alert("ID : "+object_id);
location.href='${pageContext.request.contextPath}/delete?id=object_id';
}