Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preventing page refreshing on anchor tag

As my question seems common but I refer my question on stackoverflow but did not get my solution.I refer How to avoid page refreshing on anchor (<a></a>) tag click? link also.

My Problem:

I am using two dropdown for filtering data. I have been done with the filtration and got data according to result in data table. Now I have three action on data table with anchor tag. When I clicked on that anchor tag my filtered data will not maintain and it clear result.

Kindly help me out of this problem.

My code snippet: For two dropdown

 <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> 
 <div class="controls col-md-3">    
          <select required id='selectError2' placeholder='select Category' class='form-control' name='category'>
            <option value=''>Select Category</option>
               <?php 
                   include("db.php");
                   $sql="SELECT * FROM category";
                   $result=mysql_query($sql);       
                   while($row = mysql_fetch_array($result)){
                       echo "<option value='". $row['category_name'] ."' >". $row['category_name'] ." </option>"; 
                   }
                ?>
        </select>
</div>
<div class="controls col-md-3"> 
             <select required id='selectError2' placeholder='select subcategory'  class='form-control' name='subcategory'>
                <option value=''>Select SubCategory</option>
                     <?php 
                         include("db.php");
                         $sql="SELECT * FROM subcategory";
                         $result=mysql_query($sql);
                         while($row = mysql_fetch_array($result)){
                              echo "<option value='". $row['subcategory_name'] ."'>". $row['subcategory_name'] ."</option>";                                
                         }
                     ?>
         </select>
</div>
<div class="controls col-md-2">
      <input type="submit" class="btn btn-default" name="btn_submit" style="font-weight:bold" value="GO"/>
</div>
</form>
</tr>
</thead>
</table> 

My other data table part is:

<?php             
              while($row = mysql_fetch_array($result)){    
                 /* other stuff */
                 <a class='btn btn-danger' href='product_delete.php?product_id=".  $row['product_id'] ."'>
                     <i class='glyphicon glyphicon-edit icon-white'></i>Delete
                 </a>
                 </td>  
                 $query1 = "query"; //You don't need a ; like you do in SQL
                 $result1 = mysql_query($query1);
                 $count=mysql_num_rows($result1);
                 if($count == 1){
                      echo"<td style='text-align:center'>
                      <a href='unpublished_product.php?product_id=".  $row['product_id'] ."'>
                        <img alt='' src='/admin/img/published.png' style='width:30px;height:30px;' />           
                      </a>
                      </td>";
                  }
                  else {
                      echo"<td style='text-align:center'> 
                      <a href='published_product.php?product_id=".$row['product_id']."'>
                         <img alt='' src='/admin/img/unpublished.png' id=' ". $row['product_id'] . "' onclick='atualiza()' style='width:30px;height:30px;' />       
                      </a>
                      </td>";                     
                  }                         
                  echo"</tr>";   
             }
?>
like image 613
RU_23 Avatar asked Dec 01 '25 23:12

RU_23


1 Answers

<a href="URL"> will always change your location ...
What do you want to achive is simply to use Ajax mechanism, so update your code with someting like this :

<a href="javascript:void(0)" onclick="YOUR_JS_FUNCTION();">Link</a>

and YOUR_JS_FUNCTION() will do Asynchronous request to your server (published_product.php?product_id=xxxx)
Thre are thousands of examples on the internet that show you how to use Ajax with or without additional frameworks

You can find here an example using JQuery

like image 184
Halayem Anis Avatar answered Dec 03 '25 14:12

Halayem Anis