通过php变量的值设置输入字段的值 [英] set value of input field by php variable's value
本文介绍了通过php变量的值设置输入字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的php计算器,其代码是:
< html>
< head>
< title> PHP计算器< / title>
< / head>
< body bgcolor =orange>
< h1 align =center>这是PHP计算器< / h1>
< center>
< form method =postaction =phptest.php>
类型值1:< br>< input type =textname =value1>< br>
类型值2:< br>< input type =textname =value2>< br>
操作符:< br>< input type =textname =sign>< br>
结果:< br>< input typetextname =result>
< div align =center>
< input type =submitname =submitvalue =Submit>
< / div>
< / form>
< / center>
<?php
if(isset($ _ POST ['submit'])){
$ value1 = $ _ POST ['value1'];
$ value2 = $ _ POST ['value2'];
$ sign = $ _ POST ['sign'];
$ b $ if if($ value1 ==''){
echo< script> alert('请输入值1')< / script>;
exit();
}
if($ value2 ==''){
echo< script> alert('请输入值2')< / script>;
exit();
}
if($ sign =='+'){
echoYour answer is:,$ value1 + $ value2;
exit();
}
if($ sign ==' - '){
echoYour answer is:,$ value1- $ value2;
exit();
}
if($ sign =='*'){
echoYour answer is:,$ value1 * $ value2;
exit();
}
if($ sign =='/'){
echoYour answer is:,$ value1 / $ value2;
exit();
}
}
?>
我想要做的就是回答应显示在结果输入字段中,而不是单独回显。请帮忙?我知道这很简单,但我是PHP新手。 解决方案
一种方法是将所有php代码移到上面将结果复制到一个变量中,然后将结果添加到< input>
标记中。
试试这个 -
<?php
//将php添加到顶部。
if(isset($ _ POST ['submit']))
{
$ value1 = $ _ POST ['value1'];
$ value2 = $ _ POST ['value2'];
$ sign = $ _ POST ['sign'];
...
//添加到$ result变量
if($ sign ==' - '){
$ result = $ value1- $ value2;
}
//其余的代码...
}
?>
< html>
<! - 其余的标签...-->
结果:< br>< input typetextname =resultvalue =<?php echo(isset($ result))?$ result:'';?>>
I have a simple php calculator which code is:
<html>
<head>
<title>PHP calculator</title>
</head>
<body bgcolor="orange">
<h1 align="center">This is PHP Calculator</h1>
<center>
<form method="post" action="phptest.php">
Type Value 1:<br><input type="text" name="value1"><br>
Type value 2:<br><input type="text" name="value2"><br>
Operator:<br><input type="text" name="sign"><br>
Result:<br><input type"text" name="result">
<div align="center">
<input type="submit" name="submit" value="Submit">
</div>
</form>
</center>
<?php
if(isset($_POST['submit'])){
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
if($value1=='') {
echo "<script>alert('Please Enter Value 1')</script>";
exit();
}
if($value2=='') {
echo "<script>alert('Please Enter Value 2')</script>";
exit();
}
if($sign=='+') {
echo "Your answer is: " , $value1+$value2;
exit();
}
if($sign=='-') {
echo "Your answer is: " , $value1-$value2;
exit();
}
if($sign=='*') {
echo "Your answer is: " , $value1*$value2;
exit();
}
if($sign=='/') {
echo "Your answer is: " , $value1/$value2;
exit();
}
}
?>
All I want to do is that answer should be displayed in the result input field instead of echoing them separately. Please help? I Know it's simple but I am new in PHP.
解决方案
One way to do it will be to move all the php code above the HTML, copy the result to a variable and then add the result in the <input>
tag.
Try this -
<?php
//Adding the php to the top.
if(isset($_POST['submit']))
{
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
...
//Adding to $result variable
if($sign=='-') {
$result = $value1-$value2;
}
//Rest of your code...
}
?>
<html>
<!--Rest of your tags...-->
Result:<br><input type"text" name="result" value = "<?php echo (isset($result))?$result:'';?>">
这篇关于通过php变量的值设置输入字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文