1

I am running this code in a PHP file

<?php
if(empty($item)){
    echo "Your cart is empty";
 }
 else{
    echo '<table id="cart_table"><tr><th>Item</th><th>Price</th><th></th></tr>' ;
         for($i = 0;$i< count($item);$i++){
             if(!empty($item[$i])){    
                 $numRow++; 
                 echo '<tr><td>' . "<img src = 'images/$photo[$i].jpg'>" .'</td>';
                 echo '<td>' .'$' . $item[$i] . '</td>'; 
                 echo '<td>' . '<button id= "btn"   onclick = "function(){
                    <script type="text/javascript">
                    document.getElementById("cart_table").deleteRow('. $numRow .');
                 }"> Remove</button>'.'</tr>' ;   
                }        
         }
         echo '</table>';
         }

 ?>

Please consider these lines of code only from the above snippet:

echo '<td>' . '<button id= "btn"   onclick = "function(){
       <script type="text/javascript">
       document.getElementById("cart_table").deleteRow('. $numRow .');
       }"> Remove</button>'.'</tr>' ;   
     } 

The result of this code is:

enter image description here

The result I am trying to achieve is:

enter image description here

Is it possible to achieve this result? The result is going to be a button with the label "Remove" and with the onClick property that calls a INLINE(I know it's bad practice, but I have a good reason for it) function which executes some lines of code.

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
Fabrizio Botalla
  • 697
  • 1
  • 8
  • 25
  • 2
    This is not even a PHP problem, you are just creating HTML that makes no sense. What business does ` – 04FS Jan 17 '20 at 07:47
  • 1
    @mplungjan Thank you for the help, unfortunately that will not execute any JavaScript command though. – Fabrizio Botalla Jan 17 '20 at 07:54
  • Even if you fix the markup, the logic is flawed. The index of the rows are going to shift after deleting one, so it is merely going to work correctly the first time. – Lain Jan 17 '20 at 07:55
  • 1
    I know that, my question is not regarding the logic. That will get changed. I am just asking if it is possible to have it all inline. Thanks though – Fabrizio Botalla Jan 17 '20 at 07:59
  • Of course it is possible. With what mplungjan suggested above, the quotes are still wrong though - since `"` are used as delimiters of the onclick attribute value, they can not be used around the parameter for getElementById again - otherwise the attribute value _ends there_. https://stackoverflow.com/questions/24320739/ explains how to fix that; and https://www.php.net/manual/en/language.basic-syntax.phpmode.php explains how to output larger chunks of HTML code with only small dynamic parts in a better way to begin with, so you will have less trouble dealing with quotes in the first place. – 04FS Jan 17 '20 at 08:21
  • You should take a look at https://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – Amanjot Kaur Jan 17 '20 at 08:22
  • I appreciated the help, thanks, Ill take a look at the references. – Fabrizio Botalla Jan 17 '20 at 08:28
  • Sounds like an X/Y problem. Why are you not allowed to add an unobtrusive script and have the button delete its parent row? – mplungjan Jan 17 '20 at 08:31
  • @mplungjan I was referring to `document.getElementById("cart_table")`. – 04FS Jan 17 '20 at 08:34
  • Ah, Oops. `echo ''` or `"this.closest(\'tr\').remove()"` and even better to add a delegated click event handler – mplungjan Jan 17 '20 at 08:48
  • 1
    `onclick = "this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode)"` – Lain Jan 17 '20 at 12:57

1 Answers1

1

Try not to mix up HTML and PHP together, if there is a need to mix HTML and PHP, try in proper formatting.

<?php
    if(empty($item)):
?>
         <span>Your cart is empty</span>
<?php
    else:
?>
        <table id="cart_table">
            <tr>
                <th>Item</th>
                <th>Price</th>
                <th></th>
            </tr>
            <?php
                for($i = 0;$i< count($item);$i++):
                    if(!empty($item[$i])):
                        $numRow++; 
            ?>
                        <tr>
                            <td><img src ='images/<?php echo $photo[$i].jpg; ?>'/></td>
                            <td>$<?php echo $item[$i]; ?></td> 
                            <td><button id= "btn" onclick = "deleteFromCart('<?php echo $numRow; ?>')"> Remove</button></td>
                        </tr>
            <?php
                    endif;
                endfor;
            ?>   
        </table>
<?php
    endif;
?>
 <script>
    function deleteFromCart(rowToDelete){
        document.getElementById("cart_table").deleteRow(rowToDelete);
    }
 </script>
Not A Bot
  • 2,474
  • 2
  • 16
  • 33