javascript - 如何为 session 变量分配一个值,该值根据图像映射的选择而变化

标签 javascript html

我希望根据用户所做的选择向 session 变量添加一个值。用户通过单击图像映射内的某些区域形状来进行选择。

这是我的代码。

<a class="selection_5M_platform"><img src="../../images/Selection/Windmillselection-platform5M-fullview.gif" usemap="#Windmillselection_Map5M_platform" alt="windmill_platform6M" name="windmill_platform6M">
    <map class="map" name="Windmillselection_Map5M_platform">          
      <area shape="poly" coords="12,131,56,131,61,133,68,136,74,140,74,345,12,345" href="../submitreport.php" onClick="<?php $_POST['selectedreportlocation']="5M_PLATFORM_LADDERS" ?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-ladders.gif',1)" alt="ladders_platform5M">                   
      <area shape="poly" coords="73,344,73,141,69,136,59,132,48,131,48,124,267,123,267,132,229,135,202,142,194,151,195,344" href="../submitreport.php" onClick="<?php $_SESSION['selectedreportlocation']="5M_PLATFORM_FOUNDATION" ?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-foundation.gif',1)" alt="foundation_platform5M">
      <area shape="poly" coords="148,121,177,121,177,46,125,6,47,6,47,74,58,74,58,46,105,52,109,67,121,67,126,54,148,58" href="../submitreport.php" onClick="<?php $_SESSION['selectedreportlocation']="5M_PLATFORM_PALFINGER" ?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-palfinger.gif',1)" alt="palfinger_platform5M">
      <area shape="poly" coords="49,123,49,92,149,92,149,121,177,121,177,91,187,62,271,60,268,90,268,123" href="../submitreport.php"  onMouseOut="MM_swapImgRestore()" onClick="<?php $_SESSION['selectedreportlocation']="5M_PLATFORM_GUARDRAIL" ?>" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-guardrail.gif',1)" alt="guardrail_platform5M">          
    </map>        
</a>

此代码的问题:无论您做出哪种选择,它总是在 session 变量中存储相同的值(最后一个“5M_PLATFORM_GUARDRAIL”)。

我做错了什么?

最佳答案

为了让我的回答有帮助,我建议这个解决方案:

替换 <map> 的 HTML 标记与此:

<map class="map" name="Windmillselection_Map5M_platform">          
    <area shape="poly" coords="12,131,56,131,61,133,68,136,74,140,74,345,12,345" href="../submitreport.php?location=5M_PLATFORM_LADDERS" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-ladders.gif',1)" alt="ladders_platform5M">
    <area shape="poly" coords="73,344,73,141,69,136,59,132,48,131,48,124,267,123,267,132,229,135,202,142,194,151,195,344" href="../submitreport.php?location=5M_PLATFORM_FOUNDATION" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-foundation.gif',1)" alt="foundation_platform5M">
    <area shape="poly" coords="148,121,177,121,177,46,125,6,47,6,47,74,58,74,58,46,105,52,109,67,121,67,126,54,148,58" href="../submitreport.php?location=5M_PLATFORM_PALFINGER" ?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-palfinger.gif',1)" alt="palfinger_platform5M">
  <area shape="poly" coords="49,123,49,92,149,92,149,121,177,121,177,91,187,62,271,60,268,90,268,123" href="../submitreport.php?location=5M_PLATFORM_GUARDRAIL"  onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('windmill_platform6M','','../../images/Selection/Windmillselection-platform5M-guardrail.gif',1)" alt="guardrail_platform5M">          
</map>

在你的../submitreport.php中你应该插入这个 block :

session_start();
$_SESSION['selectedreportlocation'] = $_GET['location'];

// session variable will be available from now 

附注您完全误解了服务器端编程的原理。首先你必须阅读一些有关 PHP 的教程。

关于javascript - 如何为 session 变量分配一个值,该值根据图像映射的选择而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12959731/

相关文章:

javascript - 在javascript中将IST日期格式转换为YYYY-MM-DD

javascript - YouTube javascript API(带有 ASP)功能无法正常工作

javascript - 激活所选 JAVASCRIPT 的特定元素上的功能

html - 网站在手机上看起来不一样

javascript - 如何使用ui-router中的ui-sref将参数传递给 Controller

html - 让一个 div 响应地占据网页宽度的其余部分

javascript - 如何转义 ejs 模板字符串中的 ejs 标签?

带有种子的 Javascript 随机排序

jquery - 在同一区域切换两个 div 的最佳方法是什么

html - CSS 灵活布局 : flexible left column and variable fixed right column