从php数组中删除行

时间:2014-12-22 05:04:55

标签: php jquery html

我在第一页(test.php)中有表格如下

 <form method="post" action="newtest.php">
  <input  name="product[]"  type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>  alt="1607.00" />
  <input name="product[]" type="checkbox" value="2" <?php if(in_array("2", $session_products)) echo "checked='checked'"; ?> alt="1848.00" /> 
  <input name="product[]" type="checkbox" value="3" <?php if(in_array("3", $session_products)) echo "checked='checked'"; ?> alt="180.00" />
  <input name="product[]" type="checkbox" value="4" <?php if(in_array("4", $session_products)) echo "checked='checked'"; ?> alt="650.00" />
  <input name="product[]" type="checkbox" value="5" <?php if(in_array("5", $session_products)) echo "checked='checked'"; ?> alt="495.00" />
     <div class="ph-float">
         <input type="submit" name="button" value="Checkout >>" class="ph-button ph-btn-green fm-submit" disabled="true" >
     </div> 
</form>

基于发布的复选框选择,使用foreach循环在表格中显示Book name,Amount和totalamount,如下所示(newtest.php)

    <?php     
$product = array();
$product[1] = array('name' => "Text Book of Human Anatomy by B.D.Chaurasiavol 6th edition Vol -I Vol-II Vol-III.", 'price' => 1607);
$product[2] = array('name' => "Nettars Atlas of Anatomy", 'price' => 1848);
$product[3] = array('name' => "Genera Anatomy by B.D.Chaurasia", 'price' => 180);
$product[4] = array('name' => "Inderbir Singh Embryology 10th edition ", 'price' => 650);
$product[5] = array('name' => "Inderbir Singh Histology ", 'price' => 495);

    if(isset($_POST['button']))
{        
       $first = array();
   $second = array();
    foreach ($_POST['product'] as $pId)
     {
    $first[] = $product[$pId]['name'];
    $second[] = $product[$pId]['price']; 
     }
  $bookauthor = count($first);
  $bookprice = count($second);
  $max = ($bookauthor > $bookprice ? $bookauthor : $bookprice);
  echo '<br />';
  echo '<i style="font-color:#000000;font-weight:bold;text-decoration:underline;font-size:21px; padding-left:110px;margin-top:10px"> List of books you have selected:</i>';
  echo '<table>';
  echo '<tr>';
  echo '<th style="text-align: center">SL No.</th>';
  echo "<th>Book Name</th>";
  echo "<th>Amount in INR</th>";
      echo "<th>Action</th>";
  echo '</tr>';
   $count = 0;
   for ($i = 0; $i < $max; $i++)
    {
    $count++;
    echo '<tr>';
    echo "<td style='text-align: center'>{$count}</td>";
    echo "<td>{$first[$i]}</td>";
    echo "<td>{$second[$i]}</td>";
            echo "<td><a href='#'><i style='color:#F5F5F5;background:#D52020'>REMOVE</i> </a></td>";
    echo '</tr>';
    }
    $total =  array_sum($second);
    echo '<tr>';
    echo "<td colspan='2' style='font-weight:bold;font-size:14px'>Total Amount</td>";
    echo "<td style='font-weight:bold;font-size:14px;'>{$total}</td>";
    echo "</tr>";
    echo '</table>';
    }

&GT;

当我点击显示在同一行的REMOVE链接时,我想从php中删除特定行,   下面的脚本从表中删除但我想从php中删除

 <script>
$('i').click(function(){
$(this).parent('a').parent('td').parent('tr').remove();
});
</script>

1 个答案:

答案 0 :(得分:1)

试试这个:

您可以使用closest

$('i').click(function(){
$(this).closest('tr').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table border="1">
	<tr>
		<td>A1</td>
		<td><a href="#"><i>remove</i></a></td>
	</tr>
	<tr>
		<td>A2</td>
		<td><a href="#"><i>remove</i></a></td>
	</tr>
	<tr>
		<td>A3</td>
		<td><a href="#"><i>remove</i></a></td>
	</tr>
	<tr>
		<td>A4</td>
		<td><a href="#"><i>remove</i></a></td>
	</tr>
</table>