Hi Friend.

i want to add a tr in an table using php only.

please help me how to create it.

i dont want jquery or javascript.

Recommended Answers

All 3 Replies

Member Avatar for diafol

PHP must add the row before being sent to the client. So it can't be added interactively - unless you use AJAX, which will involve javascript/jQuery.

If you show your code, we'll have a better idea of what you want to do.

thanks diafol.

I tried using the below code. but i am unable to get the second drop down value.

index.php

`

<!DOCTYPE html>
<?php include('../conn.php'); 
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
?>
<html>
<head>
<title>jquery add remove table row dynamically with checkbox functionality</title>
<style>
@font-face{font-family: Lobster;src: url('Lobster.otf');}
h1{font-family: Lobster;text-align:center;}
table{border-collapse:collapse;border-radius:25px;width:880px;}
table, td, th{border:1px solid #00BB64;}
tr,input{height:30px;border:1px solid #fff;}
input{text-align:center;}
input:focus{border:1px solid yellow;} 
.space{margin-bottom: 2px;}
#container{margin-left:210px;}
.but{width:270px;background:#00BB64;border:1px solid #00BB64;height:40px;border-radius:3px;color:white;margin-top:10px;margin:0px 0px 0px 290px;}
</style>
<script src='jquery-1.9.1.min.js'></script>
</head>
<body>
<h1>Student Marks</h1>
<div id='container'>
<form id='students' method='post' name='students' action='index.php'>

<table border="1" cellspacing="0">
  <tr>
    <th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
    <th>S. No</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Tamil</th>
    <th>English</th>
    <th>Computer</th>
    <th>Conn Type</th>
    <th>Type</th>
  </tr>
  <tr>
    <td><input type='checkbox' class='case'/></td>
    <td>1.</td>
    <td><input type='text' id='first_name' name='first_name[]'/></td>
    <td><input type='text' id='last_name' name='last_name[]'/></td>
    <td><input type='text' id='tamil' name='tamil[]'/></td>
    <td><input type='text' id='english' name='english[]'/> </td>
    <td><input type='text' id='computer' name='computer[]'/></td>
    <td><select id='c_type'  onChange='showptype1(this.value)' ><option value=''>Select Ctype...</option><?php $query = "SELECT distinct SCYQ64CTYPE FROM $database.FQ64006"; $result = odbc_exec($connection,$query); while($row = odbc_fetch_array($result))   {   ?><option value="<?php echo trim($row['SCYQ64CTYPE']); ?>"><?php echo trim($row['SCYQ64CTYPE']); ?></option><?php } ?></select> </td>
    <td><select id='type1'><option value="">Select type...</option><?php $tquery = "SELECT distinct SCYQ64TYPE FROM $database.FQ64006 WHERE SCYQ64CTYPE = '$q'"; $result = odbc_exec($connection,$tquery); while($trow = odbc_fetch_array($tresult)) {   ?><option value="<?php echo trim($trow['SCYQ64TYPE']); ?>"><?php echo trim($trow['SCYQ64TYPE']); ?></option><?php } ?></select></td>
  </tr>
</table>

<button type="button" class='delete'>- Delete</button>
<button type="button" class='addmore'>+ Add More</button>
<p>
<input type='submit' name='submit' value='submit' class='but'/></p>
</form>
</div>

<script>
<?php
$query = "SELECT distinct SCYQ64CTYPE FROM $database.FQ64006"; $result = odbc_exec($connection,$query); 
$tquery = "SELECT distinct SCYQ64TYPE FROM $database.FQ64006 WHERE SCYQ64CTYPE = '$q'"; $result = odbc_exec($connection,$tquery);
?>
$(".delete").on('click', function() {
    $('.case:checkbox:checked').parents("tr").remove();

});
var i=2;
$(".addmore").on('click',function(){
    var data="<tr><td><input type='checkbox' class='case'/></td><td>"+i+".</td>";
    data +="<td><input type='text' id='first_name"+i+"' name='first_name[]'/></td> <td><input type='text' id='last_name"+i+"' name='last_name[]'/></td><td><input type='text' id='tamil"+i+"' name='tamil[]'/></td><td><input type='text' id='english"+i+"' name='english[]'/></td><td><input type='text' id='computer"+i+"' name='computer[]'/></td><td><select id='c_type"+i+"'  onChange='showptype"+i+"(this.value)' ><option value=''>Select Ctype...</option><?php while($row = odbc_fetch_array($result)){?><option value='<?php echo trim($row['SCYQ64CTYPE']); ?>'><?php echo trim($row['SCYQ64CTYPE']); ?></option><?php } ?></select></td><td><select id='type"+i+"' onChange='AddToStack"+i+"();'><option value=''>Select type...</option><?php while($trow = odbc_fetch_array($tresult))   {   ?><option value='<?php echo trim($trow['SCYQ64TYPE']); ?>'><?php echo trim($trow['SCYQ64TYPE']); ?></option><?php } ?></select></td></tr>";
    $('table').append(data);

    function showptype"+i+"(str1)
    {
        if (str1=='')
          {
          document.getElementById('type"+i+"').innerHTML='';
          return;
          }
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById('type"+i+"').innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open('GET','getptype.php?rid='+i+'&q='+str1,true);
        xmlhttp.send();
    }
    i++;
});

function select_all() {
    $('input[class=case]:checkbox').each(function(){ 
        if($('input[class=check_all]:checkbox:checked').length == 0){ 
            $(this).prop("checked", false); 
        } else {
            $(this).prop("checked", true); 
        } 
    });
}

function showptype1(str1)
    {
        var rid=1;
        console.log(rid);
        if (str1=='')
          {
          document.getElementById('type1').innerHTML='';
          return;
          }
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById('type1').innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open('GET','getptype.php?rid='+rid+'&q='+str1,true);
        xmlhttp.send();
    }

</script>

<?php
if(isset($_POST['submit'])){
print_r($_POST);
}
?>

</body>
</html>

getptype.php

<?php
    session_start();
    include('conn.php');

    $q=$_GET['q'];
    echo $rid=$_GET['rid'];
    $tquery="select * from $database.FQ64006 WHERE SCYQ64CTYPE='$q'";
    $tresult = odbc_exec($connection,$tquery);

    if($q!=='')
        {
?>
    <select name="type[]"  onChange='AddToStack<?php echo $rid; ?>();' id="type<?php echo $rid; ?>"><option value=''>Select type</option><?php while($trow=odbc_fetch_array($tresult)) { ?>
<option value="<?php echo trim($trow["SCYQ64TYPE"]); ?>"><?php echo trim($trow["SCYQ64TYPE"]); } ?><option>
</select> 
<?php } ?>
Member Avatar for diafol

How do you even maintain this Pal? It's pretty grim - so no offence - but markup, css, js, php, sql all mixed to hell. Separarte out your concerns / languages.

If you have php in a page, place it above the !doctype declaration (or in include files).

Just have trivial php in the page, like echo/conditionals/loops

Leave all styling to a css file - don't use style tags if you can avoid it - even table attributes - border and cellspacing.

THe js is inline and a mix of native and jQuery.

This is pretty painful to try and dissect. Here's an example of how you could clean it up:

<?php 
    ini_set ('display_errors', 1);
    error_reporting (E_ALL & ~E_NOTICE);
    include('../conn.php'); 

?>
<!DOCTYPE html>
<html>
<head>
    <title>jquery add remove table row dynamically with checkbox functionality</title>
    <link href="yourcssfile.css" rel="stylesheet">
</head>

<body>
    <h1>Student Marks</h1>
    <div id='container'>
        <form id='students' method='post' name='students' action='index.php'>
            <table border="1" cellspacing="0">
                <tr>
                    <th>
                        <input class='check_all' type='checkbox' />
                    </th>
                    <th>
                        S. No
                    </th>
                    <th>
                        First Name
                    </th>
                    <th>
                        Last Name
                    </th>
                    <th>
                        Tamil
                    </th>
                    <th>
                        English
                    </th>
                    <th>
                        Computer
                    </th>
                    <th>
                        Conn Type
                    </th>
                    <th>
                        Type
                    </th>
                </tr>
                <tr>
                    <td>
                        <input type='checkbox' class='case'/>
                    </td>
                    <td>
                        1.
                    </td>
                    <td>
                        <input type='text' id='first_name' name='first_name[]'/>
                    </td>
                    <td>
                        <input type='text' id='last_name' name='last_name[]'/>
                    </td>
                    <td>
                        <input type='text' id='tamil' name='tamil[]'/>
                    </td>
                    <td>
                        <input type='text' id='english' name='english[]'/>
                    </td>
                    <td>
                        <input type='text' id='computer' name='computer[]'/>
                    </td>
                    <td>
                        <select id='c_type' name='c_type'>
                            <option value=''>Select Ctype...</option>
                            <?php foreach($array1 as $item) ?>
                            <option value="<?= $item['SCYQ64CTYPE']; ?>">
                                <?= $item['SCYQ64CTYPE']; ?>
                            </option>
                        </select>
                    </td>
                    <td>
                        <select id='type1' name='type1' >
                            <option value="">Select type...</option>
                            <?php foreach($array2 as $item) ?>
                            <option value="<?= $item['SCYQ64TYPE']; ?>">
                                <?= $item['SCYQ64TYPE']; ?>
                            </option>
                         </select>
                     </td>
                </tr>
            </table>
            <button type="button" class='delete'>- Delete</button>
            <button type="button" class='addmore'>+ Add More</button>
            <p>
                <input type='submit' name='submit' value='submit' class='but'/>
            </p>
        </form>
    </div>

    <script src='jquery-1.9.1.min.js'></script>
    <script src="myjsfile.js"></script>
</body>
</html>

The <th>, <td> and <p> newline/indent for content may be overkill, but at least it's clear. I may have a look at the issue if I have time, but suffice to say, you'll get far more contributions to your issues if you code cleanly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.