Saturday, December 12, 2009

Edit record using ajax

If u need ajax for edit your display record. it display all record in table row column format that will save (edit) by sending data using ajax javascript function to server php file and get response from server of fail and success and intimate user about the server response.




  //****************************************   Edit ajax file ****************************************

<?php

 $dbc=mysql_connect('localhost','root','');

  mysql_select_db('databasename',$dbc);

  $do = $_REQUEST['do'];

  $cateId = $_REQUEST['cateId'];

  switch($do) {

  case 'edit':

  $newTitle=$_REQUEST['ptitle'];

  $newDesc=$_REQUEST['pDesc'];

  $photoId=$_REQUEST['Id'];

 

  if(get_magic_quotes_gpc()) {

  $photoId = $photoId;

  $newTitle=$newTitle;

  $newDesc=$newDesc;

 

  }else{

  $photoId = addslashes($photoId);

  $newTitle = addslashes($newTitle);

  $newDesc = addslashes($newDesc);

  }

  $updatPhoto="update photo_gallery set title='$newTitle',description='$newDesc' where id=$photoId";

  if(mysql_query($updatPhoto,$dbc)or die('Error in update : '.mysql_error())){

  header('Content-Type: text/xml');

  header('Pragma: no-cache');

  echo '<?xml version="1.0" encoding="UTF-8"?>';

  echo '<result>';

  echo 'true';

  echo '</result>';

  }else{

  header('Content-Type: text/xml');

  header('Pragma: no-cache');

  echo '<?xml version="1.0" encoding="UTF-8"?>';

  echo '<result>';

  echo 'false';

  echo '</result>';

  }

 

  break;

  default:

  echo 'Error, invalid action';

  break;

  }

  ?>

//***************************************AJax file end*************************************************


//*********************** Edit html File Start ***********************************************



<script type="text/javascript">

// *************************Ajax call function start***************************************

function editPhotoDetail(Id){

  if (window.XMLHttpRequest) {

  http = new XMLHttpRequest();

  }else if (window.ActiveXObject) {

  http = new ActiveXObject("Microsoft.XMLHTTP");

  }

 

  var handle = Id;

 

  var url = 'ajax.php?';

  //alert(handle);

  var newtitle=document.getElementById('title'+handle).value;

  var newDesc=document.getElementById('desc'+handle).value;

  //alert('title : ' + newtitle+'   '+photoId  );

  // alert('Desc : ' + newDesc);

  if(handle > 0) {

  //alert('call statechange');

  var fullurl = url + 'do=edit&Id=' + encodeURIComponent(handle)+'&ptitle=' + encodeURIComponent(newtitle) + '&pDesc=' + encodeURIComponent(newDesc);

  http.open("POST", fullurl, true);

  http.send(null);

  http.onreadystatechange = statechange_detailchange(handle);

 

  }

 

 

  }


function statechange_detailchange(handle) {

  //alert('in statechange');

  if (http.readyState == 4) {

  var xmlObj = http.responseXML;

  var rs = xmlObj.getElementsByTagName('result').item(0).firstChild.data;

  //    alert('RS '+rs);

  if(rs=="true"){

 

  errorField = document.getElementById('msgbox'+handle);

  errorField.innerHTML = "Photo Record Successfully Update";

  errorField.style.visibility='visible';

  errorField.style.border='#339900  solid 1px';

  errorField.style.background='#FBE3E4';

  errorField.style.color='#339900';

  errorField.style.fontSize='12px';

 

  }else{

  errorField = document.getElementById('msgbox'+handle);

  errorField.innerHTML = "Photo Record Update Fail.";

  errorField.style.visibility='visible';

  errorField.style.border='#FF3333 solid 1px';

  errorField.style.background='#C9FFCA';

  errorField.style.color='#8a1f11';

  errorField.style.fontSize='12px';

 

  }

  }

}

//*********************************Ajax call function end*********************************

  </script>


//********************************Record fetch from database and display for edit

  <table>

  <?php



  $selRec="select * from table";

  $rsRec=mysql_query($selRec,$dbc);

  while($row=mysql_fetch_array($rsRec)){

  ?>

  <tr>

  <td><input type="radio" name="coverId" value="<?php echo $newId;?>" onclick="setId(<?php echo $newId;?>)"/></td>

  <td height="170px" width="190px" valign="middle" align="center">

 

 

 

  <span id="msgbox<?php echo $row["id"];?>"></span>

  </td>

  <td valign="top"><input type="text" size="30" name="title<?php echo $row["id"];?>" id="title<?php echo $row["id"];?>" value="<?php echo $row["title"];?>"></td>

  <td valign="top"><textarea name="desc<?php echo $newId;?>" id="desc<?php echo $row["id"];?>" rows=7><?php echo $row["descs"];?></textarea>    </td>

  <td align="center"><a onclick="editDetail(<?php echo $row["id"];?>)"><span style="color:#003300; background-color:#C4FFE1; font-family:Arial, Helvetica, sans-serif; font-size:14px;">Edit</span></a></td>

  </tr>

  <?php

  }?>

  </table>

//*****************************Html file end***************************************

No comments:

Post a Comment