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>
Related
I have an order form in a flask-based app. The form starts out with a single row for the user to fill out with the name of a product, etc.
Then there is an Add Item button which sends an htmx request. It can be used to add an indefinite number of additional rows.
Here is the code:
<table class="table" >
<thead class="thead-dark">
<tr>
<th>Product Name</th>
<th>Unit</th>
<th>Quantity</th>
</tr>
</thead>
<form autocomplete="off" action="/neworder" method="POST">
<tr>
<td><input type="text" name="product" ></td>
<td><input type="text" name="unit"></td>
<td><input type="text" name="number_of_units"></td>
</tr>
<tr hx-target="this">
<td><button type="button" hx-get="/adminorder" hx-swap="outerHTML" >Add Item</button></td>
</tr>
</tbody>
</table>
<p>Order Date: <input type="text" name="date"></p>
<input type="submit">
</form>
htmx returns this:
<tr>
<td><input type="text" name="product"></td>
<td><input type="text" name="unit"></td>
<td><input type="text" name="number_of_units" ></td>
</tr>
<tr hx-target="this">
<td><button type="button" hx-get="/adminorder" hx-swap="outerHTML" >Add Item</button></td>
</tr>
As you see, all the inputs in a column have the same name.
When the form is submitted, I want to use flask request to do this:
#app.route('/neworder', methods=["POST"])
def new_order():
...
order_date = request.form.get("date")
items = request.form.getlist("product")
return f'<html>{order_date} - {items}</html>'
However, the only thing that ends up in items is what was entered into the first row...
I tried starting out with more than one row, and then things worked as expected, so it must have something to do with the part of the code returned by htmx...
I don't think this is relevant (?) but the function used by hx-get looks simply like this:
#app.route('/adminorder')
def admin_order():
return render_template('adminorder.html')
Your HTML template is invalid, you should move the form opening tag outside of the table. I've also added a missing <tbody> tag.
<form autocomplete="off" action="/neworder" method="POST">
<table class="table" >
<thead class="thead-dark">
<tr>
<th>Product Name</th>
<th>Unit</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="product" ></td>
<td><input type="text" name="unit"></td>
<td><input type="text" name="number_of_units"></td>
</tr>
<tr hx-target="this">
<td><button type="button" hx-get="/adminorder" hx-swap="outerHTML" >Add Item</button></td>
</tr>
</tbody>
</table>
<p>Order Date: <input type="text" name="date"></p>
<input type="submit">
</form>
I have my edit page and i would like to submit info to db once fields have been edited, it seems like something simple yet i cannot find a good example that pertains to my situation, i really don't know how to go about resolving this, this is the last part of my project. I would appreciate any suggestions Please.
This is my edit page code:
<title>
</title>
</head>
<body ng-app="app" ng-controller="decontroller" class="container">
<div id="banner" style="text-align:center; margin-left:auto; margin- right:auto; display:block;">
</div>
<h2></h2>
<h3>Personal Information:</h3>
<div id="validation-errors">
</div>
<form action="" method="post" accept-charset="utf-8" ng-submit="addEdit()">
<table class="table table-bordered">
<tbody>
<tr>
<td>ParticipantID</td>
<td>{{edit.Stlc_id}}</td>
</tr>
<tr>
<td>First Name:<br>
</td>
<td>{{edit.First_Name}}</td>
</tr>
<tr>
<td>Last Name:<br>
</td>
<td>{{edit.Last_Name}}</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name ="Address" ng-model="edit.Address" ></td>
</tr>
<tr>
<td>Phone:</td>
<td><input size="20" name ="phone" ng-model="edit.Phone_Number" ></td>
</tr>
<tr>
<td>Assistive Devices:</td>
<td><input name ="AssistiveDevices" ng-model="edit.Assistive_Devices" ></td>
</tr>
<tr>
<td>Lanyard Code</td>
<td>
<input name ="Lanyard_Status" ng-model="edit.Lanyard_Status" /> </td>
</tr>
<tr>
<td>Comments</td>
<td>
<textarea cols="100" name="comments" ng-model="edit.Comments">.</textarea>
</td>
</tr>
<tr>
<td>Disenrolled</td>
<td><input name="disenrolled" ng-model="edit.Disenrolled" ></td>
</tr>
<tr>
<td>Deceased</td>
<td><input name="deceased" ng-model="edit.Deceased" ></td>
</tr>
</tbody>
</table>
</form>
<h3>Days in Center<br></h3>
<table class="table table-bordered">
<tbody>
<tr>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thursday</td>
<td>Friday</td>
</tr>
<tr>
<td><input name="Attendance_Monday" ng-model="edit.Attendance_Monday" ></td>
<td><input name="Attendance_Tuesday" ng-model="edit.Attendance_Tuesday" ></td>
<td><input name="Attendance_Wednesday" ng-model="edit.Attendance_Wednesday" ></td>
<td><input name="Attendance_Thursday" ng-model="edit.Attendance_Thursday" ></td>
<td><input name="Attendance_Friday" ng-model="edit.Attendance_Friday" > </td>
</tr>
</tbody>
</table>
<h3>Transportation Types</h3>
<table class="table table-bordered">
<tr>
<td>Type of Transportation</td>
<td>Approved For</td>
<td>Comments</td>
</tr>
<tr>
<td width="300px">Wheel Chair Van</td>
<td><input name="WheelChair_Van" ng-model="edit.WheelChair_Van"></td>
<td><textarea cols="100" name="WheelChair_Van comments" ng-model="edit.Comments" ></textarea></td>
</tr>
<tr>
<td width="300px">Transit Van 240</td>
<td><input name="TransitVan_240" ng-model="edit.TransitVan_240"></td>
<td><textarea cols="100" name="TransitVan_240 comments" ng-model="edit.Comments" ></textarea></td>
</tr>
<tr>
<td width="300px">Transit Van 360</td>
<td><input name="TransitVan_360" ng-model="edit.TransitVan_360"></td>
<td><textarea cols="100" name="TransitVan_360 comments" ng- model="edit.Comments"></textarea></td>
</tr>
<tr>
<td width="300px">Subaru Impreza</td>
<td><input name="Subaru_Impreza" ng-model="edit.Subaru_Impreza"></td>
<td><textarea cols="100" name="Subaru_Impreza comments" ng- model="edit.Comments"></textarea></td>
</tr>
</table>
<h3>Pick up and Drop Off Times</h3>
<br>
<table class="table table-bordered">
<tr>
<td width="300px">Pick Up Time:</td><td><input type="text" name="Pick_Up_Time" value=""></td>
</tr>
<tr>
<td width="300px">Drop off Time</td><td><input type="text" name="Drop_Off_Time" value=""></td>
</tr>
</table>
<br>
<h3>Personal Care Hours Pick Up/Drop Off</h3>
<table class="table table-bordered">
<tbody>
<tr>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thursday</td>
<td>Friday</td>
<td>Saturday</td>
<td>Sunday</td>
</tr>
<tr>
<td><input type="text" name="Monday_Pick_Up" ng-model="edit.Monday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Tuesday_Pick_Up" ng-model="edit.Tuesday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Wednesday_Pick_Up" ng-model="edit.Wednesday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Thursday_Pick_Up" ng-model="edit.Thursday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Friday_Pick_Up" ng-model="edit.Friday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Saturday_Pick_Up" ng-model="edit.Saturday_Pick_Up" placeholder="Pick Up Time"></td>
<td><input type="text" name="Sunday_Pick_Up" ng-model="edit.Sunday_Pick_Up" placeholder="Pick Up Time"></td>
</tr>
<tr>
<td><input type="text" name="Monday_Drop_Off" ng-model="edit.Monday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Tuesday_Drop_Off" ng-model="edit.Tuesday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Wednesday_Drop_Off" ng-model="edit.Wednesday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Thursday_Drop_Off" ng-model="edit.Thursday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Friday_Drop_Off" ng-model="edit.Friday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Saturday_Drop_Off" ng-model="edit.Saturday_Drop_Off" placeholder="Drop Off Time"></td>
<td><input type="text" name="Sunday_Drop_Off" ng-model="edit.Sunday_Drop_Off" placeholder="Drop Off Time"></td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" ng-click="saveEdit()" />
</form>
<pre>{{edit | json}}</pre>
<a href="http://localhost:8080/stlc/index.php/transport/list_show_data/transport_vi ew">
<button type="button" class="btn btn-primary">Back</button>
</a>
<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"> </script>
<script type="text/javascript">
var app = angular.module('app',[]);
app.controller('decontroller', function($scope,$http){
$scope.edit=<?php echo json_encode($aggregate_data_view);?>;
$scope.saveEdit = function(){
console.log("hey i'm submitting!");
console.log($scope.edit);
$http.post('?php echo site_url("index.php/transport/saveData")?>', $scope.edit).
success(function(data){console.log(":)") }).
error(function(data){
console.log(":(")
});
};
});
</script>
</body>
</html>
Here is my Controller:
public function saveData()
{
}
it is empty now cause i really don't know what to do,nothing has worked.
In my page I have three button. First two create random AVL data on the page
the second one save that data to the db
HTML:
<button data-ng-click="createAvl()">Create Avl</button>
<button data-ng-click="create1000Avl()">Create 1000 Avl</button>
<button data-ng-click="saveAvl()">Save Avl</button>
ngController:
app.controller("myCtrl", function ($scope, $http) {
$scope.avl = []; --here is where i save the avl
$scope.avl.push({
'tracker_avl_id': getRandomArbitrary(0, 1000000, 1),
'plate_num': getRandomArbitrary(0, 10000, 1),
'x_lat': getRandomArbitrary(-69, -66, 0),
'y_long': getRandomArbitrary(8, 10, 0),
'azimuth': getRandomArbitrary(0, 359, 1),
'engine_status': engine,
'gps_fix': gps_fix,
'event_time': getRandomDate()
});
$scope.saveAvl = function () {
$scope.ResponseDetails = 'Working...'; -- some variables to show progress
$scope.startDate = getDate();
$scope.startTime = new Date().getTime();
$scope.timeEllapsed = null;
-- use $.param jQuery function to serialize data from JSON
var data = $.param({
avl_list: $scope.avl
});
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
-- send the data to my webservice
$http.post('/AvlApi/putTrackerAVL', data, config)
.success(function (data, status, headers, config) {
var end = new Date().getTime() - $scope.startTime;
$scope.timeEllapsed = 'milliseconds passed: ' + end;
$scope.ResponseDetails = data;
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<hr />status: " + status +
"<hr />headers: " + header +
"<hr />config: " + config;
});
};
My Webservice is in IIS using C#
[HttpPost]
public JsonResult putTrackerAVL(List<avl> avl_list)
{
try
{
avl_list.ForEach(car => db.avl.Add(car));
db.SaveChanges();
}
catch (Exception ex)
{
return Json(new { status = "Fail", message = ex.InnerException });
}
return Json(new { status = "Success" });
}
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';
}
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
})
});
});
This is part of my code where I send ID to url and I need to get that ID. But problem is that I get something like this
delete.x=5&delete.y=8&delete=Delete&ID=124&ID=125&ID=126&ID=127&ID=128&ID=129&ID=130&ID=131&ID=132&ID=133&ID=134&ID=135&ID=136
It sends all my IDs from table. But I need to just send one ID, depends which button do I click (delete or publish)
<tr>
<td>125</td>
<td>Blacksfeets Photos</td>
<td>di_do#yahoo.com</td>
<td>2929610140-09 M plava.pdf</td>
<td>24</td>
<td>570099</td>
<td>0</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<input name="delete" type="image" src="images/delete.png" value="Delete"><br>
<input name="publish" type="image" src="images/publish.gif" value="Publish">
<input type="hidden" value="125" name="ID">
</td>
</tr>
<tr>
<td>126</td>
<td>TimeSince</td>
<td>sanlic#windowslive.com</td>
<td>25189210140-09 M plava.pdf</td>
<td>24</td>
<td>614227</td>
<td>0</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<input name="delete" type="image" src="images/delete.png" value="Delete"><br>
<input name="publish" type="image" src="images/publish.gif" value="Publish">
<input type="hidden" value="126" name="ID">
</td>
</tr>
<tr>
<td>127</td>
<td>TimeSince</td>
<td>saic#windowslive.com</td>
<td>21813910140-09 M plava.pdf</td>
<td>24</td>
<td>966462</td>
<td>0</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<input name="delete" type="image" src="images/delete.png" value="Delete"><br>
<input name="publish" type="image" src="images/publish.gif" value="Publish">
<input type="hidden" value="127" name="ID">
</td>
</tr>
You seem to have one form for all your buttons. Use one form per ID instead, e.g.
<form method="get" action="whatever">
<input name="delete" type="image" src="images/delete.png" value="Delete"><br>
<input name="publish" type="image" src="images/publish.gif" value="Publish">
<input type="hidden" value="127" name="ID">
</form>