繁体   English   中英

PHP变量值未显示在JS警报中

[英]PHP variable value not being displayed in JS alert

在以下代码中,我生成了一个php表单,该表单接受了用户正在处理的arp网络攻击的值。 但是,我试图显示在JavaScript警报中发送的“ $ loops”数据包的数量,当我运行代码时,警报中未显示数据包的数量,而且我不知道出了什么问题,所以如果有人可以通过我的代码,我会非常感激。

<form method ='post' action ="<?php echo $_SERVER['PHP_SELF'];?>">

<?php $loops = "";?>

<b> <p>Number of packets to be sent:</p>
<input type="text" name="loops" value="<?php echo $loops;?>">
<br><br></b>

<?php
$php_var = $loops;?>


<p style="text-align: center;">
<button onclick="myFunction()">Submit</button></p><br>  
<script>
    function myFunction() {
            var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
                    alert(js_var);
                        }
</script>
</font>
</form>


<?php
if($_SERVER["REQUEST_METHOD"]== "POST"){
if(isset($_POST["loops"])){
    $loops=$_REQUEST['loops'];
}
?>

您在此处将$php_var分配$php_var空白。

<?php $loops = "";?>

<?php $php_var = $loops;?>

因此,它没有显示任何价值。

我认为您需要在文本框中输入用户输入的值。

<input type="text" name="loops" value="<?php echo $loops;?>" id="loops"> // Observe the added `id` attribute.

您可以这样做:

<script>
function myFunction() {
  //var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
  var val = document.getElementById('loops').value;
  var js_var = "The number of packets sent is: " + val;
  alert(js_var);
}
</script>

尝试像

<?php $loops = "123";?>
    function myFunction() {
      var js_var = "The number of packets sent is: <?php echo $php_var; ?> ";
      alert(js_var);
    }

试试这个代码

 <form method ='post' action ="<?php echo $_SERVER['PHP_SELF'];?>">

          $loops = isset($_POST["loops"])?$_POST["loops"]:"";

         <b> <p>Number of packets to be sent:</p>
        <input type="text" name="loops" value="<?php echo $loops;?>">
        <br><br></b>

       <?php
       $php_var = $loops;?>


       <p style="text-align: center;">
         <button onclick="myFunction()">Submit</button></p><br>  
        <script>
function myFunction() {
        var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
                alert(js_var);
                    }
        </script>
     </font>
       </form>


      <?php
    if($_SERVER["REQUEST_METHOD"]== "POST"){
      if(isset($_POST["loops"])){
         $loops=$_REQUEST['loops'];
    }
 ?> 

您应该始终在页面开头使用PHP。 它是必需的,以便您可以获得HTML中可用的变量。

    <form method ='post' action ="<?php echo $_SERVER['PHP_SELF']; ?>">

    <?php $loops = ''; ?>
    <?php if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (isset($_POST["loops"])) {
                $loops = $_REQUEST['loops'];
            }
        }
        ?>
        <b>
            <p>Number of packets to be sent:</p>
            <input type="text" name="loops" value="<?php echo $loops; ?>">
            <br><br>
        </b>
        <?php $php_var = $loops; ?>
        <p style="text-align: center;">
            <button onclick="myFunction()">Submit</button>
        </p>
        <br>  
        <script>
            function myFunction() {
                var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
                alert(js_var);
            }
        </script>
</form>

您应该在$loop变量初始化后检查是否有任何$_REQUEST['loops']

所以代码应该是

<?php
    $loops = "";

    if($_SERVER["REQUEST_METHOD"]== "POST"){
        if(isset($_POST["loops"])){
            $loops=$_REQUEST['loops'];
        }
    }
?>

<form method ='post' action ="<?php echo $_SERVER['PHP_SELF'];?>">

<b> <p>Number of packets to be sent:</p>
<input type="text" name="loops" value="<?php echo $loops;?>">
<br><br></b>

<?php
$php_var = $loops;?>


<p style="text-align: center;">
<button onclick="myFunction()">Submit</button></p><br>  
<script>
    function myFunction() {
            var js_var = "The number of packets sent is: <?php echo $php_var; ?>";
                    alert(js_var);
                        }
</script>
</font>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM