mend
mend

Reputation: 163

How to submit inline edit with ajax?

How can I submit this form with ajax.

Right now it creates a table from mysql query. When you click the Edit button behind a row, Javascript makes all the cells to input=text.

Now when you click the Submit button at the end of the line I would like it to submit edited data to mysql via ajax.

I don't understand where do I get the data that I need to POST with ajax.

<script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.9.custom.min.js"></script>
<script type="text/javascript" src="js/jquery.qtip-1.0.0.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
      $(".edit").click(function(){
          var tr = $(this).closest("tr");
          var submit = "<input type='submit' name='Submit' value='Submit' />";

          tr.find(".td").each(function(){
              var name = $(this).attr("title");
              var value = $(this).html();
              var input = "<input type='text' name='"+name+"' value='"+value+"' />";
              $(this).html(input);
          });

          tr.find(".button").html(submit);

      });
  });

// this is the id of the submit button
$(".button").click(function() {    

$.ajax({
       type: "POST",
       url: "index.php?page=update_mysql",
       data: $("#change").serialize(), // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

return false; // avoid to execute the actual submit of the form.
});
</script>

<form id="change" method="post" action="#">
    <table>

<?PHP
$sql="SELECT * FROM names";
$result = mysql_query($sql)or die(mysql_error());
  WHILE ($row = mysql_fetch_array($result, MYSQL_ASSOC))  {
        echo '<tr class="row">';
            echo '<td class="td" title="id">'.$row["id"].'</td>';
            echo '<td class="td" title="first_name">'.$row["first_name"].'</td>';
            echo '<td class="td" title="last_name">'.$row["last_name"].'</td>';
            echo '<td class="button" title="button"><button class="edit">Edit</button></td>';
        echo '</tr>';
  }
?>

    </table>
</form>

And update_mysql.php looks like this:

<?php
include 'firewall.php';
if ($_POST['Submit'] == "Submit") {
$id = $_POST['id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$sql_edit = "UPDATE names SET first_name = '$first_name', last_name = '$last_name' WHERE id = '$id'";
$result_edit = mysql_query($sql_edit) or die(mysql_error());

}

?>

Upvotes: 0

Views: 2568

Answers (2)

simplyharsh
simplyharsh

Reputation: 36393

Try this.

$(document).ready(function(){
  $(".edit").click(function(){
    var tr = $(this).closest("tr");

    tr.find(".td").each(function(){
      var name = $(this).attr("title");
      var value = $(this).html();
      var input = "<input type='text' name='"+name+"' value='"+value+"' />";
      $(this).html(input);
    });

    var submit = "<input type='button' name='Submit' value='Submit' />";
    tr.find(".button").html(submit);

  });
});

$(".button input[type=button]").live('click', function() {
  var data = $('form#change').serialize();
  // post data using ajax                                                                                                                                                                         
  $.ajax({
    type: "POST",
    url: "index.php?page=update_mysql",
    data: data,
    success: function(data) {
      alert(data); // show response from the php script.                                                                                                                                          
    }
  });
});

NOTE: I have changed the "type" attribute of submit button to "type='button'". That will not fire default submit of browser. The same button is bound to collect form data and submit using ajax.

Happy Coding.

EDIT:

It takes you to "index.php?page=my_page#" coz form is submitted whey you click the <input type="submit">. Haven't you changed it to <input type="button"> yet? You don't need a submit button when its not there to submit the form. There are other non-submit button to bind javascript handlers to.

Upvotes: 2

Tomer
Tomer

Reputation: 17940

You can use jquery.serialize():

var data = $('form').serialize();

Upvotes: -1

Related Questions