0

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>
6
  • 1
    This code is a huge mess so its going to be hard to help you, however are you posting any value with the name $chkrnk? PHP does not evaluate variables inside ' encapsulated strings. Commented Mar 18, 2013 at 11:49
  • Why are you not able to do it? What is causing the problem? Errors? Commented Mar 18, 2013 at 11:50
  • @AneeshDogra i am trying to get the value of checkbox, label, and select box but its id/name are generating dynamically so i am not able to get exact variable id/name. to save in database Commented Mar 18, 2013 at 11:56
  • Did you read @cernunnos's comment? Commented Mar 18, 2013 at 11:58
  • @cernunnos i tried it outside ' encapsulated strings but it not getting exact value of tag. Commented Mar 18, 2013 at 12:00

1 Answer 1

1

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.

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

7 Comments

i tried this but when i used a[$a] name for checkbox and b[$] name for select it showing blank array.
Are they disabled when you submit the form?
No @cernunnos when i click on checkbox then drop down get enable and then after i am trying to save value of checkbox and drop down.
Inspect your generated HTML, check if it valid HTML and look at the names of the inputs (names like a[0] have to be unique, names like a[] dont). Also put a var_dump($_POST) to check what is being posted by the form.
if i use <select name="bleh[$a]">...</select> like this then it not show any incremental value it show name="bleh[]" only and if i use it <select name="bleh[<?php echo $a; ?>]">...</select> like this it show incremental value i.e. name="bleh[1]"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.