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

时间:2015-10-16 10:55:24

标签: javascript php forms submit

在下面的代码中,我生成了一个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'];
}
?>

5 个答案:

答案 0 :(得分:2)

您在此处将$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>

答案 1 :(得分:0)

尝试

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

答案 2 :(得分:0)

试试这段代码

 <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'];
    }
 ?> 

答案 3 :(得分:0)

您应始终在页面的开头使用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>

答案 4 :(得分:0)

你应该检查gsubfn('\\d+', ~sprintf('%02d', as.numeric(x)), sub('(\\D+)$', '\\11', myvar)) #[1] "VT01" "AK01" "AL02" "CA24" "NY12" "AZ06" "WY04" 变量初始化后是否有$_REQUEST['loops']

所以代码应该是

$loop