dream6120 2015-05-13 00:28
浏览 57
已采纳

Javascript事件处理程序还是?

I have a question. I have a php file with queries of delete, insert, and to display data. There's a delete button in the file as well. To execute the delete query, it'll be something like

if($_POST['delete'])
 {
     //execute delete query
 }

which is in the PHP file. Now I have implemented ajax and I want to execute the delete query only when the button is clicked, but by using a function in javascript(I'm not too sure if this is the correct way or most suitable way). So everytime the delete button is clicked, a javascript function would be executed so that ajax would work. However it doesn't seem to do anything. Am I doing it the right way by using a javascript function to execute on button click or is there any other better ways? I'm doing this because I would like all the queries to be in one file so that's why I'm using an if statement to detect whether the button was clicked previously.

P.S. I'm using pure javascript codes as I am still learning so if there are any suggestions, I would prefer the raw codes rather than jQuery. Do comment if you would like to see the codes as I have asked a similar question before but I didn't find the answer I was looking for.

EDIT: Here are the codes

This is the javascript external file(for delete function only)

function deleteButtonClick(){
  //Perform the Ajax request to delete this user
  var del = document.getElementById("delete").innerHTML;
  var delRow = document.getElementById("deleteRow").innerHTML;
  var page = "database.php";
  var parameters = 'delete=true&deleteRow='+delRow;
  var xmlhttp = new XMLHttpRequest();

    if(confirm('Are you sure you want to delete this?')==true){

      xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
         return false;
        }
      }
      xmlhttp.open("GET", page+"?"+parameters, true);
      xmlhttp.send(null);
     }
    }

These are the codes in the PHP file to execute the query

   // Delete Row
    if(isset($_POST['delete'])){
     $id = $_POST['deleteRow'];
     $query_string = "delete from $table_info where user_id='$id'";
     $result = @mysql_query($query_string);
     echo "row deleted";
     }

and these are the codes in the same PHP file, for the delete button at every row in a table

 echo "<td><input type='hidden' id='deleteRow' value=".$row['user_id']."/>";
 echo "<input type='button' id='delete' value='Delete' onclick='deleteButtonClick()'/></td>";
 echo "</tr></form>";
  • 写回答

1条回答 默认 最新

  • dongre6404 2015-05-15 03:51
    关注

    I managed to code at the most simplest way to do this.

    function deleteThis(count){
    
     var id  = document.getElementById("row"+count).innerHTML;
     var page = "database.php";
     var parameters = 'id='+id+'&action=delete';
     var xmlhttp = new XMLHttpRequest();
    
     if(confirm('Are you sure you want to delete this?')==true){
    
     if(xmlhttp==null){
      alert("Your browser does not support ajax!");
      return false;
     }
     xmlhttp.onreadystatechange=function(){
      document.getElementById("divTable").innerHTML=xmlhttp.responseText; 
     };
     xmlhttp.open("GET", page+"?"+parameters, true);
     xmlhttp.send(null);
     }
    }
    

    In the PHP file:

                                      .
                                      .
                                Some other codes
                                      . 
                                      .
    
    
    function insertRow($name, $address, $phone, $gender, $nation){
      $table_info = "info";
      $query_string = "insert into $table_info(name, address, phone, gender, nation) values('$name', '$address', '$phone', '$gender', '$nation');";
      $result = @mysql_query($query_string) or die (mysql_error());  
    }
    ?>
    
    <?php
    $action = rtrim($_REQUEST['action']);
    
    if($action=="delete"){
    
     $id  = rtrim($_REQUEST['id']);
     echo deleteRow($id);
     echo update_database(); 
    }
    elseif($action=="insert"){
    
     $name = $_REQUEST['name'];
     $address = $_REQUEST['address'];
     $phone = $_REQUEST['phone'];
     $gender = $_REQUEST['gender'];
     $nation = $_REQUEST['nation'];
    
     echo insertRow($name, $address, $phone, $gender, $nation);
     echo update_database();
    }
    else{
     echo update_database();
    }  
    ?>
    

    Basically, I overlooked the capabilities of php and forgot that I can still manipulate the whole logic with functions. I removed the whole form and hidden input from the button entirely and used onclick to execute a function based on a variable $count. It increments every time a row is added which makes it easier as I confused myself with using the actual unique ID that was available in the database. All I had to do after that was request the data I needed and use it in my function to execture the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)