从表单按钮调用PHP函数

前端之家收集整理的这篇文章主要介绍了从表单按钮调用PHP函数 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

enter image description here

好吧,我的脚本应该看起来像那样.我必须主要在PHP中进行操作.按钮添加应将数据保存到文件,显示应读取该文件并将其放入textarea,删除必须删除所选行,然后重置将重置所有内容.

  1. <?PHP
  2. $plik =fopen("data.dat","a+");
  3. @fputs($plik,$_POST["name"]. " " . $_POST["sname"] . " " . $_POST["adres"] . " " . $_POST["number"] . "<br>" );
  4. fclose($plik);
  5. ?>
  6. <html>
  7. <body>
  8. <form action = "<?PHP $_PHP_SELF ?>" method = "POST">
  9. Name: <input type = "text" name = "name" /><br>
  10. Second Name: <input type = "text" name = "sname" /><br>
  11. Adres: <input type = "text" name = "adres" /><br>
  12. Number: <input type = "text" name = "number" /><br>
  13. <input type = "submit" name="add" value="Add"/>
  14. <input type = "button" name="show" value="Show"/>
  15. <input type = "button" name="reset" value="Reset"/>
  16. <input type = "button" name="delete" value="Delete"/><br>
  17. <textarea id="lista" name="lista" rows="20" cols="40" style="overflow:scroll" readonly="" wrap="off"></textarea>
  18. </form>
  19. </body>
  20. </html>

我的脚本看起来像这样,我不知道下一步该怎么做.如何为这些按钮添加功能,外观如何?

最佳答案
我认为这应该可以做到:

  1. <?PHP
  2. if(isset($_POST['action'])) {
  3. switch($_POST['action']) {
  4. case('Add'): ... break;
  5. case('Show'): ... break;
  6. case('Reset'): ... break;
  7. case('Delete'): ... break;
  8. default: ...
  9. }
  10. }
  11. ?>
  12. <html>
  13. <body>
  14. <form action = "<?PHP $_PHP_SELF ?>" method = "POST">
  15. Name: <input type = "text" name = "name" /><br>
  16. Second Name: <input type = "text" name = "sname" /><br>
  17. Adres: <input type = "text" name = "adres" /><br>
  18. Number: <input type = "text" name = "number" /><br>
  19. <input type = "submit" name="action" value="Add"/>
  20. <input type = "submit" name="action" value="Show"/>
  21. <input type = "submit" name="action" value="Reset"/>
  22. <input type = "submit" name="action" value="Delete"/><br>
  23. <textarea id="lista" name="lista" rows="20" cols="40" style="overflow:scroll" readonly="" wrap="off"></textarea>
  24. </form>
  25. </body>
  26. </html>

如您所见,我刚刚将操作按钮的类型更改为“提交”,并为所有按钮设置了相同的名称.然后在PHP中,只需测试是否设置了动作,然后选择要执行的正确动作即可.希望能帮助到你

猜你在找的HTML相关文章