By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,688 Members | 1,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,688 IT Pros & Developers. It's quick & easy.

Ajax-How to create delete button for each record after search

P: 5
I want to design a webpage where user can search the data from the database and list out the related records. Each of the record got a delete button which allow user to delete the record.


Filename : search_student.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("../user_access/user_access_control.php");
  3. include("../Database/database.php");
  4.  
  5. $searchStudentControl = new Access_user;
  6.  
  7. // call the levelAccess method to check the user access level
  8. $searchStudentControl->levelAccess($_SESSION['level'],1);
  9. ?>
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  14. <title>Untitled Document</title>
  15. <style type="text/css">
  16. <!--
  17. body {
  18.     margin-left: 0px;
  19.     margin-top: 0px;
  20.     margin-right: 0px;
  21.     margin-bottom: 0px;
  22. }
  23. .style1 {
  24.     font-size: 18px;
  25.     color: #FFFFFF;
  26. }
  27. -->
  28. </style>
  29. <script type="text/javascript" src="engine.js"></script>
  30. </head>
  31.  
  32. <body>
  33. <table width="707" border="0" cellspacing="0" cellpadding="0">
  34.   <tr>
  35.     <td width="62" height="31">&nbsp;</td>
  36.     <td colspan="5"></td><td width="1"></td>
  37.   </tr>
  38.   <tr>
  39.     <td>&nbsp;</td>
  40.     <td colspan="5"><form>
  41.       <table width="641" border="0" cellspacing="0" cellpadding="0">
  42.         <tr>
  43.           <td width="173"><input name="sData" type="text" id="sData">
  44.             by</td>
  45.           <td width="80"><select name="sSelect" id="sSelect">
  46.             <option value="Stu_ID">ID</option>
  47.             <option value="Stu_Name">Name</option>
  48.             <option value="Class_Code">Class</option>
  49.           </select></td>
  50.           <input name="sTable" type="hidden" value="student_recording">
  51.           <td width="72"><input name="search" type="button" onClick="getSearch(sData.value,sSelect.value,sTable.value,'search');" value="Search"></td>
  52.           <td width="316">&nbsp;</td>
  53.         </tr>
  54.       </table>
  55.     </form></td>
  56.   </tr>
  57.   <tr>
  58.     <td>&nbsp;</td>
  59.     <td colspan="3">&nbsp;</td>
  60.     <td width="6">&nbsp;</td>
  61.     <td width="207">&nbsp;</td>
  62.   </tr>
  63.   <tr>
  64.     <td>&nbsp;</td>
  65.     <td colspan="5" bgcolor="#999999">&nbsp;</td>
  66.   </tr>
  67.   <tr>
  68.     <td>&nbsp;</td>
  69.     <td colspan="5" bgcolor="#666699"><strong><span class="style1">:: Student Record :: </span></strong></td>
  70.   </tr>
  71.   <tr>
  72.     <td rowspan="4">&nbsp;</td>
  73.     <td colspan="5"></td>
  74.   </tr>
  75.   <tr>
  76.     <td width="230"><strong>Student Name </strong></td>
  77.     <td width="157"><strong>Contact Number </strong></td>
  78.     <td width="44"><strong>Class</strong></td>
  79.     <td>&nbsp;</td>
  80.     <td>&nbsp;</td>
  81.   </tr>
  82.   <tr>
  83.     <td colspan="5"><hr></td>
  84.   </tr>
  85.  
  86.   <tr>   
  87.     <td colspan="5">
  88.     <div id="getResult"></div>
  89.     </td>    
  90.   </tr>
  91. </table>
  92. </body>
  93. </html>
  94.  

Filename : engine.js
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp;
  2.  
  3. function deleteData(paction)
  4. xmlHttp=GetXmlHttpObject()
  5. if (xmlHttp==null)
  6.  {
  7.  alert ("Browser does not support HTTP Request")
  8.  return
  9.  } 
  10. var url="../user_access/process.php"
  11. url=url+"?id="+paction
  12. url=url+"&sid="+Math.random()
  13. xmlHttp.onreadystatechange=stateChanged 
  14. xmlHttp.open("GET",url,true)
  15. xmlHttp.send(null)
  16. }
  17.  
  18. function getSearch(pvalue,ptype,ptable,paction)
  19. xmlHttp=GetXmlHttpObject()
  20. if (xmlHttp==null)
  21.  {
  22.  alert ("Browser does not support HTTP Request")
  23.  return
  24.  } 
  25. var url="../user_access/process.php"
  26. url=url+"?value="+pvalue+"&type="+ptype+"&table="+ptable+"&action="+paction
  27. url=url+"&sid="+Math.random()
  28. xmlHttp.onreadystatechange=stateChanged 
  29. xmlHttp.open("GET",url,true)
  30. xmlHttp.send(null)
  31. }
  32.  
  33. function stateChanged() 
  34. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  35.  { 
  36.  document.getElementById("getResult").innerHTML=xmlHttp.responseText
  37.  } 
  38. }function GetXmlHttpObject()
  39. {
  40. var xmlHttp=null;
  41. try
  42.  {
  43.  // Firefox, Opera 8.0+, Safari
  44.  xmlHttp=new XMLHttpRequest();
  45.  }
  46. catch (e)
  47.  {
  48.  //Internet Explorer
  49.  try
  50.   {
  51.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  52.   }
  53.  catch (e)
  54.   {
  55.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  56.   }
  57.  }
  58. return xmlHttp;
  59. }
  60.  

