从文本字段向表中添加值

时间:2014-05-25 06:40:09

标签: php mysql

在表单中有一个文本字段和一个表。如果我们在该文本字段中放置一个值,则需要在表格中显示相应的名称:

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

}
?>

使用此代码,值会在表格中输入,但是当下一个值输入到文本字段时,我想要将值经常添加到表格作为下一行

1 个答案:

答案 0 :(得分:1)

在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>" ;

}