繁体   English   中英

如何将javascript变量作为php参数发送

[英]how to send javascript variable as a php parameter

嗨,我有一个javascript函数,它会打开一个弹出窗口并打开一个php文件,但我什至要发送一个参数,但它不起作用,该参数将作为变量名而不是其值发送,这是我的脚本

             <?php 

 if($addflag == 0){
echo "
<script type=\"text/javascript\">
function mopen(){
var mobj=document.getElementById('dtype').value; //// this variable is shown as a name instead of its value
window.open('quotprin.php?vouchno=' . urlencode($getvouch) . '&dtype=mobj','popUpWindow','height=800,width=950,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}
</script>
";

echo "<td>";
echo '<font color="red">Print On Letter Head</font>
<input type="checkbox" id="dtype" name="dtype" value="1" checked="checked" />';
echo '<input class="cmdprint" type="button" id="submit" name="print" value="Print" onclick="mopen();"></td>';
echo "<td>";
}
            ?>

这不完全是您的原始代码,但是可以工作。 您有太多的转义斜杠和单引号或双引号。 找出这样的错误可能会令人困惑。

<?php 

$getvouch = isset($_GET['getvouch'])? $_GET['getvouch'] : null;

?>

    <script type="text/javascript">
    function mopen(){
       var mobj=document.getElementById('dtype').value;
       var url="quotprin.php?vouchno=<?php echo $getvouch; ?>&dtype="+ mobj;
       window.open(url,'popUpWindow','height=800,width=950,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
    }
    </script>

    <table><tr><td>
    <font color="red">Print On Letter Head</font>
    <input type="checkbox" id="dtype" name="dtype" value="1" checked="checked" />
    <input class="cmdprint" type="button" id="submit" name="print" value="Print" onclick="mopen();"></td>
    </tr></table>

这里最重要的是查看如何将“ getvouch”变量添加到getmobi url中。

如果您使用以下网址:add-a-javascript-var-and-send-it-with-post.php?getvouch = 6

获得手机链接到:quotprin.php?vouchno = 6&dtype = mobj

如果这不是问题,请稍作解释。

更新。 现在我看到的错误是您没有在URL中转义mobj变量。 尝试更改。

try this:      
<?php 

if($addflag == 0){
$getvouch = urlencode($getvouch);
echo "
<script type=\"text/javascript\">
function mopen(){
var mobj=document.getElementById('dtype').value; //// this variable is shown as a name     instead of its value
window.open('quotprin.php?vouchno=    {$getvouch}&dtype=mobj','popUpWindow','height=800,width=950,left=100,top=100,resizable=yes,    scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');
}
</script>
";

echo "<td>";
echo '<font color="red">Print On Letter Head</font>
<input type="checkbox" id="dtype" name="dtype" value="1" checked="checked" />';
echo '<input class="cmdprint" type="button" id="submit" name="print" value="Print"     onclick="mopen();"></td>';
echo "<td>";
}
          ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM