0

I'm trying to call jsfunction() from php. This jsfunction() will redirect me to code.php. But, it's not working and not showing any error.

<?php
    $sid=$_GET['link'];
    echo '<script type="text/javascript">';
    echo  'jsfunction();';
    echo   '</script>';

?>

<script type="text/javascript"></script>
<script>
    function jsfunction()
    {
        var site_id="<?php echo $sid; ?>";
        window.location.href = "code.php?site_id=" + site_id; 
    }
</script>
  • 3
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – ChrisGPT was on strike Jun 30 '17 at 11:56
  • you cant call js from php. php runs on a server somewhere, and js in the client application (browser or other). – YvesLeBorg Jun 30 '17 at 11:56
  • Instead you can write that javascript function code in php itself. – Rahul Jun 30 '17 at 11:57
  • Possible duplicate of [How to call a JavaScript function from PHP?](https://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Alessandro Da Rugna Jun 30 '17 at 12:43

5 Answers5

1

As @Deljoei suggested you can redirect user using header. Coming to your question why this isn't working is because jsfunction(); which you are calling before which result in error, because jsFunction is not defined. If you will change your code to

<?php
$sid=$_GET['link'];
?>

<script type="text/javascript"></script>
<script>
    function jsfunction()
    {
        var site_id="<?php echo $sid; ?>";
        window.location.href = "code.php?site_id=" + site_id; 
    }
</script>
<?php
    echo '<script type="text/javascript">';
    echo  'jsfunction();';
    echo   '</script>';
?>

then you will achieve what you want do. Now you when jsFunction is called, it will not give error because it is defined earlier.

Th3
  • 124
  • 2
  • 9
0

You need to use php header for redirecting instead. Use the following code to redirect the user to the specific url :

header('location:code.php?site_id='.$sid);
exit();
0

You need to declare function before use, try this:

<?php
    $sid=12;

?>

<script type="text/javascript"></script>
<script>
    function jsfunction()
    {
        var site_id="<?php echo $sid; ?>";
        window.location.href = "code.php?site_id=" + site_id; 
    }
</script>

<script type="text/javascript">;
jsfunction();
</script>;
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

Do like this:

$sid=$_GET['link'];
if($sid){
header('location:code.php?site_id='.$sid);
exit();
}

I hope this helps!

Ingus
  • 1,026
  • 12
  • 34
0

Try this

<?php
    $sid=$_GET['link'];
    echo '<script type="text/javascript">';
    echo  'jsfunction('.$sid.');';
    echo   '</script>';

?>

<script type="text/javascript"></script>
<script>
    function jsfunction(id)
    {
        var site_id=id;
        window.location = "code.php?site_id=" + site_id; 
    }
</script>
Prabhakaran
  • 60
  • 2
  • 12