Najmal Jabbar
Najmal Jabbar

Reputation: 335

Add a value to a table from text field

In a form there is a text field and a table. If we put a value in that text field the corresponding name needs to be displayed on the table:

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<center><table>
<tr><td>no</td><td><input type="text" name="no"  autofocus></td></tr>
</table></center>
<br>
<br>
<center>
<table cellpadding="0" cellspacing="0" width="60%" border='1' bgcolor="#999999">
  <thead>
    <tr>
      <th> Element_Number</th>

    </tr>
  </thead>

<?php
if(isset($_POST['no']))
{
    $c=mysql_connect("localhost","root","");
    mysql_select_db("test");
    if(!$c)
    {
        echo "db prob".mysql_error();
    }

    $no=$_POST['no'];
    $qry=mysql_query("select name from opt where no='$no'");
    if(!$qry)
    {
        echo "ret".mysql_error();
    }


        if(mysql_num_rows($qry)!=0)
        {
            $row=mysql_fetch_array($qry);
            $name=$row['name'];
        }
        else
        {
            echo "query Invalid";
        }
    echo    "<td>" .$name."</td></tr>" ;

}
?>

Using this code the value enters in the table but I want to add the name frequently the value to the table as next row when the next value enter to the text field

Upvotes: 0

Views: 84

Answers (1)

ɹɐqʞɐ zoɹǝɟ
ɹɐqʞɐ zoɹǝɟ

Reputation: 4370

do it with the help of ajax

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<center>
<table>
<tr><td>no</td><td><input type="text" name="no"  autofocus></td></tr>
<tr><input type="button" name="submit" value="submit"></tr>
</table></center>
</form>
<br>
<br>
<center>
<table id="test_table" cellpadding="0" cellspacing="0" width="60%" border='1' bgcolor="#999999">
  <thead>
    <tr>
      <th> Element_Number</th>
    </tr>
  </thead>
<tbody>
</tbody>
</table>

<script>

    $(document).ready(function(){
      $('#submit').click(function(){
        var no=$('[name="no"]');
        $.post('your_current_page_name.php',{"no":no},function(data){
$("#test_table> tbody").append(data);

});
        });
      });
</script>

<?php
if(isset($_POST['no']))
{
    $c=mysql_connect("localhost","root","");
    mysql_select_db("test");
    if(!$c)
    {
        echo "db prob".mysql_error();
    }

    $no=$_POST['no'];
    $qry=mysql_query("select name from opt where no='$no'");
    if(!$qry)
    {
        echo "ret".mysql_error();
    }


        if(mysql_num_rows($qry)!=0)
        {
            $row=mysql_fetch_array($qry);
            $name=$row['name'];
        }
        else
        {
            echo "query Invalid";
        }
    echo    "<tr><td>" .$name."</td></tr>" ;

}

Upvotes: 1

Related Questions