Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variable get's "lost" if obtained from mysql database.

    Hello guys,

    I need your help with a combined JavaScript-PHP code...

    Let me post the codes

    First, the PHP code:
    PHP Code:
    <div id="shop_product_table" style="float: left; margin-left: 0px; width: 205px;">
     
    ';
     $search_pm = mysql_query("SELECT * FROM ".$table_articles." WHERE pm='
    1'") or trigger_error (mysql_errno());
     $num_search_pm = mysql_num_rows($search_pm);
     if ($num_search_pm > 0) {
       $table_visibility = '
    visibilityvisible;';
     } else {
       $table_visibility = '
    visibilityhidden;';    
     }
     echo '
     
    <table id="Table_Pm" cellspacing="0" style="'.$table_visibility.'">
      
    ';
      if ($num_search_pm > 0) {
        echo '
    <tr style="height: 10px;" id="menu_left_title">
        <
    td width="80" align="left" style="font-size: 12px; padding-left: 5px;">Afisare</td>
        <
    td width="80" style="font-size: 12px;">Cod produs</td>
        <
    td width="40" align="center" style="font-size: 12px;">Sterge</td>
        </
    tr>';  
        while ($row_search_pm = mysql_fetch_array($search_pm)) {
          //print '
    in while<br />';    
          echo '
    <tr>';      
          print '
    <td width="90" align="left" style="padding-left: 5px;">Detalii</td>';
          print '
    <td width="100">'.$row_search_pm['code_article'].'</td>';
          print '
    <td width="20" align="center" style="padding-right: 5px;">';
          print '
    <img id="deleteImage" src="images/icon_trash.png" onclick="deleteTheRow(this); undressModel('.$row_search_pm['code_article'].');" />';
          echo '
    </td>';
          echo '
    </tr>';
        }
      }
      echo '
      
    <!-- START content from the javascript-->
      <!-- 
    END content from the javascript-->
     </
    table>
    </
    div
    Now the Javascript code

    Code:
    function deleteTheRow(src) {
         var oRow = src.parentElement.parentElement;   
         document.all("Table_Pm").deleteRow(oRow.rowIndex); 
    }
       
    function undressModel(u_idImg) {
         var ver_elm_fata = document.getElementById(''+ u_idImg +'_fata');
         if (null !== ver_elm_fata) {
           ver_elm_fata.parentNode.removeChild(ver_elm_fata); 
         }
    	 
    	 var ver_elm_spate = document.getElementById(''+ u_idImg +'_spate');
    	 if (null !== ver_elm_spate) {
           ver_elm_spate.parentNode.removeChild(ver_elm_spate); 
         }
    	 
    	 $.get('phpinclude/undressModel.php', {'code_articole': ''+ u_idImg +''}, function(d) {
              alert(''+ d +'');
    	 });
    	 
    	 undress_model_fata.removeAttribute ('id', ''+ u_idImg +'_fata');
    
    	 undress_model_spate.removeAttribute ('id', ''+ u_idImg +'_spate');
    	 
    }
    The problem is that if I call this javascript function from another javascript by adding the following:

    cell3_elm.setAttribute('onclick','stergeRow(this); undressModel('+ d_idImg +');'); // for IE/Firefox
    cell3_elm.onclick = function() {stergeRow(this); undressModel(d_idImg);}; // for Chrome/Opera/Safari
    cell3.appendChild(cell3_elm);

    the function works like clockwork...

    But when I add the function to a static table with values pulled from a mysql_database, the function won't "see" the variable (in this case u_idImg)
    Basically it will "lose it".

    Any idea's on how this can be fixed ?

    I'll appreciate any help and thank everyone in advance!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,720
    Thanks
    80
    Thanked 4,514 Times in 4,478 Posts
    Some of that code just makes no sense at all.

    Code:
        document.all("Table_Pm").deleteRow(oRow.rowIndex);
    document.all is *ONLY* legal in MSIE. It will never work in any other browser.

    Code:
    cell3_elm.setAttribute('onclick','stergeRow(this); undressModel('+ d_idImg +');'); // for IE/Firefox
    That's not legal code! In any browser that I know of. Oh, it will set the onclick attribute, but then clicking on the element won't do anything, except maybe cause an error.

    But what's funny is that this code:
    Code:
    cell3_elm.onclick = function() {stergeRow(this); undressModel(d_idImg);};
    should work just fine in any browser. On top of which, since it comes right after the setAttribute line, it *OVERWRITES* what the setAttribute just did, so now the onclick works in all browsers.

    But in any case, *DO NOT SHOW US PHP CODE*. We don't have your database, so we have no idea what values you are stuffing into the HTML from PHP.

    Instead, bring up your page in your browser, then click on the VIEW menu, then click on the SOURCE or PAGE SOURCE menu item. Now you will be looking at the HTML *as the browser sees it*. Copy/paste the relevant part of *THAT* code here.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •