简体   繁体   English

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

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

hi i have a javascript function which opens a pop-up window and opens a php file into it but i even want to send a parameter with it but it doesnt work the parameter is sent as a variable name instead of its value here is my script 嗨,我有一个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>";
}
            ?>

This is not exactly your original code, but it works. 这不完全是您的原始代码,但是可以工作。 You have too many escape slashes and single or double quotes. 您有太多的转义斜杠和单引号或双引号。 It can be too confusing to figure out errors like this. 找出这样的错误可能会令人困惑。

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

Most important here is to see how the "getvouch" variable is added to the getmobi url. 这里最重要的是查看如何将“ getvouch”变量添加到getmobi url中。

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

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

If this is not the issue, please explain a little better. 如果这不是问题,请稍作解释。

UPDATE. 更新。 Now I see the error is you did not escape the mobj variable in the url. 现在我看到的错误是您没有在URL中转义mobj变量。 try the changes. 尝试更改。

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