douquan1015 2015-06-08 15:49
浏览 56
已采纳

检索PHP表单输入的标准方法

I have been doing web programming with PHP for about 2 months,and for forms, I have been retrieving user input in a manner such as this:

For standard <input type="text">:

/*input sanitation*/
function testInput($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = mysql_real_escape_string($data);
    return $data;
}

/*just gets the data*/
function getRText($HTMLname) {

if (isset ( $_POST [$HTMLname] ) && ! empty ( $_POST [$HTMLname] )) {
    return testInput(( $_POST [$HTMLname] ));
} else {
    throw new Exception("Input is missing from " + $HTMLname);
}
}

And then, on another script, I'd do something like this:

$userID = getRText('uid');
$company = getRText('company');
$projectNum = getRText('projnum');

$dataArray = array($userID, $company, $projectNum);

The problem with this approach is it's very time consuming when I have a large form. I'm thinking in Perl (using Perl CGI), I'd be able to dynamically loop across the user input fields, and add each input into an array dynamically, but I'm not sure if something like this is possible in PHP. Right now, I'm currently having to manually pull each data from each input. All the PHP form examples online do it in this manner as well. Is this the correct way of pulling data from PHP forms?

  • 写回答

2条回答 默认 最新

  • doupo1908 2015-06-08 15:56
    关注

    All data from a form is delivered to your script in either the $_POST or $_GET array.

    So you could simply do

    foreach ($_POST as $key => $val)
    {
        $_POST[$key] = testInput($val);
    }
    

    This will run your sanitization and place the data back into the $_POST array therefore removing the need for yet another array.

    I never did understand why people move data from the $_POST/$_GET array to scalar variables or other arrays. Its a perfectly good array and once delivered to you its all yours to do whatever you like with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了