基本上我有2页; Page1.php和Page2.php ...在第1页上,用户可以输入他们的邮政编码和属性值,提交页面后将POST到page2.php。
我正在寻找帮助让小脚本根据属性值返回(回显或打印)一个值给客户,为了简单起见,我将在属性值范围内设置,例如:
£0 => £100,000 --> Return as £125
£101,000 => £200,000 --> Return as £195
£201,000 => £300,000 --> Return as £275
等等......
我希望让脚本返回输出变量,这样我就可以使用echo函数显示用户在页面上的set css框中输入的范围的默认值。
因此,如果我作为客户输入page1.php并输入我的邮政编码并输入我的属性值为225,000英镑(这不会被用作选定的选项,将由用户输入手动与邮政编码框相同)
当我提交此内容并转到page2.php时,脚本将意识到我已进入201,000英镑 - 300,000英镑的范围内并使用echo变量函数返回默认值。
我希望我已经解释了我能做的最好的事情,因为我正在学习基础知识并且仍然试图使用某些术语。非常感谢任何帮助,并感谢您抽出宝贵时间阅读这篇文章,包括评论和帮助的时间。
答案 0 :(得分:1)
这是page1.php的代码:
<form action="page2.php" method="get">
<input name="userfield" id="thiscanbedifferent" type="text">
</form>
page2.php的此代码将使用户输入安全使用:
$userPrice = $_GET['userfield'];//The $_GET variable has all the values from your form as long as you specify method: get. Remember that users are both incompetent and devious, and that you can never trust their input. Always clean and check it!!
$userPrice = preg_replace('/[^0-9.]/','',$userPrice);//This is called a regular expression, it filters out stuff we don't want. This one gets rid of characters that are not in the range 0-9, or "."
$userPrice = floatval($userPrice);//This turns the cleaned value into a number
此代码会将任意数字转换为范围。
$rangeLabels = ["$0 - $100,000",
"$101,000-$200,000",
"$201,000-$300,000"];//labels for pricing. can be extended indefinately, as needed
$rangePricing = [125,175,195];//pricing steps. can be extended indefinately, as needed
$rangeIndex = floor($userPrice/100000);//100000 can be adjusted to any increment you would like, just be sure to update labels accordingly
//$rangeIndex = floor(($userPrice-1000)/100000);//this alternate equation makes values such as 100,999 fall below $101,000. My literal (probably wrong) interpretation of your range labels.
if($rangeIndex < 0 || $rangeIndex > count($rangePricing)){
die('Number is out of range!');//Use better error handling in production
}
echo 'The user has selected the price $' . floatval($userPrice) . ' which falls under into the range ' . $rangeLabels[$rangeIndex] . ', for which the pricing is ' . $rangePricing[$rangeIndex] . '<br>';
答案 1 :(得分:1)
第1页包含用户输入其输入的表单:
<form action="page2.php" method="post">
<input type="text" name="userinput" placeholder="Type.." />
<input type="submit" value="Send to page 2" name="submitPrice" />
</form>
查看输入元素上的名称属性。
第2页
此页面处理表单中的数据。
你可以做的一种方法是有一个存储的查找表 范围:
$data = array(
array(
'min' => 0,
'max' => 100,
'value' => 125
),
array(
'min' => 101,
'max' => 200,
'value' => 195
),
array(
'min' => 201,
'max' => 300,
'value' => 275
)
);
现在,您可以使用此表检查用户输入是否在 min 和 max 之间。我在此示例中使用了array_filter
:
function getAdjustedPrice($price, &$table) {
$priceData = current(array_filter($table, function($value) use(&$price) {
return $value['min'] <= $price && $value['max'] >= $price;
}));
return $priceData['value'];
}
并检查它是否有效:
$input = 101;
printf("User entered: %d, adjusted price: %d",
$input,
getAdjustedPrice($input, $data));
$input
现在只是静态的,所以我们可能想用它替换它
来自表格的价值。该值存储在超全局数组_POST
中:
$input = intval($_POST["userinput"]);
&#34; userinput&#34; 是我们在第1页表单中输入元素上使用的名称。