dongmin4052 2013-03-18 11:44
浏览 70

如何使用php获取(调用)动态生成的变量值以保存在(mysql)数据库中

i am trying to save value of my dynamically generated checkbox, label and select tag in to mysql database using php in this tags i am retrieving value from different database and i am trying to save data of checked field with label value and selected option value, but i am not able to do it, please suggest me where i am doing wrong.

<script language="javascript">
                function getmatchplayer1(mid)
                {
                    window.location="?mid="+mid;
                }

                </script>
        </head>
        <?php

        $host="localhost";                          // Host name 
    $username="root";                       // Mysql username 
    $password="";                           // Mysql password 
    $db_name="test";                    // Database name 

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 

    // select database on server
    mysql_select_db("$db_name")or die("cannot select DB");


        if(isset($_POST['SaveTeam'])){
    $tmnmA = $_POST['TeamNameA'];
    $tmnmB = $_POST['TeamNameB'];
    $mch_id = $_POST['mtch_id'];
    $chk_id = $_POST['$chkid'];
    $cric_nm = $_POST['$ply_nm'];
    $chk_rnk = $_POST['$chkrnk'];


    $sqlbat = "INSERT INTO board(cricketer_id, status,Runs,Sixes,Fifty,Hundred,Total_Score,Total_Points)VALUES('$cric_nm', 'NP','0','0','0','0','0','0')";

    $retval = mysql_query( $sqlbat );
    if(! $retval )
    {
      die('Could not update data: ' . mysql_error());
    }
    echo "Updated data successfully
";

    }
            $sql=mysql_query( "SELECT * FROM tbl_info LIMIT 5"); 
            $cric_id = $resultmp['player_id'];
         ?>
    <body style="align:center">
    <form method="post" id="form1" name="form1" action="">
            <div style="width:850px; margin-left:55px; text-align:right">
                <input type="submit" id="SaveTeam" name="SaveTeam" value="     Save     " />
            </div>
    <table align="center" width="900" height="auto" style="border-top:1px solid #CCCCCC;border-right:1px solid #CCCCCC; border-left:1px solid #CCCCCC; cellspacing:0px; cellpadding:0px;">
    <tr style="border:1px solid #CCCCCC">
      <td width="450" style="border:1px solid #CCCCCC"> Today's Match:-&nbsp;&nbsp;&nbsp;
        <select name="mtch_id" id="mtch_id" onchange="getmatchplayer1(this.value)">
          <option> Select Team </option>
          <?php
                    $i=1;
                    $sqlm=mysql_query("SELECT * FROM t_match LIMIT 5");
                    while ($resultm=mysql_fetch_array($sqlm))
                    {
                        $mtch=$resultm['match_no'];
                        echo "<option id='".$i."' value='".$mtch."'>";
                            echo $mtch;
                        echo "</option>";
                    $i++;}
                ?>
        </select></td>
      <td width="450" style="border:1px solid #CCCCCC">
                    <?php 

                                    $j=1;
                                    if (isset($_REQUEST['mid']))
                                    {
                                        $mid=$_REQUEST['mid'];
                                    }
                                    $sqlm1=mysql_query("SELECT * FROM t_match WHERE match_no = '$mid'");
                                    while ($resultm1=mysql_fetch_array($sqlm1)){
                                        $mtch=$resultm1['match_no'];
                                        $mtch1=$resultm1['team1'];
                                        $mtch2=$resultm1['team2'];
                                        $chkid = 'a'.$a;
                        ?>
              </td>
    </tr>
    <tr style="border:1px solid #CCCCCC">
      <td style="text-align:center; border:1px solid #CCCCCC">
        <?php 
            echo "Team - ".$mtch1; 
        ?>
      </td>
      <td style="text-align:center; border:1px solid #CCCCCC">
        <?php 
            echo "Team - ".$mtch2; 
            }
        ?>
      </td>
    </tr>

    <tr>
      <td height="auto" colspan="2">
        <div id="update" style="width:900px; height:24px;">
        <div style="float:left; width:450px; height:24px;">
        <?php
                    $a=1; 
                    $b=2;
                    $k=1;
                    $sqlmp=mysql_query("SELECT * FROM tbl_info WHERE player_team = '$mtch1' LIMIT 5");
                    while ($resultmp=mysql_fetch_array($sqlmp)){ 

                    echo "<SCRIPT LANGUAGE='JavaScript'>

    //on checked disabled enabled condition

    function codenameA".$a."()
    {
        if (document.form1.a".$a.".checked)
        {
            document.form1.b".$a.".disabled = false;
        }
        else
        {
            document.form1.b".$a.".disabled = true;
        }
    }

    /*Condition for limit the checked up to 11 for team A*/

    //initial checkCount of zero
    var checkCount = 0

    //maximum number of allowed checked boxes
    var maxChecks = 11

        function setChecksA".$a."(obj)
        {
            //increment/decrement checkCount
            if (obj.checked)
            {
                checkCount=checkCount+1
            }
            else
            {
                checkCount=checkCount-1
            }
            //if they checked a 11th box, uncheck the box, then decrement checkcount and pop alert
            if (checkCount>maxChecks)
            {
                obj.checked=false
                document.form1.b".$a.".disabled=true;
                checkCount=checkCount-1
                alert('you may only choose up to '+maxChecks+' options')
            }
        }
        //-->
        </script>";

        ?>

                <div style="float:left; width:80%; height:24px; border:1px solid #CCCCCC">
                    <input onclick='codenameA<?php echo $a; ?>(), setChecksA<?php echo $a; ?>(this)' type='checkbox' id='a<?php echo $a; ?>' name='a<?php echo $a; ?>' value='<?php echo $resulttmp['player_id']; ?>' /><input type="hidden" id="lt<?php $a; ?>" name="lt<?php $a; ?>" value="<?php echo $resulttmp['player_name']; ?>" />&nbsp;&nbsp;&nbsp;<?php echo $resultmp ['player_name']; ?>
                </div>
                <div style="float:right; width:19%; height:24px; border:1px solid #CCCCCC">
                    <select disabled='disabled' id='b<?php echo $a; ?>' name='b<?php echo $a; ?>' style='width:100%;'>
            <option>
                - -
            </option>
            <option value='1'>
                1
            </option>
            <option value ='2'>
                2
            </option>
            <option value='3'>
                3
            </option>
            <option value='4'>
                4
            </option>
            <option value='5'>
                5
            </option>
            <option value='6'>
                6
            </option>
            <option value='7'>
                7
            </option>
            <option value='8'>
                8
            </option>
            <option value='9'>
                9
            </option>
            <option value='10'>
                10
            </option>
            <option value='11'>
                11
            </option>
          </select> 
                </div>
      <?php          
        $a++; 
        $b++;

    }
    ?>
    </div>
      </td>
    </tr>
    </table>
    </form>
  • 写回答

1条回答 默认 最新

  • dpkajqd31574096 2013-03-18 12:14
    关注

    If i understood correctly, you are generating the names of the inputs dinamically, using a $a increment variable. But for some reason you cant recalculate those names when you post so you are not able to get the posted values. You can resolve this in many ways, one is to generate input names this way:

    <input type="checkbox" name="blah[$a]"/>
    <select name="bleh[$a]">...</select>
    

    This will result in a post with an array like so:

    array( 
        "blah" => array($a => valueofcheckbox),
        "bleh" => array($a => valueofselect)
    )
    

    That you can parse with a simple foreach

    foreach ($_POST["blah"] as $a => $value) {
        ...
    }
    
    foreach ($_POST["bleh"] as $a => $value) {
        ...
    }
    

    I dont quite understand the enable/disable logic, as i said before, thats some messy code, but you should know that disabled fields will not get posted.

    PS: The code i posted is pseudo-code.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据