在html之间传递变量(php)

时间:2015-04-27 16:51:09

标签: javascript php html css

我正在尝试在表单中的action标签中传递一个参数,就像我在href标签中所做的那样,但是这不能正常工作我能知道为什么吗?或者我应该在表单中使用href标记,是否会覆盖表单中的操作? 这是我的代码:

class Month
  attr_reader :month, :year

  def initialize( month, year)
    @month = month
    @year = year
  end

  def month_names
      names_of_months = {1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'}
      return names_of_months[@month]
  end

    def length
      days_of_months  = {1 => '31', 2 => '28', 3 => '31', 4 => '30', 5 => '31', 6 => '30', 7 => '31', 8 => '31', 9 => '30', 10 => '31', 11 => '30', 12 => '31'}
      return days_of_months[@month]
  end


  def to_s
   output = "#{month_names} #{year} #{length}"
    (1.length).each do |day|
      output << day.to_s
    end
    output
  end
end

3 个答案:

答案 0 :(得分:1)

1)由于您已经基于条件创建链接,因此您可以直接在标记内回显该变量。 e.g。

 <?php echo $rep; ?>

2)您只需传递该脚本名称,而不是在表单操作中传递html。例如

  

newblocktryteacher.php?a = somename或newblocktrypupil.php?a = somename

基于这两点,您的代码将是

<!DOCTYPE html>
<html lang="en">
<?php 
   $ty=$_GET['param'];
   $name=$_GET['param1'];
   if($ty=='teacher')
   {
      $web = "<a href='teacherrepute.php?a=$name'>My repute score</a>"; 
      $rep = "<a href='teacherreported.php?a=$name'>My reported sites</a>";
      $blk = "newblocktryteacher.php?a=$name";
      $unblk = "<a href='newtryunblockteacher.php?a=$name>Unblock this site";
   }
   else
   {
      $web = "<a href='pupilrepute.php?a=$name'>My repute score</a>"; 
      $rep = "<a href='pupilreported.php?a=$name'>My reported sites</a>";
      $blk = "newblocktrypupil.php?a=$name";
      $unblk = "<a href='newtryunblockpupil.php?a=$name>Unblock this site</a>";
   }
   // $type=$_GET['param2'];
   $courseA='A';
   $courseB='B';
?>
<body>

   <?php echo $rep; ?>
   <form action="<?php echo $blk; ?>" method="POST">
      Block : <input type="text" name="url" /></br>
      <br>
      <input type="submit" value="block" />   
      <br>
   </form>
</body>
</html>

答案 1 :(得分:0)

您的$blk不是有效的操作表单,因为它是<a>标记(链接)。因此,$blk应该是newblocktrypupil.php?a=$name'

答案 2 :(得分:0)

您的$blk格式不适合<form>

$blk = "<a href='newblocktryteacher.php?a=$name'>Block this site</a>";
<FORM action = <?php echo $blk; ?>  method ="POST";>

假设此示例中$namebob

<FORM action = <a href='newblocktryteacher.php?a=bob'>Block this site</a>  method ="POST";>

如您所见,这是不正确的。您只需要URL本身。另外,请在"POST"之后删除分号。