Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In a web page there are simlar buttons (Accept and discard) only change in id when i press a button it will do ajax post. Below code works fine for me but its generated dynamicaly and its large in size,not optimized and depends on no of rows from mysql result help me to optimize the below code



PHP
<?php
  $sql1=mysql_query("SELECT id FROM freeads where status='0' order by id desc");
while($row=mysql_fetch_array($sql1))
{
    $id=$row['id'];
     
    ?>        
                              
$("<?php echo "#A".$id; ?>").click(function(){
    
   var id=  $("<?php echo "#lbl".$id; ?>").html();
   
var dataString = '&id='+ id;
$.ajax
({
type: "POST",
url: "approve.php",
data: dataString,
cache: false,
success: function(html)
{
$("<?php echo "#D".$id; ?>").fadeOut(200);
} 
});

                      
    });
    
    $("<?php echo "#DIS".$id; ?>").click(function(){
        
         var did=  $("<?php echo "#lbl".$id; ?>").html();
        var dataString = '&did='+ did;
                       
$.ajax
({
type: "POST",
url: "approve.php",
data: dataString,
cache: false,
success: function(html)
{
$("<?php echo "#D".$id; ?>").fadeOut(200);
} 
});
     
                         
$("<?php echo "#D".$id; ?>").css( "background-color" , "darkslateblue");
                  
});
    
    <?php
}

?>
Posted
Updated 18-Jun-13 2:48am
v2

1 solution

Don't repeat code twice with different selectors. A jQuery selector returns a jQuery wrapper for a DOM object. Do it separately from Ajax call. Make Ajax part a function which accepts a DOM object wrapper parameter, call this function as many times as you want for different selectors.

—SA
 
Share this answer
 
v2
Comments
vishnulalr 18-Jun-13 11:09am    
thanks for you reply it help me a lot i have done the following is there any space for optimization
<pre lang="PHP">
<script>

function accept(id)
{
var dataString = '&id='+ id;
$.ajax
({
type: "POST",
url: "approve.php",
data: dataString,
cache: false,
success: function(html)
{
$("#D"+id).fadeOut(200);
}
});

}
</script>
<script>



$("").click(function(){
accept($("").html());
});

$("").click(function(){
discard($("").html());
});



</script>

</pre>
Sergey Alexandrovich Kryukov 18-Jun-13 12:02pm    
Why this Ajax call? Just to get success? Why? What it does? Why this selector: $("")?! It won't select anything, will it? :-)
And you did not do what I advised. The parameter of them method should be the result returned by selector, not "id".
—SA
vishnulalr 23-Jun-13 9:40am    
some problem in code project reply sysytem i had copy paseted the code but it show like this $("")

here is the site http://mrjinn.net23.net/Admin/#tabs1-js please check
Sergey Alexandrovich Kryukov 23-Jun-13 12:33pm    
Why are you showing some merchandize site? If you want to show some code, do it in your question using "Improve question".
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900