Jump to content
lauralee

Show edits to products immediately on form

Recommended Posts

I am using a loop with a form to show products in a table that can be edited. When I click the "edit" button to post the update, I want the changes to show up immediately in the form. Right now, I have to refresh the page to see the changes. How can I get the changes to show immediately after clicking on the "edit" (submit) button? My form for editing product is shown below. echo '<a name="Building Envelope Products"></a>';$sql = "SELECT * FROM products WHERE category='Building Envelope Products' order by id"; $result = @mysql_query($sql); if (!result) { $error = 'Error fetching information';exit();} echo '<table class="building"><tr><caption>Edit Building Envelope Products</caption</tr><tr><th>ID</th><th>CATEGORY</th><th>FAMILY</th><th>FAMILY ID</th><th>SBFM</th><th>NAME</th><th>DESCRIPTION</th><th>COLOR</th><th>WT</th><th>PRICE</th><th>INACT</th><th>PayPal #</th><th>EDIT</th></tr>'; while ($row = mysql_fetch_array($result)) { echo '<form action=" " method="post">'; echo '<tr><td><input type="text" name="id" value = "' . $row['id'] . '" size="9" /></td>'; echo '<td><input type="text" name="category" value = "' . htmlspecialchars($row['category'], ENT_QUOTES, 'UTF-8') . '" size="15" /></td>'; echo '<td><input type="text" name="familyname" value = "' . htmlspecialchars($row['familyname'], ENT_QUOTES, 'UTF-8') . '" size="10" /></td>'; echo '<td><input type="text" name="family_id" value = "' . htmlspecialchars($row['family_id'], ENT_QUOTES, 'UTF-8') . '" size="10" /></td>'; echo '<td><input type="text" name="subfamily_identifier" value = "' . htmlspecialchars($row['subfamily_identifier'], ENT_QUOTES, 'UTF-8') . '" size="5" /></td>'; echo '<td><input type="text" name="name" value = "' . htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') . '" size="15" /></td>'; echo '<td><input type="text" name="description" value = "' . htmlspecialchars($row['description'], ENT_QUOTES, 'UTF-8') . '" size="45" /></td>'; echo '<td><input type="text" name="color" value = "' . htmlspecialchars($row['color'], ENT_QUOTES, 'UTF-8') . '" size="5" /></td>'; echo '<td><input type="text" name="weight" value = "' . htmlspecialchars($row['weight'], ENT_QUOTES, 'UTF-8') . '" size="3" /></td>'; echo '<td><input type="text" name="list_price" value = "' . htmlspecialchars($row['list_price'], ENT_QUOTES, 'UTF-8') . '" size="3" /></td>'; echo '<td><input type="text" name="inactive" value = "' . htmlspecialchars($row['inactive'], ENT_QUOTES, 'UTF-8') . '" size="1" /></td>'; echo '<td><input type="text" name="button" value = "' . htmlspecialchars($row['button'], ENT_QUOTES, 'UTF-8') . '" size="15" /></td>'; echo '<td><input type="submit" name="action" value="Edit" /></td>'; if (isset($_POST['id']))$id = $_POST['id']; $familyname = $_POST['familyname']; $subfamily_identifier = $_POST['subfamily_identifier']; $name = $_POST['name']; $description = $_POST['description']; $color = $_POST['color']; $weight = $_POST['weight']; $list_price = $_POST['list_price']; $inactive = $_POST['inactive']; $button = $_POST['button']; $description = mysql_real_escape_string($description); $familyname = mysql_real_escape_string($familyname); $subfamily_identifier = mysql_real_escape_string($subfamily_identifier); $name = mysql_real_escape_string($name); $color = mysql_real_escape_string($color); $weight = mysql_real_escape_string($weight); $list_price = mysql_real_escape_string($list_price); $inactive = mysql_real_escape_string($inactive); $button = mysql_real_escape_string($button); $sql = "UPDATE products SET id = '$id', familyname = '$familyname', subfamily_identifier='$subfamily_identifier', name = '$name', description = '$description', color = '$color', weight = '$weight', list_price = '$list_price', inactive = '$inactive', button = '$button' WHERE id ='$id'"; if (!@mysql_query($sql)) { echo '<p>Error updating product: ' . mysql_error() . '</p>'; exit(); } echo '</form>';}echo '</table>';

Share this post


Link to post
Share on other sites

you need to use ajax to submit the form dynamically. http://w3schools.com/ajax and you can use DOM to manipulate the node where it needs update. when user click on edit you will change the content of node using DOM and then send the ajax request to update. if request failed fallback to previous data in that node which is being updated or show some error.

Share this post


Link to post
Share on other sites
echo '<a name="Building Envelope Products"></a>';if (isset($_POST['id'])&&isset($_POST['update'])){if (mysql_query("UPDATE products Set  category='".$_POST['category']."',  familyname='".$_POST['familyname']."',  family_id='".$_POST['family_id']."',  subfamily_identifier='".$_POST['subfamily_identifier']."',  name='".$_POST['name']."',  description='".$_POST['description']."',  color='".$_POST['color']."',  weight='".$_POST['weight']."',  list_price='".$_POST['list_price']."',  inactive='".$_POST['inactive']."',  button='".$_POST['button']."'WHERE id='".$_POST['id']."'")){ echo ' Record Updated.';}else { echo 'Error While Update :'.mysql_error();}}if (isset($_POST['id'])&&isset($_POST['confirm_delete'])){if (mysql_query("DELETE FROM products WHERE id='".$_POST['id']."'")){ echo '<br/>1 Record Deleded '; } else {echo 'Error While Delete: '.mysql_error(); }}if (isset($_POST['id'])&&isset($_POST['edit'])){  echo '<H1>Update products</H1>';$result_products = mysql_query("SELECT * FROM products WHERE id='".$_POST['id']."'");while($row_products = mysql_fetch_array($result_products)){ ?><form id="update_products" name="update_products" method="POST" action="">id :  <input type="hidden" name="id" value="<?PHP echo $row_products['id'];?>"/><?PHP echo $row_products['id'];?><br/>category : <input type="text" name="category" value="<?PHP echo $row_products['category'];?>"/><br/>familyname : <input type="text" name="familyname" value="<?PHP echo $row_products['familyname'];?>"/><br/>family_id : <input type="text" name="family_id" value="<?PHP echo $row_products['family_id'];?>"/><br/>subfamily_identifier : <input type="text" name="subfamily_identifier" value="<?PHP echo $row_products['subfamily_identifier'];?>"/><br/>name : <input type="text" name="name" value="<?PHP echo $row_products['name'];?>"/><br/>description : <input type="text" name="description" value="<?PHP echo $row_products['description'];?>"/><br/>color : <input type="text" name="color" value="<?PHP echo $row_products['color'];?>"/><br/>weight : <input type="text" name="weight" value="<?PHP echo $row_products['weight'];?>"/><br/>list_price : <input type="text" name="list_price" value="<?PHP echo $row_products['list_price'];?>"/><br/>inactive : <input type="text" name="inactive" value="<?PHP echo $row_products['inactive'];?>"/><br/>button : <input type="text" name="button" value="<?PHP echo $row_products['button'];?>"/><br/><input type="reset" name="reset" value="Reset" /><input type="submit" name="update" value="OK" /><br/></form><br/><?PHP }}elseif (isset($_POST['id'])&&isset($_POST['delete'])){$result_products_to_del = mysql_query("SELECT * FROM products WHERE id='".$_POST['id']."'");while($row_products_to_del = mysql_fetch_array($result_products_to_del)){ ?>Do You Want To Delete<form id="delete_products" name="delete_products" method="POST" action="">id :  <input type="hidden" name="id" value="<?PHP echo $row_products_to_del['id'];?>"/><?PHP echo $row_products_to_del['id'];?><br/>category :<?PHP echo $row_products_to_del['category'];?><br/>familyname :<?PHP echo $row_products_to_del['familyname'];?><br/>family_id :<?PHP echo $row_products_to_del['family_id'];?><br/>subfamily_identifier :<?PHP echo $row_products_to_del['subfamily_identifier'];?><br/>name :<?PHP echo $row_products_to_del['name'];?><br/>description :<?PHP echo $row_products_to_del['description'];?><br/>color :<?PHP echo $row_products_to_del['color'];?><br/>weight :<?PHP echo $row_products_to_del['weight'];?><br/>list_price :<?PHP echo $row_products_to_del['list_price'];?><br/>inactive :<?PHP echo $row_products_to_del['inactive'];?><br/>button :<?PHP echo $row_products_to_del['button'];?><br/><input type="submit" name="confirm_delete" value="YES" /><input type="button" value="NO" onClick="document.location.href='?'"><br/></form><br/><?PHP }}else{//$sql = "SELECT * FROM products WHERE category='Building Envelope Products' order by id";$sql = "SELECT * FROM products order by id";$result = @mysql_query($sql);/*if (!isset(result)){$error = 'Error fetching information';exit();}*/echo '<table class="building"><tr><caption>Edit Building Envelope Products</caption</tr><tr><th>ID</th><th>CATEGORY</th><th>FAMILY</th><th>FAMILY ID</th><th>SBFM</th><th>NAME</th><th>DESCRIPTION</th><th>COLOR</th><th>WT</th><th>PRICE</th><th>INACT</th><th>PayPal #</th><th>EDIT</th><th>Delete</th></tr>';while ($row = mysql_fetch_array($result)){echo '<form action="" method="post">';echo '<tr><td><input type="text" name="id" value = "' . $row['id'] . '" size="9" /></td>';echo '<td><input type="text" name="category" value = "' . htmlspecialchars($row['category'], ENT_QUOTES, 'UTF-8') . '" size="15" /></td>';echo '<td><input type="text" name="familyname" value = "' . htmlspecialchars($row['familyname'], ENT_QUOTES, 'UTF-8') . '" size="10" /></td>';echo '<td><input type="text" name="family_id" value = "' . htmlspecialchars($row['family_id'], ENT_QUOTES, 'UTF-8') . '" size="10" /></td>';echo '<td><input type="text" name="subfamily_identifier" value = "' . htmlspecialchars($row['subfamily_identifier'], ENT_QUOTES, 'UTF-8') . '" size="5" /></td>';echo '<td><input type="text" name="name" value = "' . htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') . '" size="15" /></td>';echo '<td><input type="text" name="description" value = "' . htmlspecialchars($row['description'], ENT_QUOTES, 'UTF-8') . '" size="45" /></td>';echo '<td><input type="text" name="color" value = "' . htmlspecialchars($row['color'], ENT_QUOTES, 'UTF-8') . '" size="5" /></td>';echo '<td><input type="text" name="weight" value = "' . htmlspecialchars($row['weight'], ENT_QUOTES, 'UTF-8') . '" size="3" /></td>';echo '<td><input type="text" name="list_price" value = "' . htmlspecialchars($row['list_price'], ENT_QUOTES, 'UTF-8') . '" size="3" /></td>';echo '<td><input type="text" name="inactive" value = "' . htmlspecialchars($row['inactive'], ENT_QUOTES, 'UTF-8') . '" size="1" /></td>';echo '<td><input type="text" name="button" value = "' . htmlspecialchars($row['button'], ENT_QUOTES, 'UTF-8') . '" size="15" /></td>';echo '<td><input type="submit" name="edit" value="Edit" /></td>';echo '<td><input type="submit" name="delete" value="Delete" /></td>';echo '</form>';}echo '</table>';}

Share this post


Link to post
Share on other sites

Your original code displays the form first, then processes it. You should process it before you display it.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...