1

我想根据用户的选择将一个值添加到会话变量中。用户通过单击图像映射内的某些区域形状来进行选择。

这是我的代码。

<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>

这段代码的问题:它与你选择哪个无关,它总是将相同的值存储在会话变量中(最后一个“5M_PLATFORM_GUARDRAIL”)。

我究竟做错了什么?

4

2 回答 2

0

您正在将 php 代码(服务器代码)与 javascript(客户端代码)混合。服务器代码在页面呈现之前执行。所以你的最后一行 php 是被执行的。检查 html 源代码,您不会在其中看到 php 代码。

于 2012-10-18T17:01:11.470 回答
0

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

用这个替换你的 HTML 标记<map>

<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你应该插入这个块:

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

// session variable will be available from now 

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

于 2012-10-18T17:19:07.440 回答