-4
 <form name="search" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
 Seach for: <input type="text" name="find" /> in 
 <Select NAME="field">
 <Option VALUE="english">english</option>
 <Option VALUE="spanish">spanish</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" />
 </form>

  <?php
$options = array('english'=> array('1' => 'one', '2' => 'two'), 'spanish' =>array('1'=>'uno', '2'=>'dos'));

if (($_POST['find'] == '1')&& ($_POST['field'] == 'english')){
    echo $options['english']['1'];
}
?>

我想有这个功能,当用户输入'1'和seclet'english'时,它会显示一个,当用户输入'1'sellet'spanish'时,它会显示uno,依此类推。

问题:

  1. 有什么好的方法可以替换这条线吗?
 if (($_POST['find'] == '1')&& ($_POST['field'] == 'english')){
      echo $options['english']['1'];
    }

想象一下,如果我有从 1 到 100 的数字,我就不可能重复这些代码(==1 && ==english,==2 && == english...)。

2 如果你运行这个脚本,它会显示

注意:未定义的索引:find ...

这是什么问题以及如何解决?

4

3 回答 3

0

First point :

<form name="search" method="post" action="<?php $_SERVER['PHP_SELF'];?>">

The <?php $_SERVER['PHP_SELF'];?> will do nothing. You shall have to use echo here. Nevertheless, the bug does not show because it's the default behaviour for a form to call the script at $_SERVER['PHP_SELF'] when the action attribute is not set.

Also :

<input type="hidden" name="searching" value="yes" />

seems useless.

Second point :

Of course you don't want to parse each possible choice in a static fashion. You'll use abstraction and dynamic behavior.

You have one array index by language and each sub array gives the word used for each number. Hence, you have your rule and your computations will look like :

<?php
$input = trim($_POST['find']);
$language = $_POST['field'];
$output = $options[$language][$input];
echo $output;
?>

That's minimal code. Please don't use it 'as is'. You'll have at least to check if keys exist and provide some adequate error messages.

于 2013-05-19T09:59:30.273 回答
0

It looks like even on initial page load you're checking for posted values. Try wrapping the bottom php code like this:

<?php
    if(!empty($_POST)) {
        $options = array('english'=> array('1' => 'one', '2' => 'two'), 'spanish' =>array('1'=>'uno', '2'=>'dos'));

        if (($_POST['find'] == '1')&& ($_POST['field'] == 'english')){
            echo $options['english']['1'];
        }
    }
?>
于 2013-05-19T09:47:18.370 回答
0
<?php

$html =<<<'HTML'

<form name="search" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
 Seach for: <input type="text" name="find" /> in 
 <Select NAME="field">
 <Option VALUE="english">english</option>
 <Option VALUE="spanish">spanish</option>
 </Select>
 <input type="hidden" name="searching" value="yes" />
 <input type="submit" name="search" value="Search" />
 </form>
HTML;

echo $html;

$options = array('english'=> array('1' => 'one', '2' => 'two'), 'spanish' =>array('1'=>'uno', '2'=>'dos'));

if ((isset($_POST['find']) == '1')&& (isset($_POST['field']) == 'english')){
    echo $options['english']['1'];
}
?>
于 2013-05-19T09:47:29.237 回答