Filename : process.php
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <title>Untitled Document</title>
  6. <style type="text/css">
  7. <!--
  8. body {
  9.     margin-left: 0px;
  10.     margin-top: 0px;
  11.     margin-right: 0px;
  12.     margin-bottom: 0px;
  13. }
  14. .style1 {
  15.     font-size: 18px;
  16.     color: #FFFFFF;
  17. }
  18. -->
  19. </style>
  20. <script type="text/javascript" src="../admin_sys/engine.js"></script>
  21. </head>
  22. <?php
  23. include("../Database/database.php");
  24.  
  25.     if($_GET['action'] == "search"){
  26.       showData(); 
  27.     }else if($_REQUEST['action'] == "Edit"){
  28.         edit();
  29.     }else if($_REQUEST['action'] == "delete"){
  30.         delete();
  31.     }
  32.     function view(){
  33.         global $conn;
  34.  
  35.         $Viewsql = "SELECT * FROM " . $_REQUEST['Table'] . " WHERE ( " . $_REQUEST['Type'] . "='" . $_REQUEST['sID'] . "')";
  36.         $result = $conn->query($Viewsql);    
  37.  
  38.         while($data = $result->fetch_assoc()){
  39.                  echo $data['Stu_ID'];
  40.                 echo "<br>";
  41.                 echo $data['Stu_Name'];
  42.                 echo "<br>";
  43.                 echo $data['Stu_Race'];                
  44.         }
  45.     }
  46.  
  47.     function edit(){
  48.     }
  49.  
  50.     function delete(){
  51.         global $conn;
  52.  
  53.         $sql="DELETE FROM ".$_GET['table']." WHERE (Stu_ID = ".$_GET['id'].")";
  54.         $conn->query($sql);
  55.         echo "Data Deleted";
  56.         showData();
  57.     }
  58.  
  59.     function showData(){
  60.  
  61.         global $conn;
  62.  
  63.         $sql="SELECT * FROM ".$_GET['table']." WHERE (".$_GET['type']." ='".$_GET['value']."')";
  64.         $result = $conn->query($sql);
  65.  
  66.         echo "<table width='641' border='0' cellspacing='0' cellpadding='0'>";
  67.         while($data = $result->fetch_assoc()){
  68.         echo "<tr>";
  69.         echo "<td width='230'>" . $data['Stu_Name'] . "</td>";
  70.         echo "<td width='153'>" . $data['Stu_Tel'] . "</td>";
  71.         echo "<td width='22'>" . $data['Class_Code'] . "</td>";
  72.         echo "<td width='48'></td>";
  73.         echo "<td width='188'><div align='right'>";
  74.         echo "<input type='button' onClick='process('delete');' value='Delete'>";
  75.         echo "</div></td>";
  76.         echo "</tr>";
  77.         echo "<tr>";
  78.         echo "<td colspan='5'><hr></td>";
  79.         echo "</tr>";
  80.           }
  81.       echo  "</table>";
  82.      }
  83. ?>
  84.  
Im getting error if i put input type='button' onClick='process('delete');' value='Delete'>"; in process.php. I know my design structure is not correct. Can anyone get me an idea about the correct way to design.

can i display the delete button for each record when the record is exist in search_student.php.

Thanks in advance
Bluez
Apr 15 '07 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
The reason why you're getting an error is that you've got a quote within a quote. Javascript thinks the quote has ended and then comes across the word 'delete' which it obviously doesn't understand.

You have two options: either escape the quotes using backslash (\) or use single and double quotes together, e.g.
[HTML]<input type='button' onClick="process('delete');" value='Delete'>[/HTML]
Apr 16 '07 #2

P: 5
Thx..it's work

and sorry for the late reply
Apr 21 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Thx..it's work

and sorry for the late reply
No problem. Glad you got it working.
Apr 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.