drh47606 2018-12-05 20:59
浏览 107

如何使用ajax在url中保存变量

I have this Code, which is based in a File named trainerModuleView.php and i need the variable lastDeletedUser in a File named database.php

echo '<script>
    $.ajax({
        url: "trainerModulview.php?groupID="+$currentGroupID+"&lastDeletedUser="+$id,
        success: function(data) {

        },
    });
</script>';

I have a Group with students and if i delete one of them, i would like to have the variable in my URL, so i can $_GET['lastDeletedUser'] it later in the database.php. The problem is though, that the URL doesn't even change.

Pls halp Thanks in advance

  • 写回答

2条回答 默认 最新

  • doujizhong8352 2018-12-05 21:03
    关注

    You can always just end and start your PHP instead of echoing it. Also you can use <?=$VARIABLE?> notation to add a variable to any non-php code.

    ?>    
    <script>
        $.ajax({
            url: "trainerModulview.php?groupID="<?=$currentGroupID?>&lastDeletedUser=<?=$currentGroupID?>",
            success: function(data) {
    
            },
        });
    </script>
    <?php
    

    Also, Javascript and Jquery notation uses '+' to add a JS variable into a JS string.

    var variable=1;
    var yourstring="this the #"+variable+" string";
    

    While php uses '.'.

    $variable=1;
    $yourstring="this the #".$variable." string";
    

    Also, if you are using double quotes, you can just do, but it's better if you still use '.'.

    $yourstring="this the #$variable string";
    

    But for single quotes you need to use '.'.

    $yourstring='this the the #'.$variable.' string';
    

    Also, you can do this to make more clean instead of echo:

    <?php
    $phpstring='this is php';
    ?>
    <script>
     var jsstring='this is JS';
     var phpstring='<?=$phpstring?>';
     alert(phpstring);
    </script>
    <?php
    echo $phpstring;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题