1

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 :

5
  • create a readonly hidden variable. Add the value of $answer and post along with the form. Commented Apr 14, 2017 at 22:31
  • Just include the file and you can access all the variables in it include "file2.php" Commented Apr 14, 2017 at 22:50
  • You should include the other php file but be careful with variable names so it won't conflict with the main file, also $_post should be $_POST. Commented Apr 14, 2017 at 23:34
  • FYI, because of a variable name typo, not animal will never be echoed. Commented Apr 15, 2017 at 1:47
  • i fix that typo, then what should i do to get that value without go to file2.php page? Commented Apr 15, 2017 at 7:07

2 Answers 2

2

Just use requiere

<?php require 'file2.php'; ?>
Sign up to request clarification or add additional context in comments.

3 Comments

This is a low-quality answer because it doesn't say where to put the line (anything at all actually). It doesn't say to replace the current code or anything.
where i put that code coz i use the button to get the value from file2.php without go to file2.php page?
Ok, then integrate the php code in the form and change file2.php to action="<?php echo $_SERVER['PHP_SELF']; ?>"
1

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;

5 Comments

can you give me the example "path of file2.php"?
if your file2.php and the file1.php is in the same dir, then path of file2.php is "./file2.php" or "file2.php". You also can give it the absolute path of file2.php.
i cant use that for the button, can you share where i place that code?
add this <?php require_once("path of file2.php"); ?> to your file1.php. I cannot do any more, you need do your homework.
i know your mean sir thanks, but what i ask its get that value without go to page file2.php, sory bad english

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.