0

I want to be able to select a favorite product from a shopping cart. This is my code (it is from IBKCart modified):

<?php
    function ShowCartWtPrd(){
        global $fav_id;
        $itemClass = ($this->getCartItems() > 2) ? "insideContMainArea2": "insideContMainArea";
        $plural = ($this->getCartItems() > 1)? 's': '' ;


        if (isset($_SESSION['favourites']) && !$_SESSION['favourites'] == "False") {

            $this->updFavourites();
            unset($_SESSION['favourites']);
        }                        

        $Out = 
        '<div id="theOverlay">&nbsp; 
          <table width="100%" border="0" cellspacing="0" cellpadding="2">
            <tr>
              <td width="20%"><div align="right"><img name="" src="./imgs/wait.gif" width="32" height="32" alt="" /></div></td>
              <td width="50%"><div align="left">&nbsp;<strong>Updating...Please Wait</strong> </div></td>
            </tr>
          </table>
        </div>
        <div id="Cart_Container">
            <div id="Car_top_bg"></div>
          <div id="Cart_middle">
                 <div id="'. $itemClass. '">
                   <table width="100%" border="0" cellspacing="2" cellpadding="2">
                     <tr bgcolor="#E6EDD2">
                       <td width="55%" height="21" bgcolor="#E6EDD2"><div align="center"><strong>Item Description </strong>
                       </div>
                       </td>
                       <td width="11%"><div align="center"><strong>Qty</strong></div></td>
                       <td width="11%"><div align="center"><strong>Prc/Qty</strong></div></td>
                       <td width="11%"><div align="center"><strong>Fav</strong></div></td>
                       <td width="11%"><div align="center"><strong>Del</strong></div></td>
                     </tr>' ;
                     $cnt=1;
                     foreach($this->cart as $itemId=>$Qty){
                        ++$cnt;
                        $ans = fmod($cnt,2);
                        if($ans == 0){$bg = '#ECF9FF';}else{$bg='';};
                        $ProdDts = $this->getProdDts($itemId);

                        $this->totAmt += $ProdDts[$this->prodPrc] * $Qty ;
                        $fav_id = $ProdDts[$this->prod_id];

                            $Out .= '<tr bgcolor="'. $bg .'">
                            <td valign="top">
                                <table width="100%" border="0" cellspacing="1" cellpadding="0">
                                 <tr>
                                    <td width="60%" valign="top">
                                        ' . $ProdDts[$this->prodNm]  .' 
                                        <br />                   
                                    </td>
                                 </tr>
                                </table>
                               </td>
                               <td valign="top"><div align="center">' . $Qty .'</div></td>
                               <td valign="top">' . $ProdDts[$this->prodPrc]  .'</td>
                               <td valign="top">
                                    <div align="center">
                                        <a href="#?favourites=true">
                                            <img src="./imgs/fav_trnsp_icon.png" 
                                                alt="Add to favourites" width="16" height="16" border="0" 
                                                title="Add to favourites" name="favourites"
                                                onclick="document.write('. $this->updFavourites() .');"
                                            />                                                                         
                                        </a>
                                    </div>
                                </td>
                               <td valign="top">
                                    <div align="center">
                                        <a href="#" 
                                            onclick="doCart(\'DelItem\', \'' . $ProdDts[$this->prodId] . '\', 0, \' \', \'Small\');">
                                            <img src="./imgs/cart_remove.png" alt="Delete Item" width="16" height="16" border="0" title="Delete Item"/></a>
                                    </div>
                                                               </td>
                             </tr>
                             <tr>
                       <td colspan="4" valign="top"><div class="clear"></div></td>
                     </tr>';
                     }

                    $Out .= '
                   </table> 
                 </div>
          </div>
            <div id="Cart_gtotal">
                <div class="theCont">Grand Total => '. $this->ShowCurSymbol() .' '. $this->toMoney($this->totAmt) . '</div>
            </div>
        </div>' ;   

        echo $Out;
    }
            function updFavourites() {
                global $username;
                global $password;
                global $database;
                global $fav_id;
                $conn = mysqli_connect("localhost",$username,$password, $database);
                // Check connection
                if (mysqli_connect_errno($conn))
                  {
                  echo "Failed to connect to MySQL: " . mysqli_connect_error();
                  }
                // mysql_connect("localhost","$username","$password") or die("Error: ".mysqlerror());
                // mysql_select_db("$database");
                if (isset($_SESSION['fav_id'])) {
                    $fav_id = $_SESSION['fav_id'];
                }
                $dealer_id  = $_SESSION['dealer_id'];
                $ProdDts = $this->getProdDts($fav_id);

                $part_id        = $ProdDts[$this->prod_id];
                $sql = "INSERT INTO favourites VALUES (0,'$dealer_id','$part_id')";
                $result = $conn->query($sql) or exit("Error code ({$conn->errno}): {$conn->error}");

                /* close up */
                //$conn->close();
            }
 ?>

            }

If I step through the code with a debugger, I get as far as the line

$Out .= '<tr bgcolor="'. $bg .'">

and it goes straight into the updFavourites() function, Which I wanted to do on the onclick event "favourites" near the end of the ShowCartWtPrd function. I have been trying to debug this but can't see what is causing it. Perhaps someone has an idea.

3
  • Post the rendered HTML. Commented Apr 3, 2014 at 20:17
  • You have not understood the difference between server-side PHP code and client-side JavaScript code yet … of course updFavourites gets executed there and then, because it is a PHP function. PHP is long done with its work, before the page even reaches the client. Commented Apr 3, 2014 at 20:24
  • as I said to Bikonja, the updFavourites is dependent on the session variable favourites which only gets set by the onclick evernt, so I can't see how that happens. Commented Apr 3, 2014 at 23:15

1 Answer 1

1

You are confusing server-side and client side events. The onclick event you are using is a javascript (client-side) event and will be executed when the user clicks on the element, but since you are outputting the string $Out from PHP (server-side) and you are concatenating the values of the string constants and the updFavourites function, PHP executes the server-side code and then gives the client-side code which is the evaluated PHP function.

Sign up to request clarification or add additional context in comments.

4 Comments

I certainly get what you are saying, but why does the function updFavourites()get called. There is only one call to it in the code and that seems to be dependent on the onclick event. Is that explicable?
@Geoff It shouldn't be dependent on the onclick event. The PHP variable to which you concatenate the updFavourites function is always called within an iteration of the foreach loop. Are you maybe not entering the foreach? PHP doesn't event know what onclick is so it's not dependant on that. Try seeing if the foreach is being called when you think the onclick is not "triggered".
I got the code for this onclick event from this question: link. ** The onClick attribute of html tags only takes Javascript but not PHP code. However, you can easily call a PHP function from within the Javascript code by using by using the JS document.write() function - effectively calls the PHP function by "writing" its call to the browser window**
That answer is badly written. Using document.write('<?php // PHP code ?>'); is just so you could add evaluated PHP code to your page onclick, so while it does defer writing to your HTML page to the onclick event, it does not defer the PHP evaulation of the code that it will put in the document.write part. The only way to call a PHP function from a javascript onclick is by using AJAX.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.