Sjay
Sjay

Reputation: 801

PHP/JQUERY label to input in while loop on edit button click

I'm using php/jquery and html my requirement is when I click on edit button, label should be replaced with input text and I would be able to update in my mysql db and if I click on cancel button input to label..

Following is my code:

<?php 
    $sql = 'select * from demo';
    while($row = mysql_fetch_object($sql)) {
?>
    <label style="display:block;">abc</label>
    <input type="text" style="display:none;"/>

    <button id="edit">edit</button>
    <button id="cancel">cancel</button>
<?php 
    }
?>

suppose If I display 10 records from mysql db, for each record I should be able to edit on particularly clicked row.

Any help is appreciated thanks!

Upvotes: 3

Views: 1721

Answers (1)

GreyRoofPigeon
GreyRoofPigeon

Reputation: 18123

I would wrap each label in a parent (for example a p).

On a click, you hide the label and add a input and two buttons to the parent.

By clicking the cancel button, the label gets visible again and the other elements will be removed.

The "tricky" part is the submit button. You'll need a PHP page that processes the data you post to it. Then when it succeeds you should echo an ok . The $.post function knows a success argument. This function will check if the returned value will be ok, and if so, changes the text from the label, shows it, and removes the other items.

$(function() {
  $('.edit').on('click', function(e) {
    e.preventDefault();
    var elem = $(this) ,
        label = elem.prev('label') ,
        parent = elem.parent() ,
        value = label.text() ,
        input = '<input type="text" value="'+value+'" />' ,
        save = '<button class="save">Save</button>' ,
        cancel = '<button class="cancel">Cancel</button>';
    parent.children().hide();
    parent.append(input+save+cancel);
  });
  
  $(document).on('click', '.cancel', function(e) {
    e.preventDefault();
    var elem = $(this) ,
        parent = elem.parent() ,
        label = parent.find('label');
    parent.children(':not(label,.edit)').remove();
    parent.children().show();
  });
  
  $(document).on('click', '.save', function(e) {
    e.preventDefault();
    var elem = $(this) ,
        parent = elem.parent() ,
        label = parent.find('label') ,
        value = parent.find('input').val();
    parent.addClass('active');
    
    var data = '&name='+value;
    $.post("processingPage.php", data)
    .success( function(returnedData) {
      if( returnedData == 'ok' ) { /* change this to check if the data was processed right */
        var active = $('.active');
        active.find('label').text( active.find('input').val() );
        active.children(':not(label,.edit)').remove();
        active.children.show();
      }
    });
    $('.active').removeClass('active');
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><label>Some text</label><button class="edit">Edit</button></p>
<p><label>Some text</label><button class="edit">Edit</button></p>
<p><label>Some text</label><button class="edit">Edit</button></p>
<p><label>Some text</label><button class="edit">Edit</button></p>

You're PHP would look like this:

<?php
$return = false;
if( isset( $_POST['name'] ) ) {
   if( mysqli_query( ... ) ) {
       $return = true;
   }
}
if( $return ) 
   echo 'ok';
else
   echo 'not ok';
?>

Upvotes: 3

Related Questions