Friday, August 3, 2012

inline edit form with pagination in php ajax jquery

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
// JQUERY: Plugin "autoSumbit"
(function($) {
$.fn.autoSubmit = function(options) {
return $.each(this, function() {
// VARIABLES: Input-specific
var input = $(this);
var column = input.attr('name');

// VARIABLES: Form-specific
var form = input.parents('form');
var method = form.attr('method');
var action = form.attr('action');

// VARIABLES: Where to update in database
var where_val = form.find('#where').val();
var where_col = form.find('#where').attr('name');

// ONBLUR: Dynamic value send through Ajax
input.bind('blur', function(event) {
// Get latest value
var value = input.val();
// AJAX: Send values
$.ajax({
url: action,
type: method,
data: {
val: value,
col: column,
w_col: where_col,
w_val: where_val
},
cache: false,
timeout: 10000,
success: function(data) {
// Alert if update failed
if (data) {
alert(data);
}
// Load output into a P
else {
$('#notice').text('Updated');
$('#notice').fadeOut().fadeIn();
}
}
});
// Prevent normal submission of form
return false;
})
});
}
})(jQuery);
// JQUERY: Run .autoSubmit() on all INPUT fields within form
$(function(){
$('#ajax-form INPUT').autoSubmit();
});
</script>


<form id="ajax-form" method="POST" action="./ajax-update1.php">
 <table>
<tr><?php
/*
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
     
      
        // number of results to show per page
        $per_page = 5;
      
        // figure out the total pages in the database
        $result = mysql_query("SELECT * FROM wp_product");
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['no']) && is_numeric($_GET['no']))
        {
                $show_page = $_GET['no'];
              
                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page;
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page;
                }             
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page;
        }
      
        // display pagination
      
        echo "<p><b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='http://localhost/project/wp-admin/admin.php?page=add_product/add_product.php&no=$i'>$i</a> ";
        }
        echo "</p>";
          
        // display data in table
       ?>
     
       <?
        echo "<table  width='100%' border='1' cellspacing='0' cellpadding='2'>";
       echo "<tr>
   
       <th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Maker Name</th>
<th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Category</th>
<th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Status</th>
<th align='left' bgcolor='#EAEAEA' class='box-txt' style='background-color:#d7d7d7;'>Delte</th>
       </tr>";
        // loop through results of database query, displaying them in the table
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }
      
                // echo out the contents of each row into a table
                echo "<tr>";
              
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt"><input type="text" value="' . mysql_result($result, $i, 'proname') . '" name="proname"  ></td>';
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt"><input type="text" value="' . mysql_result($result, $i, 'catname') . '" name="catname"  ></td>';
               
                 echo '<td align="left" bgcolor="#FFFFFF" class="box-txt"><input type="text" value="' . mysql_result($result, $i, 'shop') . '" name="shop" size="4"></td>';
                

echo '<td align="left" bgcolor="#FFFFFF" class="box-txt"><a href="deleteproduct.php?id='. mysql_result($result, $i, 'pro_id') .'">delete</a></td>';
     echo '<input id="where" type="hidden" name="pro_id" value="'. mysql_result($result, $i, 'pro_id') .'" />';      
                echo "</tr>";
        }
        // close table>
        echo "</table>";
      
        // pagination
      
?>

</tr>

</table>  
</form>




<?php
// DATABASE: Connection variables
$db_host="localhost";
$db_name="computer";
$db_username="root";
$db_password="";
// DATABASE: Try to connect
if (!$db_connect = mysql_connect($db_host, $db_username, $db_password))die('Unable to connect to MySQL.');
if (!$db_select = mysql_select_db($db_name, $db_connect))die('Unable to select database');

// DATABASE: Clean data before use
function clean($value){return mysql_real_escape_string($value);}

// FORM: Variables were posted
if (count($_POST))
{
// Prepare form variables for database
foreach($_POST as $column => $value)
${$column} = clean($value);

// Perform MySQL UPDATE

$result = mysql_query("UPDATE wp_product SET ".$col."='".$val."'
WHERE pro_id='".$w_val."'")
or die ('Successful update row.');
}
?>

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts