dynamic submit action help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EVAS
    New Member
    • Nov 2007
    • 8

    dynamic submit action help

    hello!i will try to explain my problem here!forgive me from mu english
    i want to create buttons depented on some field in mysql.i managed to pass the variable value (how many buttons to make) with php and javascript.Now i want for each button to show some data from mysql(i guess i should call a fun with button parameter to do this) my problem is how to make each button act deferently?i try this
    Code:
    function addsubmit (fid)
    { for (i=0;i<num;i++){ //num is how many buttons to create
    
    var newsubmit = document.createElement("input");
    newsubmit.type = "submit";
    newsubmit.id = "mysubmit"&+submitCount;
    newsubmit.name = "mysubmit";
    newsubmit.value =i;
    fid.appendChild(newsubmit);
    }
    }
    and i call in body <body onLoad="return addsubmit(myfor m1)";>
    how can i make each button to call a function with maybe some parameter to show my results ? i also tried to modify above function
    Code:
    function addsubmit (fid)
    { for (i=0;i<num;i++){ //num is how many buttons to create
    
    var newsubmit = document.createElement("input");
    newsubmit.type = "submit";
    newsubmit.id = "mysubmit"&+submitCount;
    newsubmit.name = "mysubmit";
    newsubmit.value =i;
    [B]newsubmit.onclick =fun(i);[/B]
    fid.appendChild(newsubmit);
    }
    }
    but with this when page is loaded it call tha function automatically without pressing the buttons.Sorry for the long length article but i try to explain my problem! tnx ina advance
    Last edited by Dormilich; May 23 '11, 06:44 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the onclick event handler (like any event handler) expects a function reference to be given, not a function return value.

    Code:
    function myfunc(evt)
    {
        // function code
        // the event object is accessible through evt
    }
    
    elem.onclick = myfunc;
    
    // with parameter 
    function myfunc(param)
    {
        // function code
    }
    
    elem.onclick = function (evt) { myfunc("some_value"); };
    
    // with a Closure to pass variables
    elem.onclick = function (p) {
        return function() { myfunc(p); };
    }(param);

    Comment

    • EVAS
      New Member
      • Nov 2007
      • 8

      #3
      thnx for your reply! i tried
      Code:
      newsubmit.onclick = function () {
                                         fun(i);
                                         };
      and worked like charm :)
      now i have something else that bugs me.i want each button to be named by a mysql table value.So a append my code
      Code:
      <?php
      $connect=mysql_connect("localhost","admin","pass")
      or die ("Check your server connection");
      $db_found = mysql_select_db("mydb", $connect);
      $SQL = "SELECT * FROM mytable ";
      $result = mysql_query($SQL);
      $num_r = mysql_num_rows($result);
      while($nt=mysql_fetch_array($result)){
           $test[$num_r]=$nt['testing'];
      }
      By here i have all the values of mysql table saved in an array $test.Now look at the script
      Code:
      ?>
      <script language="javascript" type="text/javascript">
      function addsubmit (fid){
         var num = "<?php echo $num_r; ?>";
          if (num >0) { 
                for (i=0;i<num;i++){
                 var newsubmit=document.createElement("input");
                     <?php $n=$num_r--;?>
                     data="<?php echo $test[$n]; ?>";
                     newsubmit.type = "button";
                     newsubmit.id = "mysubmit";
                     newsubmit.name = "mysubmit";
                     newsubmit.value =data;
                     newsubmit.onclick = function () {
                                         fun(i);
                                         }; 
                     fid.appendChild(newsubmit);
                }
          }
      }
      but with no result!if i try for test
      Code:
      data="<?php echo $test[2]; ?>";
      ,it doesnt work again,if i try
      Code:
      data="<?php echo $test; ?>";
      all the buttons are named Array.any ideas how will i make it work as i want ??

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        and worked like charm :)
        I doubt that.

        but with no result!
        obviously, PHP is executed on the server and JavaSript on the client.

        Comment

        Working...