如何检索<a href..=""> to PHP variable when modal is involved

时间:2017-07-20 16:41:26

标签: javascript php html ajax

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 个答案:

答案 0 :(得分:1)

将onlclick事件添加到您的主播:

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

将javascript函数(使用jQuery或普通javascript)添加到同一页面或包含的js文件中:

jQuery函数:

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

仅限Javascript功能:

    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();
    }

添加一个名为&#39; setsession.php&#39;的php文件。 在文件中,您获得请求var并设置会话变量:

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

当然,您需要做一些验证和其他事情,但这取决于您。

<强>更新

将&amp; vname更改为?vname

答案 1 :(得分:0)

我看到的最佳选择是使用ajax并执行类btn的onclick,或者为它们提供与btn,mayble modalbtn不同的类名。然后将名称作为其数据参数传递并在那里设置会话。我真的没有看到只用php做到这一点的方法。

答案 2 :(得分:-1)

使用此代码

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

<script>
    $('.btn-large').click(function(){
        sessionStorage.city = "";
        var city = $(this) .attr('name');
        sessionStorage.city = city;
    });
</script>

现在这些数据存储在一个会话变量中,无论你想要访问这个变量的哪个页面都可以做到

sessionStorage.city

希望这会有所帮助