X-editable, not updating the value in the database

I am using the X-Editable plugin in my php application to update the fields of my table and use the POST file to update the database.

Here is the form code:

<table id="restaurant" class="table table-bordered table-striped">
       <tbody>
       <?php 
       echo '
            <tr>
               <td style="width:15%">Restaurant name</td>
               <td style="width:50%"><a href="#" id="name" data-type="text" data-pk="'. escape($arrValues[$i]->r_id) .'" data-name="name" data-placeholder="Required" data-original-title="Enter Restaurant Name" class="editable editable-click" style="display: inline;">'. escape($arrValues[$i]->name) .'</a></td>
               <td style="width:35%"><span class="text-muted">Enter restaurant name.</span></td>
            </tr>';
        ?>
        </tbody>
    </table>

Here is the X-editable JS that I use at the bottom of the page:

<script>
jQuery(document).ready(function() {

//initializes all global values and plugin essentials
    FormEditable.init(); 

//below function is only initialized on one field for debug purposes
    $(function(){
        $('#name').editable({
            url: 'post.php'
        });
    });
});
</script>

Here is the contents of my Post.php file:

<?php 
require 'core/init.php';

    $pk = $_POST['pk']; //primary key aka ID
    $name = $_POST['name']; //name of the field
    $value = $_POST['value']; //value of the field

    if (!empty($value)){
        $result = mysql_query('update Restaurants set '.mysql_escape_string($name).'="'.mysql_escape_string($value).'" where r_id = "'.mysql_escape_string($pk).'"');
        print_r($_POST);
    } else {
        header('HTTP 400 Bad Request', true, 400);
        echo "This field is required!";
    }

?>

When I update the field in the application, the value changes in the DOM, but the value is not updated in the database. This is my first time using the X-Editable plugin, and I'm not very good at JS AJAX calls. Can someone please let me know how I can debug this and find out why my value is not popped into the database.

Any help would be greatly appreciated.

+4
3

, ! "" - .

, , DOM. , ajax ( ) post.php.

-, ajaxOptions put, :

 $('#name').editable({
   type: 'text',
   url: 'post.php',
   ajaxOptions: {
     type: 'put'
   }   
 });

Ajax, "" . . , . , , script.

post.php var_dump ($ _ POST); .

+3

ajaxOptions: {
     type: 'put'
   }  

ajaxOptions: {
     type: 'POST'
   }   

PHP mysql, .

+1

The problem was caused by the mockjax plugin, which was rewriting the ajax function with a different url. The problem was resolved when I removed all the mockjax links from the page.

Thanks, Jens-Andre Koch, for your help!

0
source

Source: https://habr.com/ru/post/1523818/


All Articles