Refresh div contents after making changes

Below is my code which is working fine...

<Script type="text/javascript">
function im()
{
    var Name=document.getElementById("Name").value;
    var pointsize=document.getElementById("pointsize").value;
    var format=document.getElementById("format").value;
    var bckclr=document.getElementById("bckclr").value;
    var color=document.getElementById("color").value;
    var bcolor=document.getElementById("bcolor").value;
    var font=document.getElementById("font").value;

    $(document).ready(function(){
        var url = 'Name='+Name+'&pointsize='+pointsize+
                  '&format='+format+'&bckclr='+bckclr+
                  '&color='+color+'&bcolor='+bcolor+
                  '&font='+font;

        alert(url);
        //-----Sending request to server for getting job name list by ajax-----
        $.ajax({
            type    : "POST",
            url : "i.php?",
            data    : url,              
            cache   : false,
            success : function(html) {
                //document.getElementById('Div_PJobId').style.display="block";
                //alert('hi');
                alert(html);
                var pic='<img src="'+html+'">';
                $("#Div_Im").html(pic).show();
            }
        });
    });
}
</script>

and php code...

echo('Name: <input type="text" id="Name" onchange="im()" value="your name"  name="Name" />');

echo('pointsize: <input type="text" id="pointsize" onchange="im()" value="50" name="pointsize" />');

echo('format: <input type="text" id="format" value=".gif" onchange="im()" name="format" />');

echo('BackGround Color: <input type="text" id="bckclr" value="red" onchange="im()" name="bckclr" />');

echo('FontColor: <input type="text" id="color" value="white" onchange="im()" name="color" />');

echo('Border Color: <input type="text" id="bcolor" value="blue" onchange="im()" name="bcolor" />');

echo('<a href="./lang/ims.php"><img src="'.$image.'" height="82" width="82" /></a>');

echo('Font: <input type="text" id="font" value="'.$image1.'" onchange="im()" name="font" />');

echo ('<div id="Div_Im">');
echo('replace me');
echo ('</div>');

As you can see, I am using AJAX to fetch data from server and displayed within its working fine but I need to refresh div everytime I make changes .

How to refresh div when I make some changes... also I need to add a symbol or loading image, when the data is being from server.....How to do that?

Answers


Maybe try this:

Javascript
<script type="text/javascript">
$(document).ready(function(){

  $('#theIMform input')
    .change(function(){
      // Automatically creates a Query String for all fields within the parent form
      formdata = $(this).closest('form').serialize();

      $.ajax({
        type : "POST" ,
        url : "i.php" ,
        data : formdata ,              
        cache : false ,
        error : function( jqXHR , textStatus , errorThrown ){
          // Something went wrong
        } ,
        success : function( data , textStatus , jqXHR ){
          $( "#div_im" )
            .hide()
            .html( '<img src="'+data+'">' )
            .show();

        }
      });
    });

});
</script>
PHP
?>
<form id="theIMform" name="theIMform">
  Name: <input type="text" id="Name" value="your name"  name="Name" />
  pointsize: <input type="text" id="pointsize" value="50" name="pointsize" />
  format: <input type="text" id="format" value=".gif" name="format" />
  BackGround Color: <input type="text" id="bckclr" value="red" name="bckclr" />
  FontColor: <input type="text" id="color" value="white" name="color" />
  Border Color: <input type="text" id="bcolor" value="blue" name="bcolor" />
  <a href="./lang/ims.php"><img src="<?php echo $image; ?>" height="82" width="82" /></a>
  Font: <input type="text" id="font" value="<?php echo $image1; ?>" name="font" />
</form>
<div id="div_im">replace me</div>
<?php

When you change a page element it's refreshed automatically.

I think your problem is about this line

$("#Div_Im").html(pic).show();

because you call show to the img element and not on the div.

try to change it in

$("#Div_Im").html(pic);
$("#Div_Im").show();

furthermore i suggest you use jquery to get values, so you need to change color=document.getElementById("color").value; in a more easy color = $('#color').val();


Need Your Help

maven-jetty-plugin for jetty 7.3 and forward

maven jetty

Where is maven-jetty-plugin for jetty 7 ?

AngularJS: validation in model

javascript angularjs validation

How to validate data inside model in AngularJS?