dongyiyu882684 2018-09-18 16:28
浏览 84

将javascript变量从work.php页面传递到index.php页面的具体问题

Hey guys I have this page work.php with html and javascript codes and the form refers to index.php page. Also I am trying to pass the variable counter from my javascript to the index.php page. It's not working at all. Can you please have a look? I can't seem to figure out the issue.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">
<div id=parent_div>
<button  type="button" id="add_step" > Add step!</button>

<input type='submit' name='submit'>
<br/>
</form>
</div>
</body>
</html>
<script>


$(document).ready(function(e){


var counter=2;
function send_counter(counter) {
    $.ajax({
      url   : "index.php", 
      type  : "POST",
      cache : false,
      data  : {
        counter : counter
      }
    });
  }
});
</script>

And here is my index.php page:

<?php



if (isset($_POST['submit'])){
$counter=$_POST['counter'];
echo "$counter";

}
?>
  • 写回答

1条回答 默认 最新

  • dszsajhd237437 2018-09-18 16:37
    关注

    You are only passing the variables you mention in the data parameter

    function send_counter(counter) {
        $.ajax({
          url   : "index.php", 
          type  : "POST",
          cache : false,
          data  : { counter : counter }
        });
      }
    });
    

    So only counter so $_POST will only contain that one array occurance, $_POST['counter'].

    So change the PHP to look for that

    <?php
    if (isset($_POST['counter'])){
        $counter=$_POST['counter'];
        echo "$counter";
    }
    ?>
    

    Or you could add a submit to the data object.

    function send_counter(counter) {
        $.ajax({
          url   : "index.php", 
          type  : "POST",
          cache : false,
          data  : { counter : counter, submit: 1 }
        });
      }
    });
    

    and change the PHP back to

    <?php
    if (isset($_POST['submit'])){
        $counter=$_POST['counter'];
        echo "$counter";
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目