-3

The problem has been fixed by using serialize in ajax. This information is fixed.

I made a form to update exited data in the mysql table.

Without using ajax it's updating data successfully.

but with Ajax call i have a problem to pass name="id[]" and name="title[]".

These are input tags in the form

$sql = "SELECT * from table";
$results = mysqli_query($con, $sql);
<form name="form1" method="post" action="">
<?phpwhile($rows=mysqli_fetch_array($results)){ ?>

<input name="id[](at this point i am having problem)" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
<input name="title[] (at this point i am having problem)" type="text" id="title" value="<?php echo $rows['title']; ?>">
<?php } ?> //closing while
<input type="submit" name="submit" value="Submit" id="save">

This ajax I am trying

$(document).on('click','#save',function(e) {
    var data = $('form').serialize();
 $.ajax({
 data:data,
     type: "post",
     url: "add.php",
     success: function(data){
          alert("Data Save: " + data);
     }
});
 });

This is PHP (add.php)

$sql = "SELECT * from table";
$results = mysqli_query($con, $sql);
$count=mysqli_num_rows($results);
if(isset($_REQUEST)){    
$count=count($_POST["id"]);    
for($i=0;$i<$count;$i++){
$sql="UPDATE `title` SET `title` = '" . $_POST['title'][$i] . "' WHERE `id` ='" . $_POST['id'][$i] . "'";
$result1=mysqli_query($con, $sql);
}

echo $count;

//$sql = "UPDATE `title` SET `title`='$title' WHERE `title`.`id` =$id" ;

if(mysqli_query($con, $sql)){
    
    echo "Updated";
}
else{
    
    echo mysqli_error($con);
}
}
  • 2
    You are generating invalid html. All your inputs have the same id, so your JS will only fetch the first one. – El_Vanja Nov 29 '20 at 19:21
  • 4
    Also note that the way you're building your query is unsafe. You're open to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). You should use [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) or [PDO](https://www.php.net/manual/en/book.pdo) instead. – El_Vanja Nov 29 '20 at 19:24
  • @El_Vanja I tried with class, but this still not working. – Karan Gautam Nov 30 '20 at 07:28
  • Can you show how you changed your AJAX to reflect this? – El_Vanja Nov 30 '20 at 07:59
  • This would be much easier, if you just serialized your whole form, instead of trying to do it separately for the different types of field names. – CBroe Nov 30 '20 at 09:32
  • Add new information to the question by editing (the link is at the bottom of it), comments aren't suitable for lengthy pieces of code. – El_Vanja Nov 30 '20 at 12:59
  • Thanks, @CBroe. i used serialized and it's worked properly. thanks for help :) – Karan Gautam Nov 30 '20 at 13:16

1 Answers1

0

as @EL_Vanja suggested:

in your loop:

<?phpwhile($rows=mysqli_fetch_array($results)){ ?>

<input name="id[](at this point i am having problem)" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
<input name="title[] (at this point i am having problem)" type="text" id="title" value="<?php echo $rows['title']; ?>">
<?php } ?> //closing while

you are creating:

<input name="id[]" type="hidden" id="id" value="10">
<input name="title[]" type="text" id="title" value="foo">
<input name="id[]" type="hidden" id="id" value="25">
<input name="title[]" type="text" id="title" value="bar">
<input name="id[]" type="hidden" id="id" value="36">
<input name="title[]" type="text" id="title" value="fuzz">

and when Java script try to get element by id. it can get the first element

The correct way of generating this is to give them a class instead of an id:

<input name="id[](at this point i am having problem)" type="hidden" class="id" value="<?php echo $rows['id']; ?>">
<input name="title[] (at this point i am having problem)" type="text" class="title" value="<?php echo $rows['title']; ?>">
<?php } ?> //closing while
Abilogos
  • 4,777
  • 2
  • 19
  • 39