Php 2个输入框1个提交按钮|最小-最大

Php 2个输入框1个提交按钮|最小-最大,php,forms,Php,Forms,我需要创建两个输入框和一个提交按钮,我已经这样做了 现在的问题是,我如何通过单击按钮使函数触发并告知输入中较大的数字成为可能,例如 数字A大于数字B php: HTML: 给你,应该可以用了 Javascript解决方案询问者在10分钟后更新了他的问题,添加了php/html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.d

我需要创建两个输入框和一个提交按钮,我已经这样做了

现在的问题是,我如何通过单击按钮使函数触发并告知输入中较大的数字成为可能,例如

数字A大于数字B

php:

HTML:


给你,应该可以用了

Javascript解决方案询问者在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>
您应该使用全局变量$\u POST获取参数,如下所示:

$x = $_post['demo'];
$y = $_post['name'];
你应该在你的变量和你的回应之间保持一致

而不是

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';
    }
}

首先,您需要使用POST变量,而不是数组

此外,您的按钮需要是submit type=submit,而不是button-type=button


到目前为止你试过什么?我的意思是这真的很基本,询问答案无助于你的学习,而且这闻起来像是家庭作业。同意@user574632,请给我们看看代码@用户574632我知道,但我真的找不到解决方法。。。我试着用If和else来做,但没有成功worked@Timothy好的,那么请展示你尝试过但没有成功的,然后你会得到帮助。只要问答案,你就不会知道一个数字是否比php大——见结果2和3。2 3从那里继续你的搜索和/或向我们展示你的尝试。这根本不使用PHP,这正是原始海报所使用的。请尝试使用与原文相同的语言,除非这是不可能的。他在10分钟后编辑了他的文章,添加了php+html。不是我的问题,非常感谢!它是有效的:@Timothy,非常欢迎。我认为在这种情况下不需要form-action属性。@TraianTatic-True。我在后面加上了这个。我认为这会有助于防止XSS注入。我对它的原样做了一个编辑,并在作品中做了一个记录。
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';
    }
}
<?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>
elseif ($x == $y && is_numeric) {
      echo "Both are the same.";
}