Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to submit inline edit with ajax?
    primarykey
    data
    text
    <p>How can I submit this form with ajax.</p> <p>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.</p> <p>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.</p> <p>I don't understand where do I get the data that I need to POST with ajax.</p> <pre><code>&lt;script type="text/javascript" src="js/jquery-1.4.4.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="js/jquery-ui-1.8.9.custom.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="js/jquery.qtip-1.0.0.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $(".edit").click(function(){ var tr = $(this).closest("tr"); var submit = "&lt;input type='submit' name='Submit' value='Submit' /&gt;"; tr.find(".td").each(function(){ var name = $(this).attr("title"); var value = $(this).html(); var input = "&lt;input type='text' name='"+name+"' value='"+value+"' /&gt;"; $(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. }); &lt;/script&gt; &lt;form id="change" method="post" action="#"&gt; &lt;table&gt; &lt;?PHP $sql="SELECT * FROM names"; $result = mysql_query($sql)or die(mysql_error()); WHILE ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '&lt;tr class="row"&gt;'; echo '&lt;td class="td" title="id"&gt;'.$row["id"].'&lt;/td&gt;'; echo '&lt;td class="td" title="first_name"&gt;'.$row["first_name"].'&lt;/td&gt;'; echo '&lt;td class="td" title="last_name"&gt;'.$row["last_name"].'&lt;/td&gt;'; echo '&lt;td class="button" title="button"&gt;&lt;button class="edit"&gt;Edit&lt;/button&gt;&lt;/td&gt;'; echo '&lt;/tr&gt;'; } ?&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>And update_mysql.php looks like this:</p> <pre><code>&lt;?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()); } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload