试图在onclick中插入PHP变量,以便设置隐藏变量


trying to insert php variable in onclick in order to set the hidden variable

下面是Javascript中有一些语法错误的小代码。我试图在onclick插入PHP变量,以设置隐藏变量ie。, $xyz

我没有得到值为我的隐藏变量ie。,美国广播公司(abc)

请告诉我印刷中哪里出错了。

这里

:

 <form action="/go" method=post>
   <input type=hidden name='abc' >
<?php
            if(someCondition) {
           print "<input type='"submit'" value='"My Button'" onclick='" document.getElementsByName('abc').value = ''<?php echo $xyz ?>'' '" />"; //where $xyz having some value in it.
        } 
?>
</form>
编辑:

这是类型错误之前,现在我编辑为"getElementsByName",即使这样,我仍然没有得到值为我的隐藏变量ie。,美国广播公司(abc)

  1. JavaScript是区分大小写的,getelementsbyname应该是getElementsByName
  2. 这个getElementsByName函数返回一个集合,所以你不能对返回值做.value赋值。
  3. <?php ?>标签里面有一个<?php ?>标签。

编辑:下面是应该工作的代码:

<form action="/go" method="post">
   <input type="hidden" name="abc">
   <?php
      if (someCondition) {
        echo '<input type="submit" value="My Button" onclick="document.getElementsByName(''abc'')[0].value = '''.$xyz.'''" />';
      } 
    ?>
</form>