2

I can't get the $_POST['name'] values sent from an html form on my php file. I've seen lots of similar questions but nothing helped. I have lot's of includes so I believe it's a scope issue, but I can't figure it out.

index.php

print_r($_POST); //Returns nothing, tried other ways too

//lot's of variables being defined

include 'sql_data_handlers.php';
//instantiating some things

sql_data_handlers.php

//some functions retrieving data from sql db and finally:
include($DOCUMENT_ROOT . "page.html");

page.html

//html stuff
<?php
//Some conditions
include($DOCUMENT_ROOT . "comment_form.html");
?>

comment_form.html

<form action="index.php" name="comment_form" id="comment_form" method="post">
    <input type="text" name="name" value="Anonymous" required><br>
    //lot's of inputs
    <input type="submit">
</form>

I used to have action="send_comment.php" but I realized it could be turned into a function so I ctrl+c and adapted send_comments.php to a function on sql_data_handlers.php. The problem is, now I can't get the $_POST values on index.php to use in the function on sql_data_handlers.php (which is included in index.php).

I would use action="my_php_function_from_data_handlers.php($args)" if it was possible, but I guess it isn't. btw, I already tried action="". This may seem pretty messy but this way I only need one .html for the site layout, pages are on the sql and the .php files do all the job.

Complete source of all files (pretty big, still working on it): http://pastebin.com/2nRuCpNx

Alex
  • 1,416
  • 4
  • 16
  • 42

3 Answers3

5

Make sure you don't have any kind of redirects, since you're using index.php to save data, you're probably reloading the page to show the updated comments. And if you're reloading, there'll be no data on $_POST to be printed by print_r().

Leonardo Arroyo
  • 573
  • 6
  • 16
  • That was it, the .php script was refreshing the page once the submit button was clicked so the $_POST variables were lost -- the array was in fact being generated, I just overlooked it. Thanks. – Alex Mar 13 '13 at 14:55
  • 1
    You saved my day. I had a rewrite rule that was sending all `http://my_domain.com` to `http://www.my_domain.com` and it lost the _POST data in the process. Maybe my comment helps someone who uses rewrite rules in .htaccess files. – DudeOnRock Mar 12 '16 at 02:53
  • @DudeOnRock Glad I could help :) – Leonardo Arroyo Aug 09 '16 at 20:35
0

Try changing the value of the input name attribute to something different from "name". Some configurations have issues with that, especially wordpress.

for example:

<input type="text" name="user_name" value="Anonymous" required>
Moshe Shaham
  • 15,448
  • 22
  • 74
  • 114
0

would you mind to change your

.html filename to .php filename

your codes runs in my local server because sometimes the difference lies in how your web server is configured.

when running in an web server it is best to use .php in your files

Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
  • I already did, the result is the same - I can't get $_POST values. – Alex Mar 13 '13 at 09:34
  • Yes. The $_POST requests were sent to send_comments.php before, I as I said. The prolem is index.php isn't getting them for some reason. – Alex Mar 13 '13 at 09:41
  • thats weird.. i try it and get results.. try to use different action instead of index.php – Jhonathan H. Mar 13 '13 at 09:59