【问题标题】:calculate using parsing variable使用解析变量计算
【发布时间】:2016-04-14 02:31:01
【问题描述】:

这是一个使用 php 变量和解析的计算器。我在 url 中为变量输入的内容与在表单中输入它们的内容相同。当我点击提交时,它应该计算。但是不知道哪里错(没有正确分配变量?)非常感谢!

这是我的问题: 1。我应该在哪里修复它才能正确计算? 2.如何通过在表单中​​输入来更改页面的实际表格填充和表格边框? 3. 如何使用从 URL 传递的变量更改表格的背景颜色?

<?php
// parse variables from URLs
echo "This is the value of <b>x:</b>  " . $_GET['x'] . ". This is the value of <b>y: </b> " . $_GET['y'] . ".This is the value of <b>z: </b>  " . $_GET['z'] . " , And the title is: " . $_GET['title'] . ".";
?>

<?
$result1 = $x + $y - 2*$z;
$result2 = 2*$x + 4*$y + (3*$z - 80);
$result3 = $result1 + $result2; 
?>

<center><h2> Math </h2></center>
<center> 
<table  cellpadding=20  border=20 bgcolor=#00ffff  >  
<tr>
<td><b>Mathematical Operation</b></td>
<td><b>Result</b></td>  
</tr>

<tr>
<td>x + y - 2 * z</td>
<td><?echo "$result1";?></td>
</tr>

<tr>
<td>2x - 4y + (3z - 80)</td>
<td><?echo "$result2";?></td>
</tr>

<tr>
<td>Row2 - Col2 + result of Row3 - Col2</td>
<td><?echo "$result3";?></td>
</tr>
</table> 
</center>

<form>
<u>Inputs:</u> <br />
X-value <input type="text" name="x" value=""></input><br />
Y-Value <input type="text" name="y" value=""></input><br />
Z-Value <input type="text" name="z" value=""></input><br />
Title <input type="text" name="title" value=""></input><br />
Table Padding <input type="text" name="pad" value=""></input><br />
Table Border <input type="text" name="brdr" value=""></input><br />
Background Color <input type="color" name="bckrd" value=""></input><br />
<input type="submit" name="submit" value="SUBMIT and CALCULATE"></input>
</form>

<?
if ($_GET['submit']) {  
$result1 = $x + $y - 2*$z;
$result2 = 2*$x + 4*$y + (3*$z - 80);
$result3 = $result1 + $result2;     
}
?>  

【问题讨论】:

    标签: php parsing variables calculator


    【解决方案1】:
    1. 您需要实际声明您正在使用的变量并为其赋值:

      $x = $_GET['x'];

      $y = $_GET['y'];

      $z = $_GET['z'];

      $result1 = $x + $y - 2*$z;

      $result2 = 2*$x + 4*$y + (3*$z - 80);

      $result3 = $result1 + $result2;

    2. 与上面的想法和表格的其余部分相同,这是一种方法。将此添加到您的表单中: Padding: <input type="text" name="tblpadding" value="1"></input><br /> Border width: <input type="text" name="tblwidth" value="1"></input><br /> Colour: <input type="text" name="tblcolour" value="#ccc"></input><br />

    然后,在您的 PHP 代码中:

    $tblPadding = $_GET['tblpadding'];
    $tblWidth = $_GET['tblwidth'];
    $tblColour = $_GET['tblcolour'];
    

    然后,将您的 &lt;table&gt; 声明更改为: &lt;table cellpadding="&lt;?php echo $tblPadding; ?&gt; border="&lt;?php echo $tblWidth; ?&gt;" bgcolor="&lt;?php echo $tblColour;"&gt;

    1. 请参阅#2 中的答案,我在其中添加了它。

    【讨论】:

    • 对不起,可怕的格式,无法弄清楚如何正确格式化列表项中的整个代码块,缩进似乎不起作用:/
    猜你喜欢
    • 2015-07-19
    • 2021-02-24
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2016-10-29
    相关资源
    最近更新 更多