Can only delete first row with Ajax - php

I have a page that prints out rows from a mysql table. I'm trying to create an ajax form that allows users to delete rows but for some reason I can only seem to get it to delete the very top row that is printed out.
I've only included the script that might be needed here and left out the database query(which works fine).Firebug only shows my form being posted when I click the top row of results, any other rows it does nothing. Can anyone tell me what's wrong? Thanks
My_reviews.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
//Delete Review
$(document).ready(function(){
$("#deleteReview").click(function (e) {
e.preventDefault();
var username=$("#username").val();
var film_id=$("#film_id").val();
var id=$("#id").val();
$.post('ajax_deleteReview.php', {username: username, film_id: film_id, id: id},
function(data){
$("#message").html(data);
$("#message").hide();
$("#message").fadeIn(500);
$("#message").fadeOut(2500);
});
return false;
});
});
</script>
</head>
<div class="container">
<div id="message"></div>
<?php
$sql = "SELECT * FROM user_reviews WHERE username='$username' ORDER BY DATE desc";
$result = $db_conx->query($sql);
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$film_id = $row['film_id'];
$review = $row['review'];
$movie = $tmdb->getMovie ($film_id);
echo '
<div class="row">
<div class="col-md-1">
<img id="image1" src="'. $tmdb->getImageURL('w150') . $movie->getPoster() .'" width="80" />
<p>
</p>
</div>
<div class="col-md-4">
<h3>
' . $movie->getTitle() .'
</h3>';
echo'
<p>
'.$review. '
</p>
<form>
<input type="hidden" id="username" name="username" value="'. $username.'">
<input type="hidden" id="film_id" name="film_id" value="'.$film_id .'">
<input type="hidden" id="id" name="id" value="'.$id .'">
<button type="submit" id="deleteReview" class="btn btn-danger btn-xs pull-right">delete</button>
</form>
</div>
<div class="col-md-7">
</div>
</div>';
}
?>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js">
</script>
</div>
</body>
</html>
ajax_deleteReview.php
<?php
//include db configuration file
include_once("ajax_review/config.php");
//Configure and Connect to the Databse
$username=$_POST['username'];
$film_id=$_POST['film_id'];
$id=$_POST['id'];
//Delete Data from Database
$delete_row = $mysqli->query("DELETE * FROM `user_reviews` WHERE id='$id' AND username='$username' AND film_id='$film_id' LIMIT 1");
if($delete_row){
echo '<img src="images/tick_large.png"/>';
}
else{ echo "An error occurred!"; }
?>

