dongliao3450 2017-07-20 16:41
浏览 80
已采纳

如何在涉及模态时将<a href..>的值/名称检索到PHP变量

I have a modal that is common to all the href links, but I need to pass the value of the href so that I can set a PHP session variable to further use info of the href clicked.

For example: If I click href Tokyo, I need to set a session variable 'prefecture' to 'Tokyo', if I click 'Kanagawa', 'prefecture' = 'Kanagawa'

I cannot use GET as I am calling a modal that is common to all the href links, or may be I am unaware of an efficient method. Here is the code,

HREF LINKS

<section id="area">
            <div class="container inner">
                <div class="train-line">
                    <div class="row">
                        <h2> Kanto Area </h2>
                        <a href="#modal-work03" data-toggle="modal" name="tokyo" id="1" class="btn btn-large">Tokyo</a>
                        <a href="#modal-work03" data-toggle="modal" name="kanagawa" id="2" class="btn btn-large">Kanagawa</a>
                        <a href="#modal-work03" data-toggle="modal" name="chiba" id="3" class="btn btn-large">Chiba</a>
                        <a href="#modal-work03" data-toggle="modal" name="saitama" id="4" class="btn btn-large">Saitama</a>
                        <a href="#modal-work03" data-toggle="modal" name="ibaraki" id="5" class="btn btn-large">Ibaraki</a>                           
                    </div>
                </div>
            </div>
</section>

MODAL

    <div class="modal fade" id="modal-work03" tabindex="-1" role="dialog" aria-labelledby="modal-work03" aria-hidden="true">

. . .

Please advice on how to set a PHP session variable when a particular href link is clicked, so that I can use that info on other PHP pages.TIA

  • 写回答

3条回答 默认 最新

  • duanluan3651 2017-07-21 17:27
    关注

    Add the onlclick event to your anchors:

    <a href="#" data-toggle="modal" name="tokyo" id="1" class="btn btn-large"  onclick="setSessionVariable('Tokyo');return true;">Tokyo</a>
    

    Add a javascript function (use jQuery or plain javascript) to the same page or in your included js file:

    jQuery function:

    function setSessionVariable(varname) {
        $.ajax({url: "setsession.php?vname="+varname, success: function(result){
            alert("ajax function success: " + result);
        }});
    

    Javascript only function:

        function setSessionVariable(varname) {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                   document.getElementById("demo").innerHTML = this.responseText;
                }
            };
            xhttp.open("GET", "setsession.php?vname="+varname, true);
            xhttp.send();
        }
    

    Add a php file called 'setsession.php'. In the file you get the request var and set the session variable:

    <?php 
    session_start();
    $_SESSION['prefecture'] = $_REQUEST['vname'];
    echo("success");
    exit(0);
    ?>
    

    Of course you will want to do some validation and other stuff but that is up to you.

    UPDATE

    Changed &vname to ?vname

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)