从表单中打印出数组的键值
Print out key values of array from form
目前我是运行一个php文件,格式是我自己的html文件。我想在最后打印出数组中的键值。数组用于保存下拉框和单选框的值。我想知道如何连接用户在 operating system
、processor
、memory
等结束回显行内选择的值。
我试过:
echo "<strong>Operating system:</strong> .$OS. <br>\n";
我也尝试开始做:
foreach($OS as $cost => name)
但只知道循环内打印。
所以我希望得到帮助以了解如何执行此操作。
表格如下所示。
下面是我的 html
代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Computer Store</title>
<!-- Bootstrap -->
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<style type='text/css'>
select {
border: 1px solid #669;
font: bold 14pt;
color: #3399cc;
border-radius: 5px;
}
.c1 {
width: 150px;
font-weight: bold;
color: #036;
}
td {
padding: 5px;
}
input[type=text] {
border: 2px solid #036;
border-radius: 2px;
}
.outerbox {
border: 2px solid #036;
border-radius: 5px;
margin: 0;
padding: 0;
margin-bottom: 10px;
}
#ctr {
margin-top: 10px;
}
</style>
</head>
<body class='container'>
<h2>Online Computer Order Form</h2>
<form name="OrderForm" method="GET" action="assignment1.php">
<div class='row outerbox'>
<h2>Configure your system</h2>
<table class='table'>
<tr>
<td class='c1'>Operating System</td>
<td>
<select name="os" id="os">
<option value="0">--select operating system--</option>
<option value="w7">Windows 7</option>
<option value="w7p">Windows 7 Pro</option>
<option value="w8">Windows 8.1</option>
<option value="u12">Ubuntu 12.04</option>
</select>
</td>
</tr>
<tr>
<td class='c1'>Processor</td>
<td>
<select name="processor" id="processor">
<option selected="selected" value="0">Intel Core i3</option>
<option value="200">Intel Core i5</option>
<option value="350">Intel Core i7</option>
</select>
</td>
</tr>
<tr>
<td class='c1'>Memory</td>
<td>
<input type="radio" name="memory" id="memory1" value="2200">128 GB
<input type="radio" name="memory" id="memory2" value="1600">64 GB
<input type="radio" name="memory" id="memory3" value="500">32 GB
<input type="radio" name="memory" id="memory4" value="295">16 GB
<input type="radio" name="memory" id="memory5" value="0" checked="checked">8 GB
</td>
</tr>
<tr>
<td class='c1'>Hard Drive</td>
<td>
<input type="radio" name="hd" id="hd1" value="0" checked>320GB
<input type="radio" name="hd" id="hd2" value="100">500GB
<input type="radio" name="hd" id="hd3" value="200">1000GB
<input type="radio" name="hd" id="hd2" value="350">2000GB
</td>
</tr>
<tr>
<td class='c1'>CD/DVD Driver</td>
<td>
<input type="radio" name="cdr" id="cdr1" value="0" checked class="d_drive">DVD-ROM Drive
<input type="radio" name="cdr" id="cdr2" value="50" class="d_drive">DVD+/-RW Drive
</td>
</tr>
</table>
</div>
<div class='row outerbox'>
<h2>Contact Information</h2>
<table class='table'>
<tr>
<td class='c1'>First Name</td>
<td>
<input type="text" name="First" size="20" id='first' placeholder="First name">
</td>
</tr>
<tr>
<td class='c1'>Last Name</td>
<td>
<input type="text" name="Last" size="20" id='last' placeholder="Last name">
</td>
</tr>
<tr>
<td class='c1'>Phone #</td>
<td>
<input type="text" name="Phone" size="20" id='phone' placeholder="Phone">
</td>
</tr>
</table>
</div>
<h3>Special instructions/Comments:</h3>
<textarea name='comments' placeholder="If you have any comments or special instructions, please enter them here" class='form-control' rows='3'>
</textarea>
<p id='ctr'>
<input type="submit" value="Finalize sale" class='btn btn-primary'>
<input type="reset" value="Cancel" class='btn btn-danger'>
</p>
</form>
</body>
</html>
PHP
文件:
<?php
// Begin by making variables for the user's First, Last Name and Phone number
$first = $_GET['First'];
$last = $_GET['Last'];
$phone = $_GET['Phone'];
//Array Variables for each section
$OS = array('w7' => 'Windows 7', 'w7p' => 'Windows 7 Pro', 'w8' => 'Windows 8.1', 'u12' => 'Ubuntu 12.04');
$Proc = array('0' => 'Intel Core i3', '200' => 'Intel Core i5', '350' => 'Intel Core i7');
$Memory = array('2200' => '320 GB', '1600' => '64 GB', '500' => '32 GB', '295' => '16 GB', '0' =>
'8 GB');
$H_Drive = array('0' => '320 GB', '100' => '500 GB', '200' => '1000 GB', '350' => '2000 GB');
$C_Drive = array('0' => 'DVD-ROM Drive', '50' => 'DVD+/-RW Drive');
$Comments = $_GET['comments'];
//Connecting options
foreach ($OS as $cost => $name){
}
//Totaling Cost
$total = 0;
$salesTax = $total * .05;
$purchaseTotal = $total + $salesTax;
//Print out information
echo "Thank you" .$first. " " .$last. " for using Sam's online store. <br>\n";
echo "You have selected the following system: <br>\n";
echo "<strong>Base system</strong><br>\n";
echo "<strong>Operating system:</strong><br>\n";
echo "<strong>Processor:</strong> <br>\n";
echo "<strong>Memory:</strong> <br>\n";
echo "<strong>Hard drive:</strong> <br>\n";
echo "<strong>DVD Drive:</strong> <br>\n";
echo "<strong>Total:</strong>" .$total. "<br>\n";
echo "<strong>Sales tax(5%):</strong>" .$salesTax. "<br>\n";
echo "<strong>Total Amount:</strong>" .$purchaseTotal. "<br>\n";
echo "<strong>Comments or special instructors:</strong> <br>\n" .$Comments."<br>\n";
?>
以与名称相同的方式获取值
$osValue = $_GET['os'];
$procValue = $_GET['processor'];
$memoryValue = $_GET['memory'];
$hdValue = $_GET['hd'];
$cdrValue = $_GET['cdr'];
并使用它作为从关联数组调用的键
echo $OS[$osValue];
echo $Proc[$procValue];
echo $Memory[$memoryValue];
echo $H_Drive[$hdValue];
echo $C_Drive[$cdrValue];
您的 assignment1.php 文件,查看评论 - 已测试,有效。
<?php
/*pulling in variables from get.*/
$osValue = $_GET['os'];
$procValue = $_GET['processor'];
$memoryValue = $_GET['memory'];
$hdValue = $_GET['hd'];
$cdrValue = $_GET['cdr'];
$first = $_GET['First'];
$last = $_GET['Last'];
$phone = $_GET['Phone'];
//Array Variables for each section
$OS = array('w7' => 'Windows 7', 'w7p' => 'Windows 7 Pro', 'w8' => 'Windows 8.1', 'u12' => 'Ubuntu 12.04');
$Proc = array('0' => 'Intel Core i3', '200' => 'Intel Core i5', '350' => 'Intel Core i7');
$Memory = array('2200' => '320 GB', '1600' => '64 GB', '500' => '32 GB', '295' => '16 GB', '0' =>
'8 GB');
$H_Drive = array('0' => '320 GB', '100' => '500 GB', '200' => '1000 GB', '350' => '2000 GB');
$C_Drive = array('0' => 'DVD-ROM Drive', '50' => 'DVD+/-RW Drive');
$Comments = $_GET['comments'];
//Connecting options
foreach ($OS as $cost => $name){
//stuff you wanna do
}
//Totaling Cost
$total = 0;
$salesTax = $total * .05;
$purchaseTotal = $total + $salesTax;
//Print out information
/* ucwords(string) capitalizes the first letter */
echo "Thank you " .ucwords($first). " " .ucwords($last). " for using Sam's online store. <br>\n";
echo "You have selected the following options: <br>\n";
/* your values would go here */
echo "<strong>Operating system:</strong> $OS[$osValue]<br>\n";
echo "<strong>Processor:</strong> $Proc[$procValue]<br>\n";
echo "<strong>Memory:</strong> $Memory[$memoryValue]<br>\n";
echo "<strong>Hard drive:</strong> $H_Drive[$hdValue]<br>\n";
echo "<strong>DVD Drive:</strong> $C_Drive[$cdrValue]<br>\n";
echo "<strong>Total:</strong>" .$total. "<br>\n";
echo "<strong>Sales tax(5%):</strong>" .$salesTax. "<br>\n";
echo "<strong>Total Amount:</strong>" .$purchaseTotal. "<br>\n";
echo "<strong>Comments or special instructors:</strong> <br>\n" .$Comments."<br>\n";
?>
上面显示的@Conceptz 解决了我的所有问题,但总信息(涉及关键值)除外。
我以为我需要一个 foreach 来获取总值,但我真正需要做的是:
$base = 890;
$total = $base + $procValue + $memoryValue + $hdValue + $cdrValue;
这给了我总计的 $key 值。
目前我是运行一个php文件,格式是我自己的html文件。我想在最后打印出数组中的键值。数组用于保存下拉框和单选框的值。我想知道如何连接用户在 operating system
、processor
、memory
等结束回显行内选择的值。
我试过:
echo "<strong>Operating system:</strong> .$OS. <br>\n";
我也尝试开始做:
foreach($OS as $cost => name)
但只知道循环内打印。 所以我希望得到帮助以了解如何执行此操作。
表格如下所示。
下面是我的 html
代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Computer Store</title>
<!-- Bootstrap -->
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<style type='text/css'>
select {
border: 1px solid #669;
font: bold 14pt;
color: #3399cc;
border-radius: 5px;
}
.c1 {
width: 150px;
font-weight: bold;
color: #036;
}
td {
padding: 5px;
}
input[type=text] {
border: 2px solid #036;
border-radius: 2px;
}
.outerbox {
border: 2px solid #036;
border-radius: 5px;
margin: 0;
padding: 0;
margin-bottom: 10px;
}
#ctr {
margin-top: 10px;
}
</style>
</head>
<body class='container'>
<h2>Online Computer Order Form</h2>
<form name="OrderForm" method="GET" action="assignment1.php">
<div class='row outerbox'>
<h2>Configure your system</h2>
<table class='table'>
<tr>
<td class='c1'>Operating System</td>
<td>
<select name="os" id="os">
<option value="0">--select operating system--</option>
<option value="w7">Windows 7</option>
<option value="w7p">Windows 7 Pro</option>
<option value="w8">Windows 8.1</option>
<option value="u12">Ubuntu 12.04</option>
</select>
</td>
</tr>
<tr>
<td class='c1'>Processor</td>
<td>
<select name="processor" id="processor">
<option selected="selected" value="0">Intel Core i3</option>
<option value="200">Intel Core i5</option>
<option value="350">Intel Core i7</option>
</select>
</td>
</tr>
<tr>
<td class='c1'>Memory</td>
<td>
<input type="radio" name="memory" id="memory1" value="2200">128 GB
<input type="radio" name="memory" id="memory2" value="1600">64 GB
<input type="radio" name="memory" id="memory3" value="500">32 GB
<input type="radio" name="memory" id="memory4" value="295">16 GB
<input type="radio" name="memory" id="memory5" value="0" checked="checked">8 GB
</td>
</tr>
<tr>
<td class='c1'>Hard Drive</td>
<td>
<input type="radio" name="hd" id="hd1" value="0" checked>320GB
<input type="radio" name="hd" id="hd2" value="100">500GB
<input type="radio" name="hd" id="hd3" value="200">1000GB
<input type="radio" name="hd" id="hd2" value="350">2000GB
</td>
</tr>
<tr>
<td class='c1'>CD/DVD Driver</td>
<td>
<input type="radio" name="cdr" id="cdr1" value="0" checked class="d_drive">DVD-ROM Drive
<input type="radio" name="cdr" id="cdr2" value="50" class="d_drive">DVD+/-RW Drive
</td>
</tr>
</table>
</div>
<div class='row outerbox'>
<h2>Contact Information</h2>
<table class='table'>
<tr>
<td class='c1'>First Name</td>
<td>
<input type="text" name="First" size="20" id='first' placeholder="First name">
</td>
</tr>
<tr>
<td class='c1'>Last Name</td>
<td>
<input type="text" name="Last" size="20" id='last' placeholder="Last name">
</td>
</tr>
<tr>
<td class='c1'>Phone #</td>
<td>
<input type="text" name="Phone" size="20" id='phone' placeholder="Phone">
</td>
</tr>
</table>
</div>
<h3>Special instructions/Comments:</h3>
<textarea name='comments' placeholder="If you have any comments or special instructions, please enter them here" class='form-control' rows='3'>
</textarea>
<p id='ctr'>
<input type="submit" value="Finalize sale" class='btn btn-primary'>
<input type="reset" value="Cancel" class='btn btn-danger'>
</p>
</form>
</body>
</html>
PHP
文件:
<?php
// Begin by making variables for the user's First, Last Name and Phone number
$first = $_GET['First'];
$last = $_GET['Last'];
$phone = $_GET['Phone'];
//Array Variables for each section
$OS = array('w7' => 'Windows 7', 'w7p' => 'Windows 7 Pro', 'w8' => 'Windows 8.1', 'u12' => 'Ubuntu 12.04');
$Proc = array('0' => 'Intel Core i3', '200' => 'Intel Core i5', '350' => 'Intel Core i7');
$Memory = array('2200' => '320 GB', '1600' => '64 GB', '500' => '32 GB', '295' => '16 GB', '0' =>
'8 GB');
$H_Drive = array('0' => '320 GB', '100' => '500 GB', '200' => '1000 GB', '350' => '2000 GB');
$C_Drive = array('0' => 'DVD-ROM Drive', '50' => 'DVD+/-RW Drive');
$Comments = $_GET['comments'];
//Connecting options
foreach ($OS as $cost => $name){
}
//Totaling Cost
$total = 0;
$salesTax = $total * .05;
$purchaseTotal = $total + $salesTax;
//Print out information
echo "Thank you" .$first. " " .$last. " for using Sam's online store. <br>\n";
echo "You have selected the following system: <br>\n";
echo "<strong>Base system</strong><br>\n";
echo "<strong>Operating system:</strong><br>\n";
echo "<strong>Processor:</strong> <br>\n";
echo "<strong>Memory:</strong> <br>\n";
echo "<strong>Hard drive:</strong> <br>\n";
echo "<strong>DVD Drive:</strong> <br>\n";
echo "<strong>Total:</strong>" .$total. "<br>\n";
echo "<strong>Sales tax(5%):</strong>" .$salesTax. "<br>\n";
echo "<strong>Total Amount:</strong>" .$purchaseTotal. "<br>\n";
echo "<strong>Comments or special instructors:</strong> <br>\n" .$Comments."<br>\n";
?>
以与名称相同的方式获取值
$osValue = $_GET['os'];
$procValue = $_GET['processor'];
$memoryValue = $_GET['memory'];
$hdValue = $_GET['hd'];
$cdrValue = $_GET['cdr'];
并使用它作为从关联数组调用的键
echo $OS[$osValue];
echo $Proc[$procValue];
echo $Memory[$memoryValue];
echo $H_Drive[$hdValue];
echo $C_Drive[$cdrValue];
您的 assignment1.php 文件,查看评论 - 已测试,有效。
<?php
/*pulling in variables from get.*/
$osValue = $_GET['os'];
$procValue = $_GET['processor'];
$memoryValue = $_GET['memory'];
$hdValue = $_GET['hd'];
$cdrValue = $_GET['cdr'];
$first = $_GET['First'];
$last = $_GET['Last'];
$phone = $_GET['Phone'];
//Array Variables for each section
$OS = array('w7' => 'Windows 7', 'w7p' => 'Windows 7 Pro', 'w8' => 'Windows 8.1', 'u12' => 'Ubuntu 12.04');
$Proc = array('0' => 'Intel Core i3', '200' => 'Intel Core i5', '350' => 'Intel Core i7');
$Memory = array('2200' => '320 GB', '1600' => '64 GB', '500' => '32 GB', '295' => '16 GB', '0' =>
'8 GB');
$H_Drive = array('0' => '320 GB', '100' => '500 GB', '200' => '1000 GB', '350' => '2000 GB');
$C_Drive = array('0' => 'DVD-ROM Drive', '50' => 'DVD+/-RW Drive');
$Comments = $_GET['comments'];
//Connecting options
foreach ($OS as $cost => $name){
//stuff you wanna do
}
//Totaling Cost
$total = 0;
$salesTax = $total * .05;
$purchaseTotal = $total + $salesTax;
//Print out information
/* ucwords(string) capitalizes the first letter */
echo "Thank you " .ucwords($first). " " .ucwords($last). " for using Sam's online store. <br>\n";
echo "You have selected the following options: <br>\n";
/* your values would go here */
echo "<strong>Operating system:</strong> $OS[$osValue]<br>\n";
echo "<strong>Processor:</strong> $Proc[$procValue]<br>\n";
echo "<strong>Memory:</strong> $Memory[$memoryValue]<br>\n";
echo "<strong>Hard drive:</strong> $H_Drive[$hdValue]<br>\n";
echo "<strong>DVD Drive:</strong> $C_Drive[$cdrValue]<br>\n";
echo "<strong>Total:</strong>" .$total. "<br>\n";
echo "<strong>Sales tax(5%):</strong>" .$salesTax. "<br>\n";
echo "<strong>Total Amount:</strong>" .$purchaseTotal. "<br>\n";
echo "<strong>Comments or special instructors:</strong> <br>\n" .$Comments."<br>\n";
?>
上面显示的@Conceptz 解决了我的所有问题,但总信息(涉及关键值)除外。
我以为我需要一个 foreach 来获取总值,但我真正需要做的是:
$base = 890;
$total = $base + $procValue + $memoryValue + $hdValue + $cdrValue;
这给了我总计的 $key 值。