将文本框1和2的总和放入文本框3中

时间:2015-07-09 14:35:12

标签: php

我有三个文本框,我想在文本框1和2中放置一个值,然后将它们一起添加到文本框中得到结果3.使用下面的代码我可以让它回显到屏幕上,但是如果我省略了回声,然后在T1和T2中放置数字后没有任何反应。 谁能告诉我我做错了什么。

<html>
<head>
    <title>My Page</title>
</head>
<body>
    <br>
    <form name="myform" action="textentry2.php" method="POST">
        <input type = "submit" name = "submit" value = "go">
        <input type = "text" name = "text1" >
        <input type = "text" name = "text2" >
        <input type = "text" name = "text3"  >

<?php

$text_entry = $_POST['text1'];
$text_entry2 = $_POST['text2'];
$text_entry3 = $_POST['text3'];

{
   $text_entry3 = ($text_entry + $text_entry2);
   echo ($text_entry3);
}

?>

5 个答案:

答案 0 :(得分:1)

对于字符串:

$text_entry3 = ($text_entry.$text_entry2);

在php中你将字符串与a组合在一起。他们之间。

For Integer:

$text_entry3 = ((int)$text_entry + (int)$text_entry2);

你需要在从你的角落获得弦乐时施放它们,并且它们不会在没有强制转换的情况下将它们加在一起。

放置它:

正如Aravona所说的那样: 您应该输出html进行所有计算。 Php是动态的。 HTML没有。因此,您需要将动态部分放入静态部分

在beginnin中创建<?php ... ?>,然后使用它来输出结果:

<input type = "text" name = "text3" value="<?=$text_entry3;?>" >

答案 1 :(得分:1)

  

在T1和T2中放置数字后没有任何反应

PHP是一种服务器端语言。你不能期望PHP能够实时行动......

输入值需要转到服务器,然后PHP Server可以在服务器上计算你的值,然后在刷新后,结果来......

如果你想编写一个实时计算器,你需要用javaScript编写它。绝对可以用AJAX或其他任何东西做到这一点,但javaScript是一种简单快捷的方法。

试试这个:

&#13;
&#13;
<html>
<head>
<title>My Page</title>
</head>
<body>
<br>
<form name="myform" action="textentry2.php" method="POST">
<input type="submit" name="submit" value = "go">
<input type="text" name="text1" onkeyup="calc()">
<input type="text" name="text2" onkeyup="calc()">
<input type="text" name="text3"    >
</form>
<script>
function calc()
  {
    var elm = document.forms["myform"];

    if (elm["text1"].value != "" && elm["text2"].value != "")
      {elm["text3"].value = parseInt(elm["text1"].value) + parseInt(elm["text2"].value);}
  }
</script>
</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我认为你的代码是这样的:

<?php
$text_entry1 = (int)$_POST['text1'];
$text_entry2 = (int)$_POST['text2'];
$text_entry3 = ($text_entry1 + $text_entry2);
?>
<html>
    <head>
        <title>My Page</title>
    </head>

    <body>
        <br>
        <form name="myform" action="textentry2.php" method="POST">
        <input type = "submit" name = "submit" value = "go">
        <input type = "text" name = "text1" value = "<?php echo $text_entry1 ?>" >
        <input type = "text" name = "text2" value = "<?php echo $text_entry2 ?>" >
        <input type = "text" name = "text3" value = "<?php echo $text_entry3 ?>" >
        </form>
    </body>
</html>

答案 3 :(得分:0)

首先将PHP放在HTML上方,以便定义值。 然后:

<input type = "text" name = "text3" 
  value="<?php echo ((int)$text_entry + (int)$text_entry2); ?>">

答案 4 :(得分:0)

我倾向于给那些不熟悉PHP的人提供一个非常好的提示,就是在任何HTML之前放置所有的PHP代码。这意味着在尝试向客户端发送任何内容之前,您将负责所有数据处理。其美妙之处在于,如果出现一些错误的情况,您可以简单地重定向或做任何必要的事情来为用户提供清晰且信息丰富的响应。而不只是倾倒一个半创建的页面,在它的中间有一个丑陋的错误(或几个)。

因此,您的PHP代码应如下所示:

<?php

// First make sure that the variable is defined, to prevent notices
// when trying to use it later.
$answer = '';

// If something has been posted (submitted).
if (!empty ($_POST)) {
    // Calculate the answer.
    // TODO: Add some validation to make sure that the values are indeed numbers.
    // See filter_var () in the PHP manual.
    $answer = $_POST['text1'] + $_POST['text2'];
}

?>
<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myform" action="textentry2.php" method="POST">
    <input type="submit" name="submit" value = "go">
    <input type="number" name="text1" >
    <input type="number" name="text2" >
    <input type="number" name="text3" value="<?php echo $answer; ?>">
</form>

通常我会在htmlspecialchars()上使用$answer来回复它,以防止XSS攻击。但是,由于它只回显一个数字,所以在这种情况下不需要。