-3

The problem that I am having is PHP won't allow me to send the variable "$IsEdit" to another page. Here is the HTML & PHP for the two pages:

<form action="Form2.php" enctype="multipart/form-data" method="POST">
    <?php
        $Test=Test;
    ?>
    <input type="submit" value="Run test" name="submit">
</form>

On the next page...

<?php
    echo $_POST["Test"];
?>

This gives me the notice:

Notice: Undefined index: IsEdit in C:\xampp\htdocs\Form2.php on line 2

Can someone please explain how to set things so that the second page displays the line "Test", please?

chris85
  • 23,846
  • 7
  • 34
  • 51
Renma
  • 13
  • 3
  • 2
    If you'd done ANY Kind of basic debugging, like checking the "view source" of that page, you'd (hopefully) NOT see that php code. You have no form field named `test`, and a PHP variable definition is utterly meaningless to the browser. PHP runs on the server, HTML is "executed" on the client. Just because you defined a php variable on the server doesn't mean that the browser will magically create an `` field for you when that html reaches the client. – Marc B Oct 17 '16 at 21:15
  • There is nothing related to `IsEdit` in this code. `Test` should be in quotes since it is a string. – chris85 Oct 17 '16 at 21:18
  • Take a look at the tutorial PHP has, http://php.net/manual/en/tutorial.forms.php. Note how the elements are passed `name="name"`, `$_POST['name']`. Your `Form2.php` must have `$_POST[' IsEdit']`, not `$_POST['Test']`. So make an element named `IsEdit` and give it a value. – chris85 Oct 17 '16 at 21:27
  • 1
    and that `Test` in `$Test=Test;` is (treated as) a constant here. – Funk Forty Niner Oct 17 '16 at 22:05
  • The error does not support the question's code. – Funk Forty Niner Oct 17 '16 at 22:07

2 Answers2

2

Hi try using the hidden type input element it will work in that way.

<form action="Form2.php" enctype="multipart/form-data" method="POST">

<input type="hidden" value="<?php echo "your value here"; ?>" name="Test" id="Test">

<input type="submit" value="Run test" name="submit">
</form>

Then you can get the value of Test in the next page using

<?php
 echo $_POST["Test"];
?>
miyaneha
  • 309
  • 3
  • 3
-1

Use a hidden input field

<?php $test=Test; ?>
<input type="hidden" name="test" value="<?php echo $test;?>"/>

This is really a hack not a fix. The problem lies with how you're handling this form.

  • Interesting. Could you expand on the problem of how I've mishandled the form, please? – Renma Oct 17 '16 at 21:34
  • Just not very good to splice in php like I suggested. I would recommend using PHP only for your backend service and building out your front end with JS and HTML only, using AJAX to fetch data. Blending PHP and HTML makes the code harder to maintain in the long run. I learned that the hard way at my last job. – Lucas Desouza Oct 17 '16 at 21:39
  • 1
    Yours got DV's because in this statement `$test=Test` PHP will likely throw an error because 'Test' is not a string and PHP might try to interpret as a constant. In other words, your code is wrong. – Jay Blanchard Oct 18 '16 at 13:00
  • While the OP might have posted that code it is *your job* as someone providing an answer to suggest there are problems with code like that and determine what is going on before speculating. ¯\\_(ツ)_/¯ – Jay Blanchard Oct 19 '16 at 12:00
  • My point was that often people don't include all of their code. It could have been a constant that was defined earlier in the code and so NOT an error. – Lucas Desouza Oct 20 '16 at 15:41