0
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>if else in php</title>
</head>

<body>

    <script>
        var a = 123;
        //to send the prompt value to php, reference taken from stack overflow
    </script>
    <?php
    $a = "<script>document.writeln(a)</script>";
    echo $a;
    ?>
</body>

</html>

in the code above, i fetched the value of javascript variable 'a' in PHP i successfully get it in the variable $a and i tried to print it then also it got printed successfully. As $a would be containing number '123' in string i tried to convert it into an integer using intval() function but, it always shows me value of $a as 0 when i echo it. the below code describes the same

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>if else in php</title>
</head>

<body>

    <script>
        var a = 123;
        //to send the prompt value to php, reference taken from stack overflow
    </script>
    <?php
    $a = "<script>document.writeln(a)</script>";
    $a = intval($a);
    echo $a;
    ?>
</body>

</html>
9
  • Your hole implementation is wrong. PHP and JavaScript do not share variables between each other, and please be more specific. what do you want to do exactly with it? Commented Dec 26, 2021 at 17:18
  • Actually i was learning php so even though php is server side scripting language, i wanted to take input from javascript and have to use it in php. by the way i was just learning if elseif ladder in php so i just wanted input from user and depending upon that input i wantto tell him if he can drive a car or not. Commented Dec 26, 2021 at 17:27
  • Php executes before JavaScript. The whole approach makes no sense, and the example is so confected and oversimplified it's impossible to tell what your real purpose is Commented Dec 26, 2021 at 17:32
  • If you want to take a variable from the browser and send it to a php script you need to send it via a http request - e.g. sending via Ajax, or via submitting a form Commented Dec 26, 2021 at 17:33
  • yes that's what i want to do but i was not knowing any other way for doing this, asa beginner in php i tried to use javascript variable value in php. Commented Dec 26, 2021 at 17:37

1 Answer 1

1

You have to submit it to the server to process it. Here a working example.

<?php

$canDrive = false;

// use GET to retrieve the value of the "age" query string

if(isset($_GET["age"]) && $_GET["age"] >= 18) $canDrive = true;



?>


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>if else in php</title>
</head>

<body>

    <!-- Here goes your code   -->
    <?php
    if ($canDrive) {
        echo "You can drive";
    } else {
        echo "You can't drive";
    }
    ?>


<form action="" method="get">
        <input type="text" name="age" id="">
        <input type="submit" value="submit">
    </form>

</body>

</html>

Remember, JavaScript and PHP can't communicate directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.