ENTER key save table instead of space

1

I have the following table that lets you edit directly on the grid:

Hercodes:

  

MySQLtable

CREATETABLEIFNOTEXISTS'php_interview_questions'('id'int(8)NOTNULL,'question'textNOTNULL,'answer'textNOTNULL,'row_order'int(8)NOTNULL)INSERTINTO'php_interview_questions'('id','question','answer','row_order')VALUES(1,'PHParrayfunctionsexample','is_array(),in_array(),array_keys(),array_values()',3),(2,'HowtoredirectusingPHP','Usingheader()function',4),(3,'DifferentiatePHPsize()andcount():','Same.Butcount()ispreferable.',1),(4,'WhatisPHP?','Aserversidescriptinglanguage.',0),(5,'Whatisphp.ini?','PHPconfigurationfile.',2);
  

DBController:

<?phpclassDBController{private$host="localhost";
    private $user = "root";
    private $password = "";
    private $database = "blog_examples";

    function __construct() {
        $conn = $this->connectDB();
        if(!empty($conn)) {
            $this->selectDB($conn);
        }
    }

    function connectDB() {
        $conn = mysql_connect($this->host,$this->user,$this->password);
        return $conn;
    }

    function selectDB($conn) {
        mysql_select_db($this->database,$conn);
    }

    function runQuery($query) {
        $result = mysql_query($query);
        while($row=mysql_fetch_assoc($result)) {
            $resultset[] = $row;
        }       
        if(!empty($resultset))
            return $resultset;
    }

    function numRows($query) {
        $result  = mysql_query($query);
        $rowcount = mysql_num_rows($result);
        return $rowcount;   
    }
}
?>
  

index.php

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$sql = "SELECT * from php_interview_questions";
$faq = $db_handle->runQuery($sql);
?>
<html>
    <head>
      <title>PHP MySQL Inline Editing using jQuery Ajax</title>
        <style>
            body{width:610px;}
            .current-row{background-color:#B24926;color:#FFF;}
            .current-col{background-color:#1b1b1b;color:#FFF;}
            .tbl-qa{width: 100%;font-size:0.9em;background-color: #f5f5f5;}
            .tbl-qa th.table-header {padding: 5px;text-align: left;padding:10px;}
            .tbl-qa .table-row td {padding:10px;background-color: #FDFDFD;}
        </style>
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script><script>functionshowEdit(editableObj){$(editableObj).css("background","#FFF");
        } 

        function saveToDatabase(editableObj,column,id) {
            $(editableObj).css("background","#FFF url(loaderIcon.gif) no-repeat right");
            $.ajax({
                url: "saveedit.php",
                type: "POST",
                data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
                success: function(data){
                    $(editableObj).css("background","#FDFDFD");
                }        
           });
        }
        </script>
    </head>
    <body>      
       <table class="tbl-qa">
          <thead>
              <tr>
                <th class="table-header" width="10%">Q.No.</th>
                <th class="table-header">Question</th>
                <th class="table-header">Answer</th>
              </tr>
          </thead>
          <tbody>
          <?php
          foreach($faq as $k=>$v) {
          ?>
              <tr class="table-row">
                <td><?php echo $k+1; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'question','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["question"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'answer','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["answer"]; ?></td>
              </tr>
        <?php
        }
        ?>
          </tbody>
        </table>
    </body>
</html>
  

saveedit.php

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$result = mysql_query("UPDATE php_interview_questions set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE  id=".$_POST["id"]);
?>

The table is all working perfectly, the only thing I want to change is next, when I edit a value and give ENTER the line breaks down, if I click on the screen somewhere vacant the same saves what was typed , what I want, when I press ENTER happens in the same way as if I click on the screen, my information is saved.

Table font: PhpPot Home (If you want to see, there has a demo.)

@Diego Souza

    
asked by anonymous 20.01.2017 / 12:53

1 answer

2

Places in TD a onkeydown calling the same parameters as onblur .

onKeyDown="checkEnter(event, this,'question','<?php echo $faq[$k]["id"]; ?>')"

But calling the function below:

function checkEnter(e, editableObj, column, id){
   if (e.keyCode == 13 && e.shiftKey == false) {
      saveToDatabase(editableObj, column, id);
      e.preventDefault();
   }
}
    
20.01.2017 / 13:10