php - 从表单检查外部数组
php - Check external array from form
我在 html 中创建了一个表单。在这种形式中,您可以指定操作系统。表格如下所示:
<input type="hidden" name="toshibaproduct" value="001"/>
<input type="hidden" name="toshibamerk" value="toshiba"/>
<input type="hidden" name="toshibamodel" value="Sattelite A100-510"/>
Operating system <select name="beschikbaarheid" value="true">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0"/>
<input type="hidden" name="toshibaprijs" value="999.99"/>
<input type="image" src="bestel.jpg" border=0 value="bestellen"/>
我还创建了一个外部函数,我将其包含在页面中,如下所示:
<?php
function beschikbaarheid($merk, $os)
{
$beschikbaar = array(
"Toshiba" => array("xp" => true, "vista" => false, "linux" => true),
"Acer" => array("xp" => true, "vista" => true, "linux" => true),
"Hp" => array("xp" => true, "vista" => false, "linux" => true));
return ($beschikbaar[$merk][$os]);
}
?>
我的计划是让表单在外部函数中检查操作系统是否可用,如果不可用,return 会显示一条消息,说明它已售罄。如果它可用,它应该继续脚本的其余部分(顺便说一下,它可以工作,但它不包括在这里)。
问题是,我不知道如何让表单检查外部函数中的数组。我还想知道在外部函数中只检查操作系统是否有问题,而不是其他形式,例如数量。
如果您需要在提交表单之前进行检查,请尝试编写 JavaScript 或 jQuery 函数。否则,您可以在提交数据的页面中调用该函数。然后您可以验证该字段并生成响应。
例如,如果您有一个包含可用操作系统列表的 JSON 文件,您可以使用它来对照列表检查选择。
`os.json`
[
"Windows Vista", "Windows XP", "OSX", "Linux"
]
some other PHP file
<?php
function is_os_available($os) {
return in_array($os, json_decode(file_get_contents('os.json')));
}
希望这对您有所帮助,
肖恩
已更新HTML
<head>
<!-- include jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Maker
<select name="maker" value="true" id="maker">
<option value="toshiba">Toshiba</option>
<option value="acer">Acer</option>
<option value="hp">Hp</option>
</select>
Operating system
<select name="beschikbaarheid" value="true" id="operationSystem">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" id="quantity" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="button" value="bestellen" id="submitData" />
<span id="messages"></span>
Ajax
<!-- Ajax -->
<script type="text/javascript">
$('#submitData').click(function(){
var selectedOs = $('#operationSystem').find('option:selected').val();
var selectedMaker = $('#maker').find('option:selected').val();
var selectedQuantity = $('#quantity').val();
if(selectedQuantity == 0 || selectedQuantity == ''){
$('#messages').text('Please provide valid information');
return false;
}
// You can perform an ajax request using the .ajax() method
$.ajax({
type: 'POST',
url: 'beschikbaarheid.php', // This is the url that will be requested
data: {operation_system: selectedOs, maker: selectedMaker, quantity: selectedQuantity},
success: function(data){
if(data){
$('#messages').text('Available');
}else{
$('#messages').text('Not available');
}
},
});
});
</script>
服务器端脚本(beschikbaarheid.php)
<?php
if(isset( $_POST['quantity'] )) {
$operatingSystem = $_POST['operation_system'];
$maker = $_POST['maker'];
$quantity = $_POST['quantity'];
echo beschikbaarheid($operatingSystem, $maker, $quantity);
}
function beschikbaarheid($operatingSystem, $maker, $quantity)
{
// I did't used the quantity section
// Please use it if needed
// Also this structure is not much prefered, I just used this becasuse you might have some out put like this.
$beschikbaar = array(
"toshiba" => array ("xp" =>false, "vista" => false, "linux" => true),
"hp" => array ("xp" =>true, "vista" => false, "linux" => true),
"acer" => array ("xp" =>true, "vista" => false, "linux" => true)
);
return $beschikbaar[$maker][$operatingSystem];
}
?>
有多种方法可以实现这一点,希望对您有所帮助。
我在 html 中创建了一个表单。在这种形式中,您可以指定操作系统。表格如下所示:
<input type="hidden" name="toshibaproduct" value="001"/>
<input type="hidden" name="toshibamerk" value="toshiba"/>
<input type="hidden" name="toshibamodel" value="Sattelite A100-510"/>
Operating system <select name="beschikbaarheid" value="true">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0"/>
<input type="hidden" name="toshibaprijs" value="999.99"/>
<input type="image" src="bestel.jpg" border=0 value="bestellen"/>
我还创建了一个外部函数,我将其包含在页面中,如下所示:
<?php
function beschikbaarheid($merk, $os)
{
$beschikbaar = array(
"Toshiba" => array("xp" => true, "vista" => false, "linux" => true),
"Acer" => array("xp" => true, "vista" => true, "linux" => true),
"Hp" => array("xp" => true, "vista" => false, "linux" => true));
return ($beschikbaar[$merk][$os]);
}
?>
我的计划是让表单在外部函数中检查操作系统是否可用,如果不可用,return 会显示一条消息,说明它已售罄。如果它可用,它应该继续脚本的其余部分(顺便说一下,它可以工作,但它不包括在这里)。
问题是,我不知道如何让表单检查外部函数中的数组。我还想知道在外部函数中只检查操作系统是否有问题,而不是其他形式,例如数量。
如果您需要在提交表单之前进行检查,请尝试编写 JavaScript 或 jQuery 函数。否则,您可以在提交数据的页面中调用该函数。然后您可以验证该字段并生成响应。
例如,如果您有一个包含可用操作系统列表的 JSON 文件,您可以使用它来对照列表检查选择。
`os.json`
[
"Windows Vista", "Windows XP", "OSX", "Linux"
]
some other PHP file
<?php
function is_os_available($os) {
return in_array($os, json_decode(file_get_contents('os.json')));
}
希望这对您有所帮助,
肖恩
已更新HTML
<head>
<!-- include jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Maker
<select name="maker" value="true" id="maker">
<option value="toshiba">Toshiba</option>
<option value="acer">Acer</option>
<option value="hp">Hp</option>
</select>
Operating system
<select name="beschikbaarheid" value="true" id="operationSystem">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" id="quantity" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="button" value="bestellen" id="submitData" />
<span id="messages"></span>
Ajax
<!-- Ajax -->
<script type="text/javascript">
$('#submitData').click(function(){
var selectedOs = $('#operationSystem').find('option:selected').val();
var selectedMaker = $('#maker').find('option:selected').val();
var selectedQuantity = $('#quantity').val();
if(selectedQuantity == 0 || selectedQuantity == ''){
$('#messages').text('Please provide valid information');
return false;
}
// You can perform an ajax request using the .ajax() method
$.ajax({
type: 'POST',
url: 'beschikbaarheid.php', // This is the url that will be requested
data: {operation_system: selectedOs, maker: selectedMaker, quantity: selectedQuantity},
success: function(data){
if(data){
$('#messages').text('Available');
}else{
$('#messages').text('Not available');
}
},
});
});
</script>
服务器端脚本(beschikbaarheid.php)
<?php
if(isset( $_POST['quantity'] )) {
$operatingSystem = $_POST['operation_system'];
$maker = $_POST['maker'];
$quantity = $_POST['quantity'];
echo beschikbaarheid($operatingSystem, $maker, $quantity);
}
function beschikbaarheid($operatingSystem, $maker, $quantity)
{
// I did't used the quantity section
// Please use it if needed
// Also this structure is not much prefered, I just used this becasuse you might have some out put like this.
$beschikbaar = array(
"toshiba" => array ("xp" =>false, "vista" => false, "linux" => true),
"hp" => array ("xp" =>true, "vista" => false, "linux" => true),
"acer" => array ("xp" =>true, "vista" => false, "linux" => true)
);
return $beschikbaar[$maker][$operatingSystem];
}
?>
有多种方法可以实现这一点,希望对您有所帮助。