php获取选择选项和文本输入

时间:2013-11-26 02:57:00

标签: php html forms post get

您好我正在尝试获取所选值并在选中时执行操作。还试图打印该文本框(打印只是临时)

<form name="recAdd" method="GET" action="add.php">
    <select name="rec">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select><br>
    <input type="text" name="name" />
    <input type="submit" value="Submit" />
</form>

PHP(add.php):

switch($_POST['rec']){
case '1':
    $rec1 = "rec1.txt";
    $fh = fopen($rec1, 'a') or die("can't open file");
    $romString = $_GET['name'];
    fwrite($fh, $romString);
    fclose($fh);
    print()
break;
case '2':
    // do Something
break;
case '3':
    // do Something
break;
default:
    print("Not working ;(");
}

没有做所有看文本框和下拉列表我使用了它并且它工作(只是为了看看写入文件是否正常等)。

$recAll = "recAll.txt";
$fh = fopen($recAll, 'a') or die("can't open file");
$stringData = "New Stuff 1\n";
fwrite($fh, $stringData);
$stringData = "New Stuff 2\n";
fwrite($fh, $stringData);
fclose($fh);

3 个答案:

答案 0 :(得分:2)

因为公众要求了!这是我的评论扩展为一个完整的答案:

您正在使用$_GET$_POST,这不常见,您是故意这样做的吗?我的猜测是:不。

如果您在HTML中看到:

<form name="recAdd" method="GET" action="add.php">

这意味着来自浏览器的请求将是GET。您可以将其更改为POST:

<form name="recAdd" method="POST" action="add.php">

PHP(add.php):

switch($_POST['rec']){
case '1':
    $rec1 = "rec1.txt";
    $fh = fopen($rec1, 'a') or die("can't open file");
    $romString = $_POST['name']; //<---
    fwrite($fh, $romString);
    fclose($fh);
    print 'something'; //<<--- fixed syntax, btw: I would use echo
break;
case '2':
    // do Something
break;
case '3':
    // do Something
break;
default:
    print("Not working ;(");
}

注意:我已经使用此变体进行了测试,但它正在运行。

或者将其全部更改为GET(我建议继续使用POST)

switch($_GET['rec']){ //<-----
case '1':
    $rec1 = "rec1.txt";
    $fh = fopen($rec1, 'a') or die("can't open file");
    $romString = $_GET['name'];
    fwrite($fh, $romString);
    fclose($fh);
    print 'something'; //<<--- fixed syntax, btw: I would use echo
break;
case '2':
    // do Something
break;
case '3':
    // do Something
break;
default:
    print("Not working ;(");
}

确保Web服务器(即Apache)有权在目标文件夹中写入。这可能会阻止操作成功。

您还可以打开来自PHP的错误消息,这可能会让您了解正在发生的事情。您可以使用以下命令为当前的PHP脚本执行此操作:

error_reporting(E_ALL | E_STRICT);

注意:error_reporting at php.net


如果您不确定使用了哪种请求方法,可以尝试使用$_SERVER['REQUEST_METHOD']

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    //Ok we got a POST, probably from a FORM, read from $_POST.
    var_dump($_PSOT); //Use this to see what info we got!
}
else
{
    //You could assume you got a GET
    var_dump($_GET); //Use this to see what info we got!
}

注意:$_SERVER at php.net

您可能也对PHP Session managementPost-Redirect-Get感兴趣。你可以这样做:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    //Ok we got a POST, probably from a FORM, read from $_POST.
    var_dump($_PSOT); //Use this to see what info we got!
    //Do stuff...
    //Write results to session
    session_start();
    $_SESSION['stuff'] = $something;
    //redirect:
    header('Location: add.php', true, 303);
    //The redirection will cause the browser to request with GET
    //The results of the operation are in the session variable
    exit();
}
else
{
    //You could assume you got a GET
    var_dump($_GET); //Use this to see what info we got!
    //Get stuff from session
    session_start();
    if (array_key_exists('stuff', $_SESSION))
    {
       $something = $_SESSION['stuff'];
       //we got stuff
       //later use present the results of the operation to the user.
    }
    //clear stuff from session:
    unset($_SESSION['stuff']);
}

注意:$_SESSION at php.net

答案 1 :(得分:0)

更改以下内容......

<form name="recAdd" method="POST" action="add.php">

$romString = $_POST['name'];

答案 2 :(得分:0)

您的表单正在使用get方法将数据发送到add.php。但是,您使用$_POST来检索rec值。有两种方法可以解决这个问题:

方法1:

<form name="recAdd" method="GET" action="add.php">更改为<form name="recAdd" method="POST" action="add.php">。同时将$romString = $_GET['name'];更改为$romString = $_POST['name'];

方法2:

switch($_POST['rec']){更改为switch($_GET['rec']){。只要您的表单方法获得,这将有效。

方法3:

switch($_POST['rec']){更改为switch($_REQUEST['rec']){。无论你的表格方法是什么,这都会有效。 $_REQUEST包含$_POST$_GET的值。

我已经按照从最理想到最少的顺序列出了这些解决方案。混合$ _GET和$ _POST通常不是最佳解决方案。