View Full Version : Notice: Undefined variable: _post in mit textarea
Die Fehlermeldung : Notice: Undefined variable: _post in
(deutet auf die Zeile mit <?= $_POST['form_prtext'] ?> in untenstehendem Code)
tritt nur auf, wenn ich im Formular textarea verwende. In der Mysql-Tabelle ist das Feld für den Inhalt des Textfelds mit varchar definiert.
Wenn ich ein inputfeld verwende, tritt der Fehler nicht auf.
Code:
Medium:<br>
<input name="form_medium" type="text" class="form_text" size="40" maxlength="40" value="<?= $_POST['form_medium'] ?>">
<br>
Pressetext:<br>
<textarea name="form_prtext" cols="40" rows="15"> <?= $_POST['form_prtext'] ?></textarea>
<br>
Ich habe oben im Script auch
if (!isset($_POST['form_prtext'])) $_POST['form_prtext'] = '';
also ist die Variable definiert!
Keine Ahnung was der Fehler sein könnte...
Not sure if this will fix your problem as I don't speak German, but try the following.
Übersetzt auf Google: Nicht sicher, ob dies Ihr Problem zu beheben, als ich kein Deutsch sprechen, aber versuchen Sie Folgendes.
<br>
<input name="form_medium" type="text" class="form_text" size="40" maxlength="40" value="<?php echo (isset($_POST['form_medium'])) ? $_POST['form_medium'] : ""; ?>">
<br>
Pressetext:<br>
<textarea name="form_prtext" cols="40" rows="15"><?php echo (isset($_POST['form_prtext'])) ? $_POST['form_prtext'] : ""; ?></textarea>
<br>
jscheuer1
08-20-2012, 06:08 AM
_post != _Post
djr33
08-20-2012, 07:25 AM
John, where do you see "_post" in the code above? I just see it in the error message, but I don't know where it's coming from.
Also, it shouldn't be '_post' or '_Post', but always '_POST'. PHP is case sensitive for this.
Losj, hier spricht man nur Englisch. Können Sie Englisch sprechen? Sie werden keine antworten auf Deutsch bekommen. (Ja, ich kann ein bisschen Deutsch sprechen, aber nicht um PHP oder Programmierung zu sprechen. Also ist es besser, dass alle verstanden können.) Aber, willkommen bei Dynamic Drive!
The error message : Notice: Undefined variable: _post in
(points to the <?= $_POST['form_prtext'] ?> in below Code)
only if I use textarea in the form. (In mysql table the field for the content is varchar)
If I use a input instead of textarea there is no mistake
Code:
Medium:<br>
<input name="form_medium" type="text" class="form_text" size="40" maxlength="40" value="<?= $_POST['form_medium'] ?>">
<br>
Pressetext:<br>
<textarea name="form_prtext" cols="40" rows="15"> <?= $_POST['form_prtext'] ?></textarea>
<br>
I have defined
if (!isset($_POST['form_prtext'])) $_POST['form_prtext'] = '';
, the variable is defined
no idea what the mistake is. :confused:.
Not sure if this will fix your problem as I don't speak German, but try the following.
Übersetzt auf Google: Nicht sicher, ob dies Ihr Problem zu beheben, als ich kein Deutsch sprechen, aber versuchen Sie Folgendes.
<br>
<input name="form_medium" type="text" class="form_text" size="40" maxlength="40" value="<?php echo (isset($_POST['form_medium'])) ? $_POST['form_medium'] : ""; ?>">
<br>
Pressetext:<br>
<textarea name="form_prtext" cols="40" rows="15"><?php echo (isset($_POST['form_prtext'])) ? $_POST['form_prtext'] : ""; ?></textarea>
<br>
There is no mistake for the input fields! All variales have been defined in the script.
The strange is that if I o not decleare one variable for a inputfield, then the error message says more than post, but the whole variable is named...
Not sure if this will fix your problem as I don't speak German, but try the following.
Übersetzt auf Google: Nicht sicher, ob dies Ihr Problem zu beheben, als ich kein Deutsch sprechen, aber versuchen Sie Folgendes.
<br>
<input name="form_medium" type="text" class="form_text" size="40" maxlength="40" value="<?php echo (isset($_POST['form_medium'])) ? $_POST['form_medium'] : ""; ?>">
<br>
Pressetext:<br>
<textarea name="form_prtext" cols="40" rows="15"><?php echo (isset($_POST['form_prtext'])) ? $_POST['form_prtext'] : ""; ?></textarea>
<br>
there is no error message for the code with the input field! but I try to declare the variable just before use as you do. I declared everx variable in the beginning of the script
Could you show us all of your code so that we can see anything that may of been done wrong? Also - when showing us errors, please copy and paste. Thanks
Could you show us all of your code so that we can see anything that may of been done wrong? Also - when showing us errors, please copy and paste. Thanks
<?PHP
if(!isset($_SESSION))
{
session_start();
}
if (!$_SESSION['userID']) {
header("Location: http://".$_SERVER['HTTP_HOST']
.dirname($_SERVER['PHP_SELF'])
."/login.php");
exit;
}
if (!isset($_POST['form_datum'])) $_POST['form_datum'] = '';
if (!isset($_POST['form_medium'])) $_POST['form_medium'] = '';
if (!isset($_POST['form_prtext'])) $_POST['form_prtext'] = '';
if (!isset($status)) $status = '';
if (isset($_POST['upload'])) {
if (isset($_POST['form_datum']) && ($_POST['form_medium']) && ($_POST['form_prtext']))
{
$sql = "INSERT INTO pressetexte (datum, medium, pressetext) VALUES (
'".$_POST['form_datum']."',
'".$_POST['form_medium']."',
'".$_POST['form_prtext']."'
)";
$result = mysql_query($sql);
if($result) {
$status = "...eingetragen";
unset($_POST);
}
else {
$status = "...fehlgeschlagen '".mysql_error()."'";
}
}
else {
$status = '...Fehler! Fehlende Daten ';
}
}
?>
<script language="javascript">
function openWindow(url) {
var win;
win = window.open(url,'Druckversion','width=500,height=400, scrollbars=yes, menubar=no');
win.focus();
}
</script>
<h2>Pressetext <?=$status?></h2>
<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post" enctype="multipart/form-data" name="pressetexte">
Datum:<br>
<input name="form_datum" type="text" class="form_text" size="30" maxlength="30" value="<?= $_POST['form_datum'] ?>">
<br>
Medium:<br>
<input name="form_medium" type="text" class="form_text" size="40" maxlength="40" value="<?= $_POST['form_medium'] ?>">
<br>
Pressetext:<br>
<textarea name="form_prtext" cols="40" rows="15"> <?= $_POST['form_prtext'] ?></textarea>
<br>
<p><input type="submit" name="upload" value="Upload"></p>
</form>
<p> </p>
<table width="650" border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
<?PHP
$sql = "SELECT id, datum, medium, pressetext FROM pressetexte ORDER by id ASC";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td width="70" bgcolor="white" align="center">'.$row['datum'].'</td>';
echo '<td width="130" bgcolor="white" align="center">'.$row['medium'].'<br /></td>';
echo '<td width="430" bgcolor="white" align="center">'.$row['pressetext'].'<br /></td>';
echo '<td width="20" bgcolor="white" align="center"><a href="javascript:openWindow(\'pressetext_edit.php?id='.$row['id'].'\');">edit</a></td>';
echo '</tr>';
}
?>
</table>
Thanks for the help
And here the complete error report:
Notice: Undefined variable: _post in /srv/www/vhosts/...../Admin/pressetext.php on line 61
The error shows up in the textarea
Is it _post or _POST. Be more specific.
The problem probably is that on line 31 you're unsetting $_POST. This would obviously lead to further issues. Don't do that...
jscheuer1
08-20-2012, 02:40 PM
Probably:
if($result) {
$status = "...eingetragen";
unset($_POST);
}
Because that unsets everything you just set in the $_POST array. But I'm not sure how to fix it.
djr33
08-20-2012, 04:14 PM
I'm not sure if this is the issue in this case or not, but one thing to remember is that unless the form is actually submitted, those values will not exist. In this case, you will need to use the following:
if (isset($....)) { echo $...; }
I'm not trying to be rude or anything, but have you guys read my previous responses?
@John I've addressed that in my latest post.
@Daniel That was in my first post. It may of been a bit hidden with my ternary ifs and inline PHP (of which I recommend to the OP not to do), the answer came later that the variables are definitely "defined" in the code. (We then noticed that he was unsetting the $_POST variable if a query was successful... John and I both pointed that out)
djr33
08-20-2012, 04:45 PM
Ah, I see it in that post now. But it didn't have an explanation there, so consider the two posts as a pair then ;)
Hi you everybody, thanks.
I' lljust try to make the condition before unsetting the _POST.
If it works, there is still the queation why this error turns up when using textarea, but not with the input fields..
I'll try and post if it works. I am a she ;-)
Hello
I found a way. Instead of unsetting the _POST I set it on a empty string
if($result) {
$status = "...eingetragen";
set($_POST['']);
And this works :-)
Thanks for the help again.
There's no set function in PHP...
There's no set function in PHP...
Well, thenmaybe I could just not write anything.
Tried.
if($result) {
$status = "...eingetragen";
}
else {
This works.
And this works , too.
$_POST[''] = '';
So this is correct? The set didn't produce an error... why? BEcause its correct syntax? even if set doesn't exist?
Merci bien.
djr33
08-20-2012, 09:12 PM
As far as I know, there isn't a set() function, because you just use the equals sign $var = 'value'; method. It's part of the syntax, not a function.
I don't think this should work:
$_POST[''] = '';
You are setting a null entry in the $_POST array to an empty string. Instead, I think you mean this:
$_POST = ''; (or $_POST = array(); for an empty array)
But why do you need to change it anyway? The values in $_POST are there for a reason and very, very rarely (in cases of complex redirects for example) would you want to change them manually. They only last for one page load and during that time, it is standard to know what was submitted via post methods in $_POST, just like using $_GET for the URL query string variables and $_COOKIE for cookie information, plus $_SERVER for information about the request and server settings.
If you need to unset $_POST, then honestly you are probably doing something wrong overall. If you want to create a temporary variable that you can later delete (a working variable), then you can just copy $var = $_POST['var'];, then you can do whatever you want with $var, but you shouldn't be changing $_POST directly (in almost all circumstances).
Regarding set() not producing an error:
1. It might be an alias for the = method for setting a variable. It's not listed in the manual, though, so I'd avoid it regardless. It is correct syntax, but an undefined function usually triggers a fatal error.
2. Check that your error reporting settings are set to report all errors (and probably notices/warnings as well). This way you'll know your code is working well, that you're writing standard code without any deprecated usages, and that it should generally work on other servers if you ever need to move it. PHP error reporting (although sometimes confusing) is a great feature that I think is very helpful once you learn to use it (again, it can be confusing).
$_POST = ''; (or $_POST[''] = array(); for an empty array)
I think you mean $_POST = array();
djr33
08-21-2012, 12:03 AM
Yes, I did... cut and paste error. Fixed now. Thanks.
Yes, I did... cut and paste error. Fixed now. Thanks.
Hi
well I just do not write anything. It's not necessary, really.
I have another form where I want to delete the content of the onput on upload. So there I put every single variable on empty string, but not in the same function, but after reading out DB Content. So it cannot be sent twice.
I have an existing script which I change for my needs.
Is it correct, in the below script I could delete the first 3 lines ?
LG J.
<?PHP
/*1*/ if (!isset($_POST['form_datum'])) $_POST['form_datum'] = '';
/*2*/ if (!isset($_POST['form_email'])) $_POST['form_email'] = '';
/*3*/ if (!isset($_POST['form_text'])) $_POST['form_text'] = '';
/*4*/ if (!isset($status)) $status = '';
if (isset($_POST['upload'])) {
if (isset($_POST['form_datum']) && ($_POST['form_email']) && ($_POST['form_text']))
{
$sql = "INSERT INTO gaestebuch (datum, email, text) VALUES (
'".$_POST['form_datum']."',
'".$_POST['form_email']."',
'".$_POST['form_text']."'
)";
$result = mysql_query($sql);
if($result) {
$status = "...eingetragen";
}
else {
$status = "...fehlgeschlagen '".mysql_error()."'";
}
}
else {
$status = '...Fehler! Fehlende Daten ';
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="de">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="Robots" content="INDEX,FOLLOW">
<meta name="Revisit" content="After 30 days">
<link href="scripts/....css" rel="stylesheet" type="text/css">
<script type="text/JavaScript">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/scripts/jquery.ez-bg-resize.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("body").ezBgResize({
img : "/grafik/hintergrund_gaestebuch.gif", // Relative path example. You could also use an absolute url (http://...).
opacity : 1, // Opacity. 1 = 100%. This is optional.
center : true // Boolean (true or false). This is optional. Default is true.
});
});
</script>
</head>
<body >
<div id="main">
<div id="logo"><img src="grafik/anna_logo.gif" alt="" border="0" width="212" height="45"></div>
<div align="left" id="navi">
<a href="index.php"> home</a>
<a href="biografie.php">biografie</a>
<a href="musik.php">musik</a>
<a href="videos.php">videos</a>
<a href="galerie.php">galerie</a>
<a href="gaestebuch.php">gästebuch</a>
<a href="pressetext.php">pressetext</a>
<a href="kontakt.php">kontakt</a>
</div>
<div id="gaestebuch">
<h1 id="inhtitel">Gästebuch</h1>
<div id="gtexte"><?PHP
$i = "0";
$sql = "SELECT datum, email, text FROM gaestebuch ORDER BY id DESC";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$i++;
if($i%2 == 0) {echo '<tr>';}
else {echo '<tr>';}
echo '<td width="30%" align="left">'.$row['datum'].'<br/></td></tr>';
echo '<tr><td width="70%" align="left">'.$row['text'].'. '.$row['email'].'<br/></td>';
echo '</tr>';
}
$_POST['form_datum'] = '';
$_POST['form_email'] = '';
$_POST['form_text'] = '';
?>
</div>
<div id="guestin" > <hr> <form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post" enctype="multipart/form-data" name="gaestebuch">
Datum:
<input name="form_datum" type="text" id="form_datum" size="15" maxlength="20" value="<?= $_POST['form_datum'] ?>">
Name: <input name="form_email" type="text" id="form_email" size="30" maxlength="40" value="<?= $_POST['form_email'] ?>">
<br>
Dein Text:<br /><textarea name="form_text" cols="53" rows="3" id="form_text"> <?= $_POST['form_text'] ?></textarea>
<input type="submit" name="upload" value="Upload">
</form> </div>
djr33
08-23-2012, 03:03 AM
If you delete the first three lines and the form has been submitted immediately before this page load (as the request for this page), then those values would exist. But $_POST values are reset every time the page loads-- the only last through one page, and they are not there permanently.
Also, you can delete the fourth line without any problems at all. $status is never set above that, so it will never have a value-- isset($status) will always be false, so it won't do anything.
The exception here is if you're using include() to bring this code into another page. If that's the case, then the situation is very different:
1. $status might be set, and you can unset it if you'd like. But I'd recommend using a new variable instead, to avoid confusion. Use $status2 if you'd like, or anything else.
2. I still would not recommend unsetting $_POST variables. Keep those accurate for what was submitted. Instead, either: 1) use a new variable name in the code below (because this way you will NEVER have a $_POST['form_datum'] value), or 2) at the beginning of your code create $form_datum and use that instead of $_POST[...], and unset $form_datum if you want.
By the way, I suggest unset($var) instead of $var = ''. The '' method will still result in a value, but just a null value. Using unset() will actually entirely remove that variable from existence. This normally won't be very different, but it can sometimes matter if you're later using isset() for example.
(The only time that using $var = '' makes sense is when you want to give a blank value for a string, such as one that you plan to echo later, and you don't want an error for 'undefined variable'.)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.