简体   繁体   中英

Pass a php variable in a script

I have the function below, how can I add my php variables id and fn instead of escape(window.location.href) ?

$mRow = $this->row;
$wRow = $mRow->getInfo(); 
$id= $mRow->id;
$fn = $mRow->first_name;

<script language="javascript">
  function fbshareCurrentPage()
    {window.open("https://www.facebook.com/sharer/sharer.php?u="+escape(window.location.href)+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
     return false; }
</script>

Assuming the below file is php file.

$mRow = $this->row;
$wRow = $mRow->getInfo(); 
$id= $mRow->id;
$fn = $mRow->first_name;

<script language="javascript">
  var id = '$id'; // Now you can use id directly in javascript
  var fn = '$fn'; //// Now you can use fn directly in javascript
  function fbshareCurrentPage()
    {window.open("https://www.facebook.com/sharer/sharer.php?u="+id+"&fn="+fn+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
     return false; }
</script>

You can assign value to javascript varibales and use them in script the way you want.

$mRow = $this->row;
$wRow = $mRow->getInfo(); 
$id= $mRow->id;
$fn = $mRow->first_name;

<script language="javascript">
  function fbshareCurrentPage()
    {window.open("https://www.facebook.com/sharer/sharer.php?u="+<?php echo $id; ?>+"&fn="+<?php echo $fn; ?>+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
     return false; }
</script>
<script language="javascript">
 var id= <?php echo json_encode($id); ?>;
 var fn = <?php echo json_encode($fn); ?>;

 function fbshareCurrentPage()
{window.open("https://www.facebook.com/sharer/sharer.php?u="+id+"/"+fn+"&t="+document.title, '', 
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600');
 return false; }

</script>

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