PUT Support for Jeditable

September 3rd, 2008

Until now Jeditable only supported submitting edited data using POST method. This was against REST principles. POST should be used for creating new objects. PUT should be used for updating existing objects.

Most browsers do not natively support PUT method. Jeditable does it Rails way by using POST method but submitting additional variable called _method with value put.

Jeditable now has basic unit tests covering most configuration parameters. If you find any failing tests drop me a line. Download latest as source, minified or packed.

Changes Since 1.6.0

Submit method can now be POST (default) or PUT.

$(".editable").editable("http://www.example.com/save.php", { 
    method : "PUT" 
});

Bug where form was submitting twice if using code like below was fixed. Thanks to Hannes Tydén of soundcloud.com for patch.

$(".editable").editable("http://www.example.com/save.php", { 
    settings.submit : '<button type="submit">Save</button>'
});

Note that code below generates exactly the same HTML as code above.

$(".editable").editable("http://www.example.com/save.php", { 
    settings.submit : 'Save'
});
 

10 Responses to “PUT Support for Jeditable”

  1. Caleb Cushing says:

    Any chance you could post a small tutorial on what to put in your SSS (server side scripts) maybe in php?

    also is it possible to say for example to have jeditable applied to targets by class (e.g. $(”.edit”) ) but have it apply the same ‘name’ as the ‘id’ of the class it’s selected when posting? that way I only have to write the jeditable script once even though it’s going to send different names on different parts of the form.

  2. Mika Tuupola says:

    Good idea. In the meanwhile check the PHP code used in demos. They should be pretty self explanatory.

    There is also a tutorial on using Jeditable with CakePHP in Cakebaker.

    For the second question, you can (and you usually should) attach Jeditable to a class. For example .editable.

    <div class=”editable” id=”foo>Lorem ipsum</div>
    <div class=”editable” id=”bar>Dolor sit amet</div>

    You only need to attach to class, not id’s. When you edit the first div and submit data, following will be sent:

    id=foo&value=newtext
    

    When you edit second div and submit data, following will be sent:

    id=bar&value=newtext
    

    If you for some reason want to override this, just define id using submitdata parameter:

    $(".charcounter").editable("http://www.example.com/save.php", { 
        submitdata : {id : "something"}
    });
    

    That said, you usually do not want to override id. You need some unique identifier for saving and loading updates from database.

  3. Caleb Cushing (xenoterracide) says:

    I don’t want to override id. here’s an example of my code

    $(document).ready(function() {
        $("#concept").editable("save.php", {
            name    :   'concept',
            height  :   '20px',
            width   :   '100px'
        });
        $("#faction").editable("save.php", {
            name    :   'faction',
            height  :   '20px',
            width   :   '100px'
        });
        $("#template").editable("save.php", {
            name    :   'concept',
            height  :   '20px',
            width   :   '100px'
        });
    });
    

    so the normal string submitted is the id and the value but I basically want to refer to them in the php like this…

    $_POST[‘concept’];

    to do that I basically would want jeditable to send the same name as the id on the class it’s selecting.

  4. Caleb Cushing (xenoterracide) says:

    also I’m not sure I really understand how to use id=bar&value=newtext from a php perspective. I don’t think that can be accessed as $POST[‘bar’] can it? most of what I see is OOP for retrieving the data… I don’t really char about OOP… I think it adds complexity and bloat in most languages.

  5. Caleb Cushing (xenoterracide) says:

    also I’m not sure I really understand how to use id=bar&value=newtext from a php perspective. I don’t think that can be accessed as $POST[‘bar’] can it? most of what I see is OOP for retrieving the data… I don’t really care about OOP… I think it adds complexity and bloat in most languages.

  6. Caleb Cushing (xenoterracide) says:

    I get it now…

    javascript

    $(document).ready(function() {
        $(".edit").editable("save.php", {
            name    :   'edit',
            height  :   '20px',
            width   :   '100px'
        });
    
    <?php
        if (!empty($_POST['edit'])) {
            echo $_POST['edit'];
            unset($_POST['edit']);
    // to get the id and give it back
            echo $_POST['id'];
        }
    ?>
    
  7. Mika Tuupola says:

    Caleb: “also I’m not sure I really understand how to use id=bar&value=newtext from a php perspective. I don’t think that can be accessed as $POST[‘bar’] can it? most of what I see is OOP for retrieving the data. I don’t really char”

    There are some good tutorials about PHP and forms. If browser POSTs string id=bar&value=newtext that will be available in PHP as:

    print $_POST['id'];
    print $_POST['value'];
    

    A good way to debug what your browser is sending is doing something like:

    print "POST: ";
    print_r($_POST);
    print "GET: ";
    print_r($_GET);
    

    “to do that I basically would want jeditable to send the same name as the id on the class it’s selecting.”

    I still do not understand the sentence. However here is explanation one again.

    • By default Jeditable POST:s value of edited elements attribute id as $_POST[‘id’].
    • By default Jeditable POST:s the edited content in $_POST[‘value’].

    This means string submitted via POST to server looks like this:

    id=foo&value=newtext
    

    If this is not what you want you can alter the behaviour using id and name settings. These settings alter the name of the parameter submitted to server. For example:

    $(".editable").editable("http://www.example.com/save.php", { 
        id   : 'element_id',
        name : 'content'
    });
    

    With setting above following will be POST:ed to server.

    element_id=foo&newvalue=newtext
    
  8. Simon says:

    Hi there, many hanks for this. I’m trying to send the input to a mysql database with php. I’‘m struggling with the following code. How do I retrieve the user input and add it to my UPDATE query?

    <?php
    $txt = $_POST["value"]; // I guess this is where I'm going wrong
    
    $con = mysql_connect("localhost","user","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("mydb", $con);
    
    mysql_query("UPDATE editinplace SET txt = '$txt' WHERE id='1' ");
    
    mysql_close($con);
    
    ?>
    

    Your help with this would be greatly appreciated.

    Simon

  9. jamieorc says:

    I’m trying out jeditable with Ruby on Rails 2.1. I’m having a problem generating a Rails resource-style URL. I need the target to be constructed from the base path like ”/categories/” plus the id of the item:

    target : ”/categories” + this.id

    The problem is that this.id is undefined.

    Any idea how I can solve this?

  10. Mika Tuupola says:

    jamieorc: Now when I think of it there is no easy way to do it. I have one idea of solution. Needs an update to Jeditable code. I try to make it during this weekend.

Leave a Reply



(will not be published)



(you can use Textile for formatting)