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.

11
  • 2
    This is not even a PHP problem, you are just creating HTML that makes no sense. What business does <script type="text/javascript"> haven being inside an onclick attribute? Absolutely zero. Commented Jan 17, 2020 at 7:47
  • 1
    @mplungjan Thank you for the help, unfortunately that will not execute any JavaScript command though. Commented Jan 17, 2020 at 7: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. Commented Jan 17, 2020 at 7: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 Commented Jan 17, 2020 at 7:59
  • 1
    onclick = "this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode)" Commented Jan 17, 2020 at 12:57

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the suggestion! Like I asked in my question I need the suggestions to be inline, thank you though!
Still missing the </td> here too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.