与PHP斗争

时间:2018-10-29 00:01:26

标签: javascript php

我正在尝试使用php创建带有表单的相对简单的网页。我对此进行了简化,因此可能更容易解释。我在php中有变量,如下所示:

class Something():
    def __init__(self,input1, input2, input3):
        self.input1 = input1
        self.input2 = input2
        self.input3 = input3

    def function1(self, input4, input5, input6):
        something = (self.input1 +  self.input2 + self.input3)+input4+input5+input6
        return something

    def function2(self, input7, input8, res1):
        something2 = res1+input7+input8
        return something2

a = Something(1,2,3)

# just save the result of function1 and pass it to function2
res1 = a.function1(4,5,6)
print(res1)
print(a.function2(7, 8, res1))

# or just call function1 in the arg list of function2
print(a.function2(7, 8, a.function1(4,5,6)))

如您所见,我调用了一个用JavaScript编写的函数,其定义如下:

<?php
$thing =(isset($_GET['thing'])) ? $_GET['thing']:"";
echo "<td><button onclick = theFunc()>Confirm</td>\n";
?>

我在线获得了函数中的第二行。现在,当我单击“提交”按钮时,我将其带到一个新的php页面,在这里我只要求它暂时在变量中回显值。在下面:

function theFunc(){
        window.location = "www.url.com?thing=$thing";
        var thing = "<?php echo $thing; ?>";
}

尽管我似乎无法真正获得价值。我是不是非常愚蠢并忘记了过渡的关键部分?它输出的只是'$ thing',而不是值。任何帮助是极大的赞赏。

3 个答案:

答案 0 :(得分:0)

这是用PHP处理的非常基本的内容:

某些页面,例如index.php

<form action="processform.php" method="GET">
  <input name="thing">
  <input type="submit" value="Confirm">
</form>

现在您需要processform.php

<?php
  $thing = $_GET["thing"];
  echo $thing;
?>

答案 1 :(得分:0)

首先,请完全在功能中编写代码

$thing 
网址中的

无效。

 function theFunc(){
    window.location = "www.url.com?thing=<?php echo $thing; ?>";
    var thing = "<?php echo $thing; ?>";
  }

答案 2 :(得分:-1)

就在您的代码中

<?php
    $thing = $_GET["thing"];
    echo "$thing";
?>

应为echo $thing;(即不带引号),否则它将以字符串形式回显“ $ thing”,这是您报告的错误。