我需要创建两个输入框和一个提交按钮(我已经做过)。
现在的问题是如何通过点击触发功能的按钮来实现这一点,并告诉输入中的数字越大,例如
Number_A大于Number_B
PHP:
<?php
function myFunction() {
$x = array('demo');
$y = array('name');
$required = $x+$y;
foreach($required as $field) {
if (!is_numeric($_POST[$field])) {
echo 'is not numeric';
}
}
if ($x>$y) {
echo "number A is bigger then number B";
}
if ($x<$y) {
echo "number B is bigger then number A";
}
}
myFunction();
?>
HTML:
<form method="post">
<p>Number:</p>
<input name="demo" type="text">
<input name="name" type="text">
<button type="button">Submit!</button>
</form>
答案 0 :(得分:0)
您应该使用全局变量$_POST
来获取参数:
$x = $_post['demo'];
$y = $_post['name'];
你应该在你的变量和回音之间保持一致!
说
if ($a>$b) {
echo "number A is bigger then number B";
}
或
if ($x>$y) {
echo "number X is bigger then number Y";
}
而不是
if ($x>$y) {
echo "number A is bigger then number B";
}
例如
function compare($x, $y){
if($x==$y){
echo 'X equal Y';
}elseif($x>$y){
echo 'X > Y';
}elseif($x<$y){
echo 'X < Y';
}
}
答案 1 :(得分:0)
首先,您需要使用POST变量而不是数组。
此外,您的按钮需要submit
类型type="submit"
而不是button
- type="button"
<?php
if(isset($_POST['submit'])){
function myFunction() {
$x = (int)$_POST['demo']; // make sure it's an integer
$y = (int)$_POST['name']; // make sure it's an integer
if ($x>$y && is_numeric) {
echo "number A is bigger then number B";
}
elseif ($x<$y && is_numeric) {
echo "number B is bigger then number A";
}
else{
echo "Enter a number in each field.";
}
}
myFunction();
} // brace for if(isset($_POST['submit']))
?>
<form method="post">
<p>Number:</p>
<input name="demo" type="text">
<input name="name" type="text">
<input type="submit" name="submit" value="Submit!">
</form>
编辑:为防止XSS注入,您可以添加
action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"
如果您想添加操作。
附加选项:(检查两个数字是否相同)。
elseif ($x == $y && is_numeric) {
echo "Both are the same.";
}
答案 2 :(得分:-1)
你去,它应该工作。
Javascript解决方案(Asker在10分钟后更新了他的问题,添加了php / html)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Assignment 10 Form</title>
<script type="text/javascript">
function greaterNum(){
var value1;
var value2;
value1 = parseInt(document.getElementById('first_num').value);
value2 = parseInt(document.getElementById('last_num').value);
if (value1 > value2)
{
alert('Value 1 is greater than value 2');
document.body.style.background = "orange";
}
else if (value1 < value2)
{
alert('Value 2 is greater than value 1');
document.body.style.background = "orange";
}
}
</script>
<style type="text/css">
body{background-color: #40FF00;
margin-left: auto;
margin-right: auto;
width: 60%;
}
#container{
border: 2px solid yellow;
padding: 20px;
}
</style>
</head>
<body>
<h1>Assignment 10</h1>
<div id="container">
<div class="Num">
<form>
<label class="label1">Enter Value 1:</label>
<input type="text" name="first_num" id="first_num" value=" " />
<label class="label1">Enter Value 2:</label>
<input type="text" name="last_num" id="last_num" value=" " />
<br/>
<input type="button" value=" Which number is greater? " onclick="greaterNum();" />
</form>
</body>
</html>
PHP解决方案:
<?php
if (isset($_POST['demo']) && isset($_POST['demo']))
{
if ($_POST['demo'] > $_POST['name'])
{
echo '<p>The first number is greated than the second one</p>';
}
else if ($_POST['demo'] < $_POST['name'])
{
echo '<p>The second number is greated than the first one</p>';
}
else if ($_POST['demo'] = $_POST['name'])
{
echo '<p>The numbers are equal</p>';
}
}
?>
<form method="post">
<p>Number:</p>
<input name="demo" type="text">
<input name="name" type="text">
<button type="submit">Submit!</button>
</form>