2

I just installed XAMPP with PHP 7. I had a script that worked before, but after installing there is an error. I will show an example which is also not working. The problem is in $_POST, I think it can be because of configurations in XAMPP or PHP.

<?php 
echo "<form action='check.php' method=\"post\"> 
<input type=\"text\" name=\"name\" >
<input type=\"submit\" name=\"submit\" value=\"ok\">
</form>";
if (isset($_POST['submit'])){echo $_POST['name'];}
if (isset($_POST['name'])) var_dump($_POST['name']);
?>

This code doesn't return anything, but if I just add echo $_POST['name']; it returns error "Notice: Undefined index: name in D:\XAMPP\php\www\index.php on line 13". How can I fix it?

  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Qirel Feb 14 '16 at 12:26
  • try by removing `backslash(\)` not needed actually – Anant Kumar Singh Feb 14 '16 at 12:27
  • 1
    I removed and user 'name' instead \"name\". But nothing changed. @A-2-A – Shamil Omarov Feb 14 '16 at 12:30
  • Why don't you just write the form in pure HTML, then start PHP *after* the closing form tag? Makes for cleaner code. No need to use echo to paste that much HTML, can just exit PHP and go back in after. – Qirel Feb 14 '16 at 12:32
  • 1
    '
    ' @Qirel the same error.
    – Shamil Omarov Feb 14 '16 at 12:38
  • In your comment you used `form action='index.php'` but in your code `form action='check.php'`. Your code will work if and only if you will give same page name in`form action`. otherwise you will be redirect to next page and code will not work – Anant Kumar Singh Feb 14 '16 at 12:41
  • no I know, I just changed the name of PHP file. It is alright. @A-2-A – Shamil Omarov Feb 14 '16 at 12:45
  • @ShamilOmarov YOU FIND THE SOLUTION? – Anant Kumar Singh Feb 16 '16 at 08:08
  • @A-2-A Actually problem was not in code. All of this happened because I used PHP Storm and the problem was in interpereter I have chosen. Now I just use other editor and everything is allright :) – Shamil Omarov Feb 16 '16 at 22:02

2 Answers2

2

@hherger I deleted xampp server and installed wampserver with php 5.6. Now it shows another error.

Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

Warning: Cannot modify header information - headers already sent in Unknown on line 0

0

It seems that you have the code for all REQUEST_METHODs in the same script.
That's ok, but then you have to differentiate how you react:

  • either send out the form for the user to fill out and transmit,
  • or interprete the data transmitted by the form.

Try this code adopted from yours:

<?php
    // Check if the form has been transmitted or not
    if ($_SERVER['REQUEST_METHOD']=='POST') {
        // A form was transmitted
        if (isset($_POST['name'])) var_dump($_POST['name']);
    } else {
        // Send the form out so the user can transmit it
        echo "<form action='check.php' method=\"post\"> 
<input type=\"text\" name=\"name\" >
<input type=\"submit\" name=\"submit\" value=\"ok\">
</form>";
    }
?>
hherger
  • 1,642
  • 1
  • 9
  • 13