如何在 PHP 阶乘程序中返回一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23501720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to return a value in PHP Factorial Program
提问by Bavanari
I am working on one of my assignment for Factorial Program. I am struck on one point.
我正在处理我的一项因子计划任务。我对一点印象深刻。
Fact.php
事实.php
<html>
<body>
<form action="FactCalc.php" method="post">
Enter a Number: <input type="text" name="number">
<input type="submit" Value="Calculate Factorial">
</form>
</body>
</html>
FactCalc.php
事实计算器
<?php
$num = $_POST["number"];
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>
this program is running fine. I want to return $factorial to Fact.php and display it there below the text box. I am kind of struck here, can you please help.
这个程序运行良好。我想将 $factorial 返回到 Fact.php 并将其显示在文本框下方。我在这里有点震惊,你能帮忙吗?
采纳答案by Jirka Kop?iva
<?php
if (isset($_POST["number"]))
{
$_POST["number"] = ($_POST["number"] * 1);
include "FactCalc.php";
}
?>
<form action="FactCalc.php" method="post">
<p>
Enter a Number:
<input type="text" name="number">
<input type="submit" Value="Calculate Factorial">
<?php
if (isset($factorial))
{
echo "</p><p>Factorial of $num is $factorial";
}
?>
</p>
</form>
回答by Pranay Bhardwaj
You can use AJAX for this but here is a simple way, hope you get an idea...
您可以为此使用 AJAX,但这里有一个简单的方法,希望您能有所了解...
<?php
$result="";
if(isset($_POST['ispost']) && $_POST['ispost']=="y"){
$num = $_POST["number"];
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
$result="Factorial of ".$num." is ".$factorial;
}
?>
<html>
<body>
<form action="" method="post">
<input type="hidden" name="ispost" value="y" />
Enter a Number: <input type="text" name="number" />
<input type="submit" value="Calculate Factorial /">
</form>
<?php print($result); ?>
</body>
</html>
回答by bittids
Here is the AJAX enabled, more eloquent solution. This answer anticipates potential conflict problems with the "$", and potential caching problems with jQuery.get or post
这是启用 AJAX 的更雄辩的解决方案。这个答案预测了“$”的潜在冲突问题,以及 jQuery.get 或 post 的潜在缓存问题
Fact.php:
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
</head>
<body>
<form action="" method="post">
<input type="hidden" name="ispost" value="y" />
Enter a Number: <input type="text" name="number" id="number" />
<input type="button" id="factorial_button" value="Calculate Factorial /">
</form>
<script>
jQuery(document).ready(function(){
jQuery("#factorial_button").click(function(){
var number = jQuery("#number").val();
jQuery.ajax({
url: "FactCalc.php",
type: "POST",
data: {
"number":number,
},
dataType : "html",
cache: false,
beforeSend: function () {
jQuery("#result").html("retrieving information...");
},
success: function( data ) {
jQuery("#result").html(data);
},
error: function( xhr, status, errorThrown ) {
jQuery("#result").html("Ajax error");
}
});
});
});
</script>
<div id="result"></div>
</body>
</html>
FactCalc.php:
事实计算器.php:
<?php
$num = $_POST["number"];
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>
回答by Ashish pathak
**try this very easy to use this factorial**
<?php
$fact=1;
for($i=5;$i>=1;$i--)
{
$fact=$fact*$i;
}
echo $fact;
?>
回答by Azouz Mohamadi
回答by ceorcham
You can do something like this
你可以做这样的事情
Fact.php
事实.php
<?php
if(isset($_POST["number"]))
{
$num = $_POST["number"];
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
}
else
$factorial = "";
?>
<html>
<body>
<form action="" method="post">
Enter a Number: <input type="text" name="number">
<input type="submit" Value="Calculate Factorial">
<?php
if($factorial)
echo "Factorial of $num is $factorial";
?>
</form>
</body>
</html>
You can't "share" the variable between FactCalc.php and Fact.php
您不能在 FactCalc.php 和 Fact.php 之间“共享”变量