【问题标题】:Change php variable with html button使用 html 按钮更改 php 变量
【发布时间】:2017-08-19 11:29:46
【问题描述】:

现在这是我的 html 按钮

<html>
<head>
</head>
<body>
<div>
<button type="button">change header</button>
</div
</body></html>

我想要的是当我点击它时改变变量

例子

<?php

$var= "path1";
$new= "path2";

if ( some one clicked the button) !== false) {
$var = $new;
header('Location: $var');

} else {
header('Location: $var');

}
?>

如果有使用javascript的方法,请发布

【问题讨论】:

标签: javascript


【解决方案1】:

使用 PHP,您可以尝试以下操作:

<form action="" method="post">
   <input type="hidden" value="t">
   <button>change header</button>   
</form>

添加帖子检查以查看页面是否已发布。

<?php

$var= "hello world";
$new= "i hate world";

if ( $_POST ) {
    $var= $new;
    echo $var;
} else {
    echo $var;
}

但我猜你真的在寻找一种 JS 的方式来做这件事。

【讨论】:

    【解决方案2】:

    <html>
    <head>
    </head>
    <body>
    <div>
    <button type="button" onClick="function()">change header</button>
    </div>
    </body></html>

    function(){
    $var= "hello world";
    $new= "i hate world";
    
    
        $var= $new;
        echo $var;
    
     echo $var;
    }
    PHP 是一种服务器端语言。您可以像这样使用javascript,或者如果您真的想使用php,您需要提交表单

    【讨论】:

    • 我不知道你想用这个来完成什么,但这绝对不是 JavaScript LOL。
    【解决方案3】:

    PHP 是服务器端,因此它在页面/html 发送给用户之前执行。任何用户交互都由浏览器中的 Javascript 处理。 如果您希望在用户单击按钮时更改内容,请使用 javascript 事件处理程序

    <html>
    <head>
    </head>
    <body>
    <div>
    <button type="button" onclick="changeStuff()">change header</button>
    </div>
    <script>
        function changeStuff(){
            //change header code
        }
    </script>
    </body></html>
    

    【讨论】:

    • 我应该用js还是phh?
    • 使用 javascript 来处理用户交互,例如按钮点击、悬停等。将 php 用于服务器端的东西,例如提交表单或将页面发送给用户之前
    【解决方案4】:

    reko beko@@ 看到这是百分百的工作

      <form action="" method="post">
           <input type="hidden" value="t">
           <input type="submit" value="change header" name="submit">
        </form>
    
    <?php
    
    $var= "hello world";
    $new= "i hate world";
    
    if (isset($_POST['submit']) ) {
        $var= $new;
        echo $var;
    } else {
        echo $var;
    }
    ?>
    

    【讨论】:

      【解决方案5】:
        <html>
                  <head>
              <script>
                    function  myfunction(){
               // no variable needed for this logic
      
                 //window.location.replace('the redirect path when button is clicked ')
                 // example 
                window.location.replace('newsletter_tb.php');
      
                  }
          </script>
                  </head>
                  <body>
                  <div>
      
                  <button type="button" onclick="myfunction()">change header</button>
                  </div
                  </body>
              </html>
      
      • 这个问题最好使用javascript

      • PHP 是服务器端脚本语言

      【讨论】:

      • 我应该使用js还是php?
      • @rekobeko 是的,如果我可以说的话。因为没有将其发布到数据库或将其处理到数据库。但另一方面,Javascript 非常适合这种示例。
      • 请不要使用&gt; 字符进行个人格式设置,this has been discussed on meta. - 我必须编辑它。
      【解决方案6】:
          <button type="button" id="chnageHeaderbtn">change header</button>
      

      输入脚本

      $("#chnageHeaderbtn").click(function() { 
      var prevUrlAddress='http://www.myfirsturl.com';
      var chnageUrlAddress='http://www.myurl.com';
      
      window.location.href =chnageUrlAddress;
      
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 2014-09-14
        • 1970-01-01
        • 1970-01-01
        • 2020-06-22
        相关资源
        最近更新 更多