0

So I have an HTML file that is a basic form. Say

<form method="POST" action="test1.php">
        Name: <input type="text" name="name" id="name" />
        <input type="submit" value="Submit" />
    </form>

Now I have that execute my test1.php file which goes as follows:

<?php
$name = $_POST['name'];
?>

So all it is doing is getting the value from the HTML form. Now I have a second PHP file test2.php that needs to get the value from the first test1.php file $name and output it via an echo statement.

I'm new to PHP and am fine with using one PHP file to output values from an HTML form, but I don't know how to approach a second one.

I'm aware that you can use the include statement to carry over the variables and their values, but it didn't seem to work in my instance. I'm almost positive the issue is that I don't have test2.php actually being executed. And I don't know how to approach that. Any help is appreciated.

EDIT: This is all I want test2.php to do. $name has to be the same value as retrieved from the HTML form in test1.php

    <?php
    echo $name;
    ?>
3
  • Show how you incorporated test2.php, and show the code in test2.php. Commented Mar 12, 2015 at 23:53
  • What sense is this supposed to make? Why not send your form to test2.php in the first place, and access the POST parameter in there directly …? Commented Mar 13, 2015 at 0:15
  • @CBroe it's a concept for an assignment, but we were conveniently never shown how to do something like this. Commented Mar 13, 2015 at 3:51

3 Answers 3

1

Just use include your file test2.php inside test1.php:

<?php
    $name = $_POST['name'];
    include('test2.php');
?>

Whenever you include a file using either include() or require(), the file always gets "executed", so maybe there's something else wrong in you code.

EDIT

test1.php:

<?php
    $name = $_POST['name'];
    include('test2.php');
?>

test2.php:

<?php
    echo $name;
?>
Sign up to request clarification or add additional context in comments.

7 Comments

So if my test2.php has an include('test1.php'); There won't be any issue with the two include statements?
If you include test1.php in test2.php and you also include test2.php i test1.php, you'll create an infinite include loop :) You have to do either of them, not them both.
Totally forgot about include_once! Thanks for bringing that up @mistapink :)
@DiegoGoesBauleo So how do I access the values of the $name variable in test1.php if I cannot include test1.php in test2.php?
@mistapink Where would I put the include once statement? In test1 or test2?
|
0

I would suggest starting a session in test1, assign the var $name to the session and then picking the session up on test2.

 test1.php
  session_start();
  $name = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
  $_SESSION['test1']['name'] = $name;


 test2.php
  session_start();
  $name = $_SESSION['test1']['name'];

Try that.

1 Comment

How do you propose test2.php gets executed, given this scenario, that is, the form is posted to test1.php?
0

@bloodyKnuckles, Your assignment was left open ended because there are a number of ways you can accomplish what you're looking to do.

 include('test2.php');

Is one option. Another would be

 header("Location: test2.php");
 exit();

Using the header option, $_SESSION variable would work. If test1.php had any HTML output, you could consider an AJAX call or urlencode() your string.

 test1.php
 echo '<a href="test2.php?name=', urlencode($name), '">Click Me</a>';

 test2.php
 $name = $_GET['name'];

I'm guessing your assignment task was designed to demonstrate various ways to pass values from script to script. Good Luck

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.