update data using jquery

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semanticnotion
    New Member
    • Sep 2010
    • 66

    #1

    update data using jquery

    I have a page that lists records from a database call. I want to have an 'edit' link on each row that will popup a Jquery dialog so that the row can be edited. My question is how do I pass the data from the selected row to the jquery dialog box so that it can be edited?
    i have the code which open jquery popup but the textboxes are empty.

    Code:
    <div id="boxes">
    
    <div id="dialog2" class="window">
    <form method="post" action="update_books_a.php">
      <input name="name" type="text" value="<?php echo $ing['book_name']; ?>"/><br/>
      <input name="author" type="text" value="<?php echo $ing['author']; ?>"/>
    
        <input type="hidden" name="book_id" value="<?php print $ing['book_id'];?>" />
        <br/><br/>
    <input type="submit" value="Update" class="close"/>
    </form>
    </div>
    myphp code is
    Code:
    $select=mysql_query("select * from books where book_id='$book_id'") or die(mysql_error());
    while($ing=mysql_fetch_array($select))
    {
     ?>
    <tr>
        <td><a href="#dialog2" name="modal"><?php echo $ing['book_name'];?></a></td>
        <td><?php echo $ing['author'];?></td></tr>
    <?php
    }
    ?>
    and the java scipt is
    Code:
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
    <script>
    
    $(document).ready(function() {
    
    	//select all the a tag with name equal to modal
    	$('a[name=modal]').click(function(e) {
    		//Cancel the link behavior
    		e.preventDefault();
    
    		//Get the A tag
    		var id = $(this).attr('href');
    
    		//Get the screen height and width
    		var maskHeight = $(document).height();
    		var maskWidth = $(window).width();
    
    		//Set heigth and width to mask to fill up the whole screen
    		$('#mask').css({'width':maskWidth,'height':maskHeight});
    
    		//transition effect
    		$('#mask').fadeIn(1000);
    		$('#mask').fadeTo("slow",0.8);
    
    		//Get the window height and width
    		var winH = $(window).height();
    		var winW = $(window).width();
    
    		//Set the popup window to center
    		$(id).css('top',  winH/2-$(id).height()/2);
    		$(id).css('left', winW/2-$(id).width()/2);
    
    		//transition effect
    		$(id).fadeIn(2000);
    
    	});
    
    	//if close button is clicked
    	$('.window .close').click(function (e) {
    		//Cancel the link behavior
    		e.preventDefault();
    
    		$('#mask').hide();
    		$('.window').hide();
    	});
    
    	//if mask is clicked
    	$('#mask').click(function () {
    		$(this).hide();
    		$('.window').hide();
    	});
    
    });
    
    </script>
    <style type="text/css">
    body {
    font-family:verdana;
    font-size:15px;
    }
    
    a {color:#333; text-decoration:none}
    a:hover {color:#ccc; text-decoration:none}
    
    #mask {
      position:absolute;
      left:0;
      top:0;
      z-index:9000;
      background-color:#000;
      display:none;
    }
    
    #boxes .window {
      position:absolute;
      left:0;
      top:0;
      width:440px;
      height:200px;
      display:none;
      z-index:9999;
      padding:20px;
    }
    
    
    
    #boxes #dialog2 {
      background:url(../images/notice.png) no-repeat 0 0 transparent;
      width:326px;
      height:229px;
      padding:50px 0 20px 25px;
    }</style>
  • Joseph Cobham
    New Member
    • Jan 2012
    • 23

    #2
    you could use jquery to execute the php script and return values to a named div for example

    the html code
    Code:
    Bla Bla Bla Title <br />
    <div style="width:950px">
    <div style="float:left; width:450px">
    Your List of Book here
    <div id="dialog2" class="window">
    <form method="post" action="javascript: editBook();">
      <input name="name" type="text" value="<?php echo $ing['book_name']; ?>"/><br/>
      <input name="author" type="text" value="<?php echo $ing['author']; ?>"/>
     
        <input type="hidden" name="book_id" value="<?php print $ing['book_id'];?>" id="book_id" />
        <br/><br/>
    <input type="submit" value="Update" class="close" onclick="javascript: editBook();"/>
    </form>
    </div>
    
    <div style="float:left" id="showedit">
    <!--your edit will show here-->
    </div>
    </div>
    Note the form action attribute value well as well as the div id showedit and the hidden form field id book_id. your html ends here...


    Now time for the javascript function

    Code:
        <script type="text/javascript" language="javascript">
    	   function editBook(){
    	$("#showedit").html("Preparing Book Edit <br /><img src=\"images/load.gif\" />"); $.get("edit.php",{ cmd: "edit", b_id: $("#book_id").val() } ,function(data){  $("#showedit").html(data); });
    	   }
    	</script>
    your javascript ends here...

    Now the php file named edit.php should be on the same directory as your current page...

    Code:
    <?php
    
    include 'my_database_connection_file or settings.php';
    
    $b_id = mysql_real_escape_string($get['b_id']);
    
    $select=mysql_query("select * from books where book_id='$b_id'") or die(mysql_error());
    while($ing=mysql_fetch_array($select))
    {
     ?>
    <tr>
        <td><a href="#dialog2" name="modal"><?php echo $ing['book_name'];?></a></td>
        <td><?php echo $ing['author'];?></td></tr>
    <?php
    }
    }
    
    ?>
    this should work without reloading the page and if you run into error please let me know.

    Comment

    Working...