2个输入框1提交按钮| 最大最大值[关闭]

I need to create two input boxes and one submit button (which I already did).

Now the problem is how can I make it possible through clicking the button that a function gets triggered and tells the bigger number in the inputs e.g.

Number_A is bigger than 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>

Firstly, you need to use POST variables and not an array.

Also, your button needs to be submit type type="submit" and not a 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>

Edit: To prevent XSS injection, you can add
action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" if you want to add an action.


Additional option: (check if both numbers are the same).

elseif ($x == $y && is_numeric) {
      echo "Both are the same.";
}

Here you go, it should work.

Javascript solution (Asker updated his question 10 minutes later adding the 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 solution:

<?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>
  1. you should get the parameters using the global variable $_POST like this :

    $x = $_post['demo'];
    $y = $_post['name'];
    
  2. you should keep consistent between your variables and what you echo !

say

if ($a>$b) {
   echo "number A is bigger then number B";
}

or

if ($x>$y) {
   echo "number X is bigger then number Y";
}

instead of

if ($x>$y) {
   echo "number A is bigger then number B";
}
  1. if you define a function, make it re-usable

for exemple

function compare($x, $y){
    if($x==$y){
        echo 'X equal Y';
    }elseif($x>$y){
        echo 'X > Y';
    }elseif($x<$y){
        echo 'X < Y';
    }
}