这有可能用html和php完成吗?

时间:2013-07-11 15:30:08

标签: php html

我已经填写了以下表格。是否有可能使用户输入字段数(例如6)时,下表中有6行。如果可以在没有任何提交按钮的情况下创建它(这样该动作的触发器将从文本输入框退出)将会很棒。

enter image description here

以下是此表单的html代码:

<fieldset>
  <legend>Student Information</legend>
  Number of fields: <input type="text"><br />
  Total number of characters: <input type="text">
  <br>
  <br>
  <table border="1">
    <th></th>
    <th>field</th>
    <th>number of characters</th>
    <tr>
        <td>1</td>
        <td><input type="text"></td>
        <td><input type="text"></td>
    </tr>
    <tr>
        <td>2</td>
        <td><input type="text"></td>
        <td><input type="text"></td>
    </tr>
  </table>

  </fieldset>

如果这是不可能的(没有提交按钮),那么你会以哪种方式完成相同的结果?感谢您提供任何帮助和/或建议。

3 个答案:

答案 0 :(得分:3)

PHP是服务器端,它只在页面加载时运行一次。 HTML不是一种编程语言。您可以使用PHP生成表,但前提是您有一个重新加载页面的提交按钮。如果由于用户事件而必须发生,则总是需要使用Javascript。

这意味着,您将需要 Javascript,无需重新加载页面即可完成此工作。理想情况下,您可以使用Jquery(Javascript最受欢迎的插件)来操作DOM。

如果您有此输入:

<input id="field" type="text">

您可以像这样调用on-leave事件:

$("p").focusout(function() 
{
    // Delete the previous table, and create a new one, here
});

至于创建实际表,它并不复杂,但它有点工作。您应该阅读以下参考资料来启动您:

http://www.tutorialspoint.com/jquery/jquery-dom.htm

您需要事先“安装”JQuery,您可以在代码顶部简单地插入它:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

答案 1 :(得分:1)

只用PHP就可以了。

因此,例如,如果您输入'6'行,您可以捕获表单帖子并执行类似(HTML中的模板表单):

<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>

<?php endfor; ?>

答案 2 :(得分:1)

好的,这是您需要的仅发布脚本

<?php
  $rows=2;
  if(isset($_POST['submit']))
  {
    if($_POST['submit']=='Update')
    {
        if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
    }
    else
    {
        // process posted data here

        // reset post or jump to another page
        $_POST=array();
        //header("Location:index.php");
        //exit();
    }
  }
?>
<form method="post">
<fieldset>
  <legend>Student Information</legend>
  Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
  Total number of characters: <input type="text">
  <input type="submit" name="submit" value="Update"/>
  <br>
  <br>
  <table border="1">
    <th></th>
    <th>field</th>
    <th>number of characters</th>
    <?php
        for($loop=1;$loop<=$rows;$loop++)
        {
            echo '<tr>';
            echo '<td>'.$loop.'</td>';
            echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
            echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
            echo '</tr>';
        }
    ?>
  </table>
  <input type="submit" name="submit" value="Submit"/>
  </fieldset>
</form>

默认为2行(最小值),并在更新行时保留数据 如果行减少,那么最终的行会消失

相关问题