繁体   English   中英

如何在PHP中单击提交后在文本框中获取结果(输出)?

[英]How to get the result(output) in text box after clicking submit in PHP?

我正在尝试用PHP计算一个数字的阶乘。 我有两个文本框:1)数字2)结果(数字的阶乘)

当我点击提交时,我希望结果在结果的文本框中。我怎么能实现它。

<html>
    <title>Factorial</title>
    <body>
    <form name="f1" method="POST">
    number :<input type="text" name="T1">
    factorial :<input type="text" name="T2" >
    <input type="submit" name="submit">
</body>
</html> 

<?php
if(isset($_POST['submit']))
{
 $num=$_POST['T1'];
 $fact=1;

 while($num>0)
 {
  $fact=$fact*$num;
   $num--;
  }

  //code for writing result to the textbox.

  }
  ?>

尝试这个:

<?php
if(isset($_POST['submit']))
{
 $num=$_POST['T1'];
 $fact=1;

 while($num>0)
 {
  $fact=$fact*$num;
   $num--;
 }

//code for writing result to the textbox.

 }
?>


<html>
<title>Factorial</title>
<body>
<form name="f1" method="POST">
number :<input type="text" name="T1" value="<?php isset($num) ? $num : ''?>">
factorial :<input type="text" name="T2" value="<?php isset($fact)? $fact : '';?>">
<input type="submit" name="submit">
</body>
</html>

在HTML之前做你的PHP,然后当你到达想要在页面上显示它的任何地方时,只需添加<?php echo $fact; ?> <?php echo $fact; ?>

也许是类似的东西

<div>
<?php echo $fact; ?>
</div>

在factorial输入中注意value="<?php echo $fact; ?>"

<?php
$fact ='';
if(isset($_POST['submit']))
{
 $num=$_POST['T1'];
 $fact=1;

 while($num>0)
 {
  $fact=$fact*$num;
   $num--;
  }

  }
  ?>
<html>
    <title>Factorial</title>
    <body>
    <form name="f1" method="POST">
    number :<input type="text" name="T1">
    factorial :<input type="text" name="T2" value="<?php echo $fact; ?>" >
    <input type="submit" name="submit">
</body>
</html> 

尝试这样使用jQuery调用一个方法来做你的动作

         function fact(){
         //do calculation
          $('#id_of_txt_box').val('the value');
          }

你可以试试javascript类似的东西

您只需在计算阶乘后添加脚本标记

尝试这个 :

echo '<script type="text/javascript">
var s = document.getElementById("t2");
        s.value = ' . $fact . ';
</script>';

“t2”是您希望放置计算值的输入元素的id。

在php标签中计算factorial后添加此代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM