duanpu5048 2015-10-26 06:04
浏览 43

Ajax / JS将变量传递给PHP并保存成功[重复]

This question already has an answer here:

The title of this question is probably misleading, I didn't know a good way to consicely ask what I need help with.

So basically, I use the following ajax to send a javacript variable on index.php to a separate php file (page2.php)

var Variable1 = '1';
$.ajax({
        type: "POST",
        url: 'page2.php',
        data: "NewVariable=" + Variable1,
        success: function() {
              ...Save or echo the value of $newervariable here ....
        }
});

So basically above I am sending the variable Variabl1 to page2.php . Page2.php looks something like this:

<?php
     if(isset($_POST['NewVariable'])) {
          $NewVariable = $_POST['NewVariable'];
          $NewerVariable = $NewVariable + 1;
}

?>

I know the example is kind of obsolete because you could just do add 1 using javascript without having the 2nd php page, but I just simplified it down really it boils down to me needing the 2nd php page, and also knowing how to save the values of it after success (if it's even possible)

</div>
  • 写回答

2条回答 默认 最新

  • douyun8885 2015-10-26 06:10
    关注

    On JS

    success: function(data) {
              Variable1=data;
            }
    

    On PHP:

    <?php
         if(isset($_POST['NewVariable'])) {
              $NewVariable = $_POST['NewVariable'];
              $NewerVariable = $NewVariable + 1;
              echo $NewerVariable;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分