I'm working a web application with PHP.
Something wrong is happening that I have never seen before. $_GET is working well, but $_POST does not work exactly. Imagine the form below:
<form action="process.php" method="post">
<input type="text" name="title" />
<input type="submit" value="send" />
</form>
As you see, I've used post for method attribute of the form. In this case, the code below will return error:
<?php
$sentData = $_POST['title'];
echo($sentData);
?>
Error message:
PHP Notice: Undefined index: title in ...
But If I had used $_GET in php scripts and get in the html form codes, everything would work without any error.
There are something more strange.
There are just one form that returns no error while I'm using POST, other forms return error.
When I run this application locally (with Xampp - Apache 2.2) everything works fine without any error, but whenever I run the application an the remote server (IIS 7), I get these errors and problems.
so i would approach this in a couple of different ways:
#Dynamicus is correct, this is only a 'Notice' and not a fatal error (at least you didn't say so in the question), so this may be a difference in your .ini config or with a config definition to suppress errors like so 'error_reporting(0);'
You may want to make a back up of your current running .ini on your local and on the server and copy the working one over to your server and restart Apache and see if that makes a difference.
Why the indexed array (ie $_POST) is producing a warning is somewhat bizarre, but do a "print_r($_POST);" or a "var_dump($_POST);" to see the contents or if anything is off.
if you're using a framework or something that does routing, this could be something to look into.
i hope this helps and there are more settings for error reporting [here] http://php.net/manual/en/function.error-reporting.php
Your input element needs both a name and value property.
Finally I changed the server that my files were located on it. I tried them on another server (same OS), and everything worked fine.
I could not get what was the reason... This was the first time that I was getting this unknown error.
However, thank you all for your suggestions and comments.
Related
This is the most weird PHP issue I ever had. Several minutes ago I noticed that my script stopped working properly. To keep it short, after debugging I found that if any variable in POST form has < symbol at the start, PHP doesn't process it anymore. This is super-strange because my code worked for years. Now, this issue happens not only on localhost, but also on 2 different servers (just installed the script there to test). So the issue can't be related to PHP config in any way.
This is the actual code I use to get all variables submitted via POST form:
if (isset($_POST)) {$form_array=$_POST;} //super variable with all form variables
Then I "extract" array values to create actual variables with correct values. But it just stopped working now. I added extra line of code to debug submitted variables:
print_r($form_array);
If I enter something into form and submit it, result is:
Array ( [var1] => something [submit_ok] =>)
However, if I enter <something, the result is:
Array ( [submit_ok] => )
The variable doesn't even exist! If I enter something<, it starts working again. However, If I enter something<here, it doesn't work again. Put simply, if any value in form contains < symbol followed by any letter, variable doesn't even exist. What the hell?
P.S. Adding HTML code of the form (this is COMPLETE code):
<form action="test.php" method="post">
<input type="text" name="var1">
<input type="submit" name="submit_ok" value="do">
</form>
I, assuming you're testing this in a browser, think it's just the browser interpreting the < symbol as the beginning of a HTML tag and then trying to render it, which fails, because it doesn't know what to do with the <something> tag.
If this is what you're seeing in the browser:
But after pressing CTRL+U (in Chrome) you're seeing:
Then it's just a rendering "problem" and my calculations were correct.
Consider adding this in your PHP files, as it tells the browser not to treat the output as HTML but rather as plain text:
<?php
// This must be called before _any_ other output is sent to the client.
header('Content-type: text/plain');
I have no logical explanation, but once I rebooted all the devices (router, server, computer) everything came back to the normal state. I noticed that after computer reboot, Firefox was updated (so maybe that update caused the issue somehow, no idea).
Anyways, everything is working normally again without modifying a single line of code.
The less than gets URL encoded as "%3C" so printing it won't work since browsers see it as a broken HTML tag, but if you...
$var1=urldecode ($_POST['var1']);
Before using $var1 in a database query, it should work. To actually print it, you could do
echo html_entity_decode(urldecode($var1));
I'm trying to upload a file to my server or just open it and handle the contents but I get this strange error and I don't know why.
HTML form: http://pastebin.com/cSfMmvKF
php script: http://pastebin.com/fPzRVy5Y
If I try to test if the file is received I get a false response. Example:
if (isset($_FILES['epifile']))
echo 'Is set';
This evaluates as false. I'm pretty sure everything is quite correct so I have no idea where the error comes from. Can you guys help?
Edit: I'm running this with XAMPP on my own PC. Could it be some setting that I need to enable within Apache?
Your form must have an enctype="multipart/form-data" attribute.
I got a file upload form using enctype=multipart/form-data, but my $_FILES array is always empty. max_upload_size is big enough (2MB, I'm testing with 100kb files). the exactly same code is working on other hosts, so I think something is wrong with my webhoster's php settings. does anyone know what I could check for? when I ask them they always tell me it can't be their fault -_-
EDIT:
You asked for code, I didn't post it because it's pretty complicated (a Shopware plugin), but I think I can extract the important parts:
HTML (Smarty):
<form method="post" action="{url action=saveRegister}" enctype="multipart/form-data">
PHP (inside my event handler):
file_put_contents('ul.log',serialize($_FILES));
Result (var_dumped unserialized):
array(0) {}
Full Form HTML: http://pastebin.com/Wq1yBTux
Found the error: some "security feature" of the framework "cleaned up" $_POST, so move_uploaded_file couldn't check if the file was uploaded correctly
Install Firebug (see network tab) or Livehttpheaders to see if your file is actually sent to the server and if the HTTP headers are correctly send by the browser.
They might be wrong for any reason (Content-Type not allowed, POST method not allowed)
It should give you data to look at and configuration parameters to check.
The problem does not happen using the home WAMP server. I have tried two remote hosts with the same results.
The textareas' hold some code that can be edited and later used on the website.
Eg:
Correct data in textarea:
<form action="login.php" method="post">
After first posting to PHP and SQLite database:
<form action=\"login.php\" method=\"post\">
And after two posts:
<form action=\\\"login.php\\\" method=\\\"post\\\">
So when I go to use this changed code, it does not work because of all the .
Note: It is not only " but also any '
I have checked my PHP and SQLite code and can't find any errors, and because it only happens on remote hosts.
I tried googling but nothing came up. Might be googling this wrong.
Is there a way to fix this or bypass it?
The remote host probably returns true if you call get_magic_quotes_gpc(). See http://de3.php.net/manual/en/function.get-magic-quotes-gpc.php
So forgive this dumb question. I'm a music teacher, not a programmer.
I am submitting a form. I have possibly checked checkboxes for "spoken languages".
<input type="checkbox" id="speakEnglish" name="speakEnglish" value="yes" />
<input type="checkbox" id="speakGerman" name="speakGerman" value="yes" />
And then on form submit I have
$speakEnglish = $_POST['speakEnglish'];
$speakGerman = $_POST['speakGerman'];
And formulate my SQL query from that. It was submitting and INSERTing fine on my local host, but since uploading to a server I'm getting this when a checkbox is empty...
AN error occurred in the script '/home/berli/public_html/TEST/signup.php' on line 295:
Undefined index: speakGerman
This confuses me .. though I understand whats going on.
But why was my form submitting with empty checkboxes before, and now it throws an error?
What is the normal way to handle this?
Something like..?
if(isset($_POST[checkboxVar]) { $SQLcheckboxVar = $_POST[checkboxVar]} };
I just don't understand why empty checkboxes didn't hang me up on local host, but now they do. I cant think of anything else I could have done to cause that and I certainly wasn't testing for the checkbox variables before... but I have a SQL table full of INSERTed entries.
Is there some kind of difference on how this would work locally vs on a server?
Your servers error reporting must show notices/warnings (not sure specifically which one that is).
You are correct in saying that if(isset($_POST['checkboxVar'])) is all you would need to hide that error if you can't change your php configuration (php.ini). This is what you need on your online server (usually /etc/php.ini):
;Show all errors, except for notices and coding standards warnings
error_reporting = E_ALL & ~E_NOTICE
Your local configuration is probably using the default php settings, which are suppressing any Notices. The production server has a stricter error reporting which lets you know of tiny (most probably not important) problems.
In this case you are trying to access a variable which is not defined, as the browser is NOT sending any data for checkboxes that have not been selected.
As already pointed out above, a simple if(isset($_POST[checkboxVar])) will do the trick to get rid of the notice on per-variable level.
In case you wish to stay a music teacher and not break your head against the wall every time these things pop up it may be easier to simply put the following as the first line of your program:
error_reporting(E_ALL & ~E_NOTICE);
The simplest way to avoid this Notice is to check if your Checkbox has been checked with something like:
if (!isset($_POST['checkboxVar'])) $_POST['checkboxVar'] = 'no';
or
$checkboxVar = isset($_POST['checkboxVar']) ? $_POST['checkboxVar'] : 'no';
The Problem you ran into is, that the browser will not submit anything in the formdata if your checkbox hasn't been checked.