You have duplicated IDs in inputs, so jQuery returns the 1st occurrence of input.
You can add film_id, id and film_name to the <a> link with a data attribute, then read with jquery.
JavaScript function for ajax request need to be assigned to the class:
$(".deleteReview").click(function (e) {...
and
<a class="deleteReview"....
so with this you eliminate duplicate IDs in HTML code.

Related

Form not submitting after bootstrap confirmation

Firstly I will apologise my knowledge of jquery/javascript is not very good so my mistake maybe very obvious to some but I am still learning so be gentle!
I have a form which allows the members to delete posts they have made from the database. The form works fine and using php deletes the data from the database as I want. I wanted to create a popup that gets the user to confirm thats what they would like to do. I don't want to use the bog standard browser specific onclick method I wanted something a bit more customisable. So I am trying to use bootbox, I currently have the confirmation box popping up onclick but when you select yes, nothing else happens. I suspect the result from the jquery is not firing the form but from what I have read my code for that part is good?
I have pasted below my php, jquery and html
PHP:
<?php
$deletepost = 'none';
$deleteposterror = 'none';
if(isset($_POST['delete'])) {
$del_topic_id = $_POST['topic_id_value'];
// sql to delete a record
$pass_fail = "DELETE FROM topics WHERE topic_id ='$del_topic_id';";
$pass_fail .= "DELETE FROM posts WHERE topic_id_post ='$del_topic_id'";
if (mysqli_multi_query($conn, $pass_fail)) {
$deletepost = 'show';
}else{
$deleteposterror = 'show';
echo "ERROR: Could not able to execute $pass_fail. " . mysqli_error($conn);
}
}
?>
HTML & PHP in while loop:
<?php
$sql1 = "SELECT * FROM topics WHERE topic_by = '".$id."' ORDER BY topic_date DESC ";
$result1 = mysqli_query($conn, $sql1);
while($row = $result1->fetch_assoc()) {
$topic_id = $row ['topic_id'];
$topic_subject = $row ['topic_subject'];
?>
<div class='chat_post_titles' >
<div class='row'>
<div class='col-md-7 chat_post_text_align_left'>
<?php echo $topic_subject?>.
</div>
<div class='col-md-2 chat_post_text_align_right'>
<?php
$my_date = $row['topic_date'];
$date = DATE("G:i:s d/m/Y",strtotime($my_date));
?>
<div class='hide_text'>Post Date: </div>
<?php echo $date?>
</div>
<div class='col-md-2 chat_post_text_align_centre'>
<div class='hide_text'>Delete Post: </div>
<form method="post" class="delete_post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="topic_id_value" id="topic_id_value" value="<?php echo $topic_id?>">
<button type="submit" name="delete" id="delete" class="btn btn-danger buttonElement">Delete</button>
</form>
</div>
</div>
</div>
<?php
}
?>
JQUERY at bottom of page:
</body>
<?php include("../PHP/js.php"); ?>
<script type="text/javascript" src="../Members/updatedetails.js"></script>
<script type="text/javascript" src="../Members/bootbox.min.js"></script>
<script>
$('form').on('click','button.buttonElement',function (e) {
var form = $(this).closest('form'); // <-- add this
e.preventDefault();
bootbox.confirm({
message: "Are you sure you want to delete your post?",
title: "Delete Post",
buttons: {
cancel: {
label: "No",
className: "btn btn-default"
},
confirm: {
label: "Yes",
className: "btn btn-default"
}
},
callback: function(result) {
if(result == true) {
form.submit();
}
}
});
});
</script>
</html>
Finally worked out why my code wasn't working in my php I was using
if(isset($_POST['delete']))
but when using javascript submit() function this name attr is removed. So I used the name of the hidden input like so if(isset($_POST['topic_id_value'])) and it worked as normal.
Still thanks to the guys that gave me advice

Return record of each query as I click the button

I have 3 query records in a database table of the same user. When I do query returns and shows the most recent query of the database table.
I want to return the 3 queries, always show the most recent query, but have a button that when clicked close the query that is showing and open the previous query and so on until no further queries.
In this way I always have access to the information registered in each consultation about the user.
At this point I return the query as follows:
<a name="view2" id="<?php echo $row["Id"]; ?>" data-toggle="modal" href="#dataModal1" class="btn btn-primary view_data2" />EGA</a>
<div id="dataModal1" class="modal fade" style="width:1000px;">
<div class="modal-dialog" style="width:1000px;">
<div class="modal-content" style="width:1000px;">
<div class="modal-header" style="width:1000px;">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title"><strong>Estado Geral e Autonomia</strong></h4>
</div>
<div class="container"></div>
<div class="modal-body" id="employee_detail1">
</div>
<div class="modal-footer">
Sair
</div>
</div>
</div>
</div>
$(document).on('click', '.view_data2', function() {
var employee_id1 = $(this).attr("Id");
if (employee_id1 != '') {
$.ajax({
url: "./select2",
method: "POST",
data: {
employee_id1: employee_id1
},
success: function(data) {
console.log(data);
$('#employee_detail1').html(data);
$('#dataModal1').modal('show');
}
});
}
});
on the select2 page I have the following code:
if(isset($_POST["employee_id1"]))
{
$output = '';
$query = "SELECT * FROM centrodb.PsicUtentes WHERE centrodb.PsicUtentes.Id = '".$_POST["employee_id1"]."'";
$result = mysqli_query($conn, $query);
$output;
while($row = mysqli_fetch_array($result))
{
$output .= '
<h4 class="modal-title">Identificação do Utente</h4>
<form method="post" id="insert_form2">
<fieldset class="grupo">
<table class="campo" cellspacing="10">
<tr>
<td>
<label>Data</label>
<input type="text" id="Data1" name="Data" class="form-control" value="'.$row["Data"].'" style="width:150px;" />
</td>
<td>
<label>Código Utente</label>
<input type="number" id="CodigoUtente1" name="CodigoUtente" value="'.$row["CodigoUtente"].'" class="form-control" style="width:100px;"/>
</td>
<td>
<label>Nome Utente</label>
<input type="text" id="Nome1" name="Nome" value="'.$row["Nome"].'" class="form-control" class="form-control" style="width:400px;"/>
</td>
<td>
<label>Data Nascimento</label>
<input type="date" id="DataNasc1" name="DataNasc" value="'.$row["DataNasc"].'" class="form-control" style="width:150px;"/>
</td>
</tr>
</table>
</fieldset>
</form>
';
}
$output;
echo $output;
}
I will show the image with the page when I have three queries:
I intended to have a button like the one surrounded in red and whenever I clicked, I closed the information of the query that is showing and opened the previous query.
Solution I'm trying.
<script type="text/javascript">
$(document).ready(function(){
$('.conteudo').hide();
$('.exibir').each(function(i){
$(this).click(function(){
$('.conteudo').each(function(j){
if(i == j) $(this).show('slow');
});
});
});
$('.ocultar').each(function(i){
$(this).click(function(){
$('.conteudo').each(function(j){
if(i == j) $(this).hide('slow');
});
});
});
});
</script>
if(isset($_POST["employee_id1"]))
{
$output = '';
$query = "SELECT * FROM centrodb.PsicUtentes WHERE centrodb.PsicUtentes.Id = '".$_POST["employee_id1"]."'";
$result = mysqli_query($conn, $query);
$output;
while($row = mysqli_fetch_array($result))
{
$output .= '
<h4 class="modal-title">Identificação do Utente</h4>
<div>
<a class="exibir" href="#">Ver</a>--
Ocultar
</div>
<div class="conteudo">
<form method="post" id="insert_form2">
<fieldset class="grupo">
<table class="campo" cellspacing="10">
<tr>
<td>
<label>Data</label>
<input type="text" id="Data1" name="Data" class="form-control" value="'.$row["Data"].'" style="width:150px;" />
</td>
<td>
<label>Código Utente</label>
<input type="number" id="CodigoUtente1" name="CodigoUtente" value="'.$row["CodigoUtente"].'" class="form-control" style="width:100px;"/>
</td>
<td>
<label>Nome Utente</label>
<input type="text" id="Nome1" name="Nome" value="'.$row["Nome"].'" class="form-control" class="form-control" style="width:400px;"/>
</td>
<td>
<label>Data Nascimento</label>
<input type="date" id="DataNasc1" name="DataNasc" value="'.$row["DataNasc"].'" class="form-control" style="width:150px;"/>
</td>
</tr>
</table>
</fieldset>
</form>
<div>
';
}
$output;
echo $output;
}
That's how it works, but I still wanted to make one thing better. I see the first record, but when I see the second the first is always open and should hide when I open the second. The second record opens when I click on view and hides when I click hide.
Also I wanted the Edit and New button, always according to the div that I open and in my project if there are two registry open the two buttons at the beginning of the project and should open as I open the div, I show in the image:
When I open the first time the:
enter image description here
When I see the first record:
enter image description here
When I see the second record:
enter image description here
Hi what you can do is hide your modal in your html content, and fill it with your ajax then once you have the data you can display it ,you can do it this way:
your php script:
if (isset($_POST["action"])) {
$action = $_POST["action"];
switch ($action) {
case 'SLC':
if (isset($_POST["id"])) {
$id = $_POST["id"];
if (is_int($id)) {
$query = "select * from client where clientId = '$id' ";
$update = mysqli_query($mysqli, $query);
$response = array();
while($row = mysqli_fetch_array($update)){
.......
// you need to fill the response array here with the info you
//want to display and send it with json encode later
}
echo json_encode($response);
}
}
break;
}
}
Where action can be a command you want to do SLC, UPD, DEL etc and id is a parameter
then in your ajax:
function getInfo(id) {
return $.ajax({
type: "POST",
url: "getInfo.php",
data: { action: "SLC", id:id }
})
}
then call it like this:
getInfo(id).done(function(response){
var data=JSON.parse(response);
if (data != null) {
$('#form_id').trigger("reset");
//fill your modal form using your data and display the modal after reset the form
$("#modal").show();
}
})
Hope it helps

Ajax get dynamic id for the submit button of the form PHP MySQL

I have a form in PHP & MySQL and I want to submit it with AJAX.
I Have similar data so I have put these data in a foreach loop.
The problem is that when I try to submit the first row data it all goes well but it changes the data in all rows. and when I try to submit other data except the first row it doesn't submit at all.
I think the problem is because the submit button always gets the same id and therefore goes to the first row always.
I don't know how to get the value in Ajax.
Here goes the code:
//php form
<?php
$pid = $_GET['pid'];
include('config.php');
$timeline_query="SELECT * FROM timeline_table WHERE pid=$pid";
$result_timeline=mysqli_query($conn,$timeline_query);
if(!$result_timeline){
echo "Error: " . $conn->error;
}
$timelines=array();
while($row_timeline=mysqli_fetch_assoc($result_timeline)){
$timelines[]=$row_timeline;
}
?>
<?php
$x = 0;
foreach ($timelines as $timeline):?>
<div class="card">
<div class="card-header" id="heading<?php echo $x; ?>">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapse<?php echo $x; ?>" aria-expanded="true" aria-controls="collapse<?php echo $x; ?>">
<?php echo $timeline['timeline_title'];?>
</button>
</h5>
</div>
<div id="collapse<?php echo $x; ?>" class="collapse" aria-labelledby="heading<?php echo $x; ?>" data-parent="#accordion">
<div class="card-body">
<form id="register_form">
<input type="text" id="tid" name="tid" value="<?php echo $timeline['tid']; ?>"/>
<textarea class="form-control" rows="4" id="timeline_description" name="timeline_description"><?php echo $timeline['timeline_description'];?></textarea>
<input id="submit" type="submit" name="submit" value="Submit" />
</form>
</div>
</div>
</div>
<?php $x++; endforeach;
// ajax function
$(document).ready(function(){
$("#submit").click(function(){
var timeline_description = $("#timeline_description").val();
var tid = $("#tid").val();
alert(timeline_description);
alert(tid);
// Returns successful data submission message when the entered information is stored in database.
// var dataString = 'timeline_description='+timeline_description+'&pid='+ pid + '&tid='+ tid +;
var dataString = 'timeline_description='+timeline_description;
alert(dataString);
if(timeline_description=='')
{
alert("Please Fill All Fields");
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "process.php?tid"=+tid,
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
}
return false;
});
});
//process.php
<?php
include('config.php');
$tid=$_GET['tid'];
if( $_POST['timeline_description']==NULL ){ $timeline_description=''; }
else { $timeline_description=$_POST['timeline_description']; }
$query_timeline = "UPDATE timeline_table SET
timeline_description='$timeline_description' WHERE tid=$tid ";
$result=mysqli_query($conn,$query_timeline);
if(!$result){
echo "Error: " . $conn->error;
}
?>
Any idea?
Id of elements must be unique. The abnormal behaviour of your code is due to non-unique ids of your input elements.
In the case, If you cannot assign unique ids to elements you can achieve desired output by using this keyword and siblings() method in jquery.
Instead of
var timeline_description = $("#timeline_description").val();
var pid = $("#pid").val();
var tid = $("#tid").val();
try to get values in this way.
var timeline_description = $(this).siblings('#timeline_description').val();
var pid = $(this).siblings('#pid').val();
var tid = $(this).siblings('#tid').val();
I see there a problem with Yours inputs id - <input type="text" id="pid" - this is in foreach, so You have multiple elements with same id. So var pid = $("#pid").val(); will return You always a value of first found element. Try to use arrays:
<input type="text" id="pid[$pid]" name="pid[$pid]" value="<?php echo $pid ?>"/>
<input type="text" id="tid[$pid]" name="tid[$pid]" value="<?php echo $timeline['tid']; ?>"/>
Please remember one thing: id of any element in html PAGE (whole document) must be unique.

How can I cancel delete action with confirm prompt window?

After I click "Cancel" in the alert window, the delete is still happening!
How can I solve this?
I am using MySQL database.
Working test ToDo list: http://qrmobile.net/todo/index.php
Delete code below, then index page code...
NEW CODE SNIPPET BELOW: Is ajax giving me the problem?
// Delete entry
$('a.deleteEntryAnchor').click(function() {
var thisparam = $(this);
thisparam.parent().parent().find('p').text('Please hold your onions.... ☺');
$.ajax({
type: "GET",
url: thisparam.attr('href'),
success: function() {
thisparam.parent().parent().fadeOut('fast');
}
});
return false;
});
require 'db.php';
$db = new Db();
$response = $db->delete_by_id($_GET['id']);
header("Location: index.php");
<div id="container">
<header><img src="/todo/images/heading-trans.gif"></header>
<ul id="tabs">
<li id="todo_tab" class="selected">
To-Do
</li>
</ul>
<div id="main">
<div id="todo">
<?php
require 'db.php';
$db = new Db();
$query = "SELECT * FROM todo ORDER BY id asc";
$results = $db->mysql->query($query);
if($results->num_rows) {
while($row = $results->fetch_object()) {
$title = $row->title;
$description = $row->description;
$id = $row->id;
echo '<div class="item">';
$data = <<<EOD
<h4>$title</h4>
<p>$description</p>
<input type="hidden" name="id" id="id" value="$id" />
<div class="options">
<a class="deleteEntryAnchor" href="delete.php?id=$id">Delete</a>
<a class="editEntry" href="#">Edit</a>
<a class="save-button" href="index.php">Save</a>
</div>
EOD;
echo $data;
echo '</div>';
} // end while
}
else {
echo "<p>There are zero items. Add one now! </p>";
}
?>
</div><!--end todo-->
<div id="addNewEntry">
<h2>Add New Entry</h2>
<form action="addItem.php" method="post">
<p><label for="title"> Title</label>
<input type="text" name="title" id="title" class="input" required/></p>
<p><label for="description"> Description</label>
<textarea name="description" id="description" required></textarea></p>
<p><input type="submit" name="addEntry" id="addEntry" value="Add New Entry" /></p>
</form>
</div><!--end addNewEntry-->
</div><!--end main-->
</div><!--end container-->
<script>
//Do all this when the DOM is loaded
$(function() {
//get all delete links (Note the class I gave them in the HTML)
$("a.deleteEntryAnchor").click(function() {
//Basically, if confirm is true (OK button is pressed), then
//the click event is permitted to continue, and the link will
//be followed - however, if the cancel is pressed, the click event will be stopped here.
return confirm("Are you sure you want to delete this?");
});
});
</script>
Its basically your js is where the issue lies and needs a preventDefault, I assume, and its not really returning anything to any function that stops the form post.
$("a.deleteEntryAnchor").click(function(e){
if(!confirm("Are you sure you want to delete this?")){
e.preventDefault();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class='deleteEntryAnchor' href='www.stackoverflow.com'>delete</a>
$(function() {} only guards against the situation where your javascripts try to do things before the DOM has finished loading.
On big pages, it's entirely possible that your user clicks that link so fast that no javascripts have been loaded. So the browser is half way loading the page, the user issued a command to go to a different link, so it does the right thing by redirecting the user, instead of finish loading the current page.
You will want to have either have a return false on the onclick attribute of the anchor (safest) or e.preventDefault() in the jQuery click function.

Update div on AJAX submit jQuery is updating all divs

I'm trying to update a div with an ajax post. Problem is...it's updating every div.
Here's the json.php:
//json.php
$data['months'] = $db->escape_value($_POST['check']);
$data['id'] = $db->escape_value($_POST['hidden']);
$query = "UPDATE month SET months = '{$data['months']}' WHERE monthID = '{$data['id']}'";
$result = $db->query($query);
if($result) {
$data['success'] = true;
$data['message'] = "Update Successful!";
$data['text'] = $_POST['check'];
echo json_encode($data);
} else {
$data['message'] = "Update could not be completed.";
}
And the html:
<?php
$query = $db->query('SELECT * FROM month');
?>
<html>
<head>
<title>jQuery/Ajax - Update is updating all divs</title>
<link rel="stylesheet" type="text/css" href="test.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("input.check, button.save, input.cancel, div.message").hide();
$(".edit").click(function(){
$(this).parent().siblings("li.liTwo").children("input.delete").hide();
$(this).parent().siblings("li.liThree").children("button.save").show();
$(this).parent().siblings("li.liFour").children("input.cancel").show();
$(this).parents("ul").siblings("div.showText").hide();
$(this).parents("ul").siblings("input.check").show();
$(this).hide();
return false;
});
$(".cancel").click(function(){
$(this).parent().siblings("li.liTwo").children("input.delete").show();
$(this).parent().siblings("li.liThree").children("button.save").hide();
$(this).parent().siblings("li.liOne").children("input.edit").show();
$(this).parents("ul").siblings("div.showText").show();
$(this).parents("ul").siblings("input.check").hide();
$(this).hide();
return false;
});
$("form[name=form1]").submit(function(){
var params = $(this);
$.post("json.php", { hidden : $(this).find("[name=hidden]").val(), check : $(this).find("[name=check]").val() },
function (data){
if(data.success) {
$(".showText").html(data.text);
$(".message").html(data.message).slideDown("fast");
$(".check").hide();
$("button.save").hide();
$(".cancel").hide();
$(".edit").show();
$(".delete").show();
$(".showText").show();
return false;
}
}, "json");
return false;
});
});
</script>
</head>
<body>
<div class="message">message</div>
<?php while($row = $db->fetch_assoc($query)) { ?>
<form action="json.php" name="form1" method="post">
<div class="container">
<div class="showText"><?php echo $row['months']; ?></div>
<input name="check" type="text" class="check" value="<?php echo $row['months']; ?>" />
<input name="hidden" type="hidden" class="hidden" value="<?php echo $row['monthID']; ?>" />
<ul class="list">
<li class="liOne">
<input name="edit" type="button" class="edit" value="edit" />
</li>
<li class="liTwo">
<input name="delete" type="submit" class="delete" value="delete" />
</li>
<li class="liThree">
<button name="save" type="submit" class="save" value="<?php echo $row['monthID']; ?>">save</button>
</li>
<li class="liFour">
<input name="cancel" type="button" class="cancel" value="cancel" />
</li>
</ul>
</div>
</form>
<?php } ?>
<!--<a id="reset" href="test3.php">reset</a>-->
</body>
</html>
You need to specify a context (the form) for the elements you're changing:
$("form[name=form1]").submit(function(){
var form = this;
var params = $(this);
$.post(form.action, { hidden : $(this).find("[name=hidden]").val(), check : $(this).find("[name=check]").val() },
function (data){
if(data.success) {
$(".showText", form).html(data.text);
$(".message", form).html(data.message).slideDown("fast");
$(".check", form).hide();
$("button.save", form).hide();
$(".cancel", form).hide();
$(".edit", form).show();
$(".delete", form).show();
$(".showText", form).show();
return false;
}
}, "json");
return false;
});
Also, if you hide a parent element, the children are hidden, too, so you probably want to do that...
Every div has the same class: showText. They need unique IDs instead, like Div1, Div2. Then update them by their ID: $("#Div1")
Hint, instead of answer:
How many elements does $(".showText") return?
2nd Hint: It's more than one!
===
Edit for more clarity:
The first issue is that you're selecting by classes like .showText. But you're creating multiple forms, each of which has an element that matches .showText. You need some way to point at the right element in each form. One way to solve this is to add an ID on each FORM tag, so you can then select things like $('#form-number-$N .showtext) -- which selects any elements with class="showtext" inside the element with id "#form-number-$N"
You're looping over rows in your database and writing the forms. So you need some variable data to identify each individual form.
You've got a while loop that populates $row:
<?php while($row = $db->fetch_assoc($query)) { ?>
But currently, every form you create has a name attribute of "form1".
So what if, instead of:
<?php while($row = $db->fetch_assoc($query)) { ?>
<form action="json.php" name="form1" method="post">
You did something like:
<?php while($row = $db->fetch_assoc($query)) { ?>
<form action="json.php" name="form<?PHP echo $row['id']; ?>" id="<?PHP echo $row['id']; ?> class="myFormClass" method="post">
Then you could use a handler that looks something like:
$("form.myFormClass").submit(function(){
var params = $(this);
$.post("json.php", { hidden : $(this).find("[name=hidden]").val(), check : $(this).find("[name=check]").val() },
function (data){
if(data.success) {
$(this.id + " .showText").html(data.text);
...
return false;
}
}, "json");
return false;
});
Do you see what's happening there?

Categories