如何将编辑后的值传递给数据库

时间:2013-10-03 08:49:28

标签: javascript php jquery ajax

我正在使用http://www.iwebux.com/inline-edit-using-ajax-in-jquery/进行内联编辑的html表我尝试将编辑后的值传递给数据库,但是它没有通过,我想将id和编辑后的值传递给数据库,这是我的代码。请有人指导我,我是编程感谢的新手。

AJAX

$(document).ready(function () {



  $('td.edit').click(function () {


      $('.ajax').html($('.ajax input').val());
      $('.ajax').removeClass('ajax');

      $(this).addClass('ajax');
      $(this).html('<input id="editbox" size="' + $(this).text().length + '" type="text" value="' + $(this).text() + '">');

      $('#editbox').focus();
    }


  );

  $('td.edit').keydown(function (event) {


      arr = $(this).attr('class').split(" ");


      if (event.which == 13) {

        $.ajax({
          type: "POST",
          url: "config.php",
          data: "value=" + $('.ajax input').val() + "&rowid=" + arr[1] + "&region=" + arr[2],
          success: function (data) {
            $('.ajax').html($('.ajax input').val());
            $('.ajax').removeClass('ajax');
          }
        });
      }

    }




  );


  $('#editbox').live('blur', function () {

    $('.ajax').html($('.ajax input').val());
    $('.ajax').removeClass('ajax');
  });



});

HTML

<body>

<table cellpadding="15"> 


<tr class="heading" bgcolor="#ccc">
    <th>region</th>
    <th>country</th>
    <th style="width:285px;">networkname</th>
         <th>mcc</th>
         <th>mnc</th>
         <th>mnp</th>
</tr>

<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'fms';
$dbPassword = 'xxxxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");



$sql = mysql_query("SELECT * FROM supplierprice");


while($row=mysql_fetch_array($sql))
{
echo '<tr '.$row['id'].' >';
echo "<td class='edit' >" . $row['region'] . "</td>";
echo "<td class='edit' >" . $row['country'] . "</td>";
echo "<td  class='edit' >" . $row['networkname'] . "</td>";
echo "<td class='edit' >" . $row['mcc'] . "</td>";
echo "<td class='edit' >" . $row['mnc'] . "</td>";
echo "<td class='edit'>" . $row['mnp'] . "</td>";
echo "</tr>";
}

?>

</table>



</body>

的config.php

<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'fms';
$dbPassword = 'xxxxxxxx';
$dbDatabase = 'fms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

print $_POST['rowid'];
if($_POST['rowid'])
{
$id=mysql_escape_String($_POST['id']);
$region=mysql_escape_String($_POST['region']);
$country=mysql_escape_String($_POST['country']);
$networkname=mysql_escape_String($_POST['networkname']);
$mcc=mysql_escape_String($_POST['mcc']);
$mnc=mysql_escape_String($_POST['mnc']);
$mnp=mysql_escape_String($_POST['mnp']);
$sql = "update supplierprice set region='$region',country='$country',networkname='$networkname',mcc='$mcc',mnc='$mnc',mnp='$mnp' where id='$id'";
mysql_query($sql);
print " $sql";
}
?>

0 个答案:

没有答案