简体   繁体   中英

how to get(call) dynamically generated variable value to save in (mysql) database using php

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\n";

    }
            $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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM