Ajun Li
Ajun Li

Reputation: 13

How to get a PHP variable value from another PHP file?

how to take the value from another .php file help me fix this:

<html>
<head>
</head>
<body>
    <form name='answer' action="file2.php" method="post">
        <input type="text" name="what">
        <input type="submit" value="Login">
    </form>
    <div><?php echo $answer; ?></div>
</body>
</html>

And the code of file2.php file is as below:

<?php
    if ($_POST['what']==animal){
    $answer="dog";
}else{
    $answer= "not animal";
}

I would like to know how can I get the value of the variable $answer with button, then what should I put at the line :

Upvotes: 1

Views: 5555

Answers (2)

LF-DevJourney
LF-DevJourney

Reputation: 28564

Use require_once() to include file2.php in your php file, then in your file you can access the variables in file2.php.

require_once("path of file2.php");

echo $answer;

Upvotes: 1

Arturo DT
Arturo DT

Reputation: 60

Just use requiere

<?php require 'file2.php'; ?>

Upvotes: 2

Related Questions