简体   繁体   中英

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

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

If you use this url: add-a-javascript-var-and-send-it-with-post.php?getvouch=6

get mobi links to : 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. 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>";
}
          ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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