PHP $_POST error - php

I'm getting the following error with $_POST['str']:
Notice: Undefined index: str in C:\Program Files\EasyPHP-5.3.8.0\www\strrev.php on line 12
I spent too much time to find an answer for this but no luck!
Please take a look at the code and let me know what's wrong with it?
<html>
<head>
<title></title>
</head>
<body>
<?php
if (trim($_POST['str'])) {
$str = $_POST['str'];
$len = strlen($str);
for($i=($len-1); $i>=0;$i--) {
echo $str[$i];
}
} else {
?>
<form method="post" action="">
<input type="text" name="str" />
<input type="button" name="submit" value="Reverse" />
</form>
<?php
}
?>
</body>
</html>
It shows the text field and Reverse button beside the error.
And also when I push the button nothing will happens.

Change if (trim($_POST['str'])) to if (!empty($_POST['str']))
Your if statement is trying to trim an array index that does not exist, hence the error. You must check if your index exists first.
In case anyone is wondering - I used empty instead of isset as the OP, by using trim, implied (or at least, I inferred from this) that a properly set $_POST['str'] containing an empty string was unacceptable. This won't fix the case where $_POST['str'] contains a bunch of spaces, however.

You're not checking if a POST has actually occured:
<html>
...
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST yada yada yada) {
...
}
}
?>
<form action="" method="POST">
...
</form>
...
</html>

$str is not necessarily an array. In order to loop through it, it needs to be an array.
It's just copying that value from $_POST['str'], so when you do the post either
Make sure it's an array with
if (is_array($str))

Related

PHP $_GET not giving me required result

I am writing a PHP code in Adobe Dreamweaver. My code is as shown below. I am expecting my code to output two boxes, into which I write something. And when I click on the submit button, I expect to see the two separate things that I entered into the box to be concatenated. But I'm not getting a concatenation. In fact, NOTHING happens when I click on submit. I am confused. Is this code not something I should be using Dreamweaver for? I am relatively new to PHP and I do not understand all the bits. My suspicion is that Dreamweaver does not recognize "twoFieldForm.php". Can someone please tell me how I can overcome this issue?
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET[fieldOne];
$second = $_GET[fieldTwo];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\";
?>
<form action = "twoFieldForm.php" method = "get">
Field 1 : <input type = "text" name = "fieldOne"/>
Field 2 : <input type = "text" name = "fieldTwo"/>
<input type = "submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\"";
?>
<form action="" method="GET">
Field 1 : <input type="text" name="fieldOne"/>
Field 2 : <input type="text" name="fieldTwo"/>
<input type="submit">
</form>
</body>
</html>
Explanation: You have to add quotes in $_GET['']. Remember indention please, always when I see people using adobe dreamweaver, their code is horrible... If you are refering to the same file, you don't have to use an action="somewhat.php". Just leave it empty. Aswell you have missed a " after your echo statement.
This will work now. Please start using a good IDE then you won't have those basic mistakes because the IDE will show you your mistakes already...
Try add quotes:
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer=$_GET['fieldOne'].$_GET['fieldTwo'];
echo "concatenated string is:".$answer;
try this...

Why am I getting this PHP error?

So here's my full code
<!DOCTYPE html>
<html>
<body>
<h1>Encrypt</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter word to encrypt<input type="text" name="in">
<input type="submit">
<hr>
</form>
<h1>Decrypt</h1>
<form>
Enter word to decrypt<input type="text" name="out">
<input type="submit">
<hr>
</form>
</body>
</html>
<?php
$encrypt = $_POST['in'];
?>
And here's the error I get
Notice: Undefined index: in in /Users/idrisk/Colourity/si/index.php on line 20
Line 20 is $encrypt = $_POST['in']; and I don't see what I'm doing wrong with it. Any ideas?
As a general practice for forms in php, always check if the submit button has been clicked.
First name your submit button:
<input type="submit" name="submit">
then further in your php:
if (isset($_POST['submit'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
EDIT #1: Added to that, you seem to have 2 forms and 2 submit buttons. I suggest you keep only one form, and one submit button (remove the 2nd form element and submit button).
If you really need 2 forms, name your submit buttons differently and then you can call them separately.
<input type="submit" name="submit-in">
<!-- ... -->
<input type="submit" name="submit-out">
<?php // ...
if (isset($_POST['submit-in'])) {
// do your stuff, eg...
$encrypt = $_POST['in'];
}
if (isset($_POST['submit-out'])) {
// do your stuff, eg...
$dencrypt = $_POST['out'];
}
EDIT #2: If you want to echo stuff posted in your form, make sure you do the form submission checking and variable setting before the form and then echo the variable after the form (or wherever you want).
you need to first check if the form has been sent, if it hasn't then $_POST['in'] does not yet exist thus throwing the error
May be nothing but you called a php script after closing the form /form, the body /body and then then the HTML /html
replace this code $encrypt = $_POST['in']; by this $encrypt = #$_POST['in'];
this is an error on client server when you upload this file on remote server you will not saw this. use # sign on the client server when you saw this error in future.

php form never shows anything whenever it's submitted

<html>
<body>
hi!
<?php
Hi!
if(htmlspecialchars($_POST["name"] == "hey"))
{
Hi!
}
?>
</body>
</html>
It's probably something small, but I can't figure out why my message never shows when I try running it. I tried echo, print, and just typing the text out on screen, and I can never get the php form to run, it's always blank. Perms are set to 644. The form submitting the block of code's below...
<html>
<body>
<form action="action.php" method="post">
<input type="text" name="name"><br>
<input type="submit">
</form>
</body>
</html>
In addition to the comments and answers from other users regarding your code missing an echo or print() on "Hi", your brackets are mixed up:
if(htmlspecialchars($_POST["name"] == "hey")) should be :
if (htmlspecialchars($_POST["name"]) == "hey")
You have a syntax error, to print data within php you need to use echo:
<?php
echo "Hi!"; // <--- here
if(htmlspecialchars($_POST["name"]) == "hey") // <---- here you was a syntax error too
{
echo "Hi!"; // <--- here
}
?>
Or other related functions like: print, print_r, var_dump
you couldn't just write html inside php without echoing it..
<html>
<body>
hi!
<?php
if(htmlspecialchars($_POST["name"]) == "hey")
{
echo "Hi!";
}
?>
</body>
</html>

PHP Pass variable up the page

I came across someone asking the question,
How can I pass a variable up the page (on the same page?).
I had a think about it but couldn't think of how to do it myself, so I was wondering if it is even possible?
So what he was trying to do was change the value of $a at the top of the page at the same time as the bottom value of $a.
Is it possible? If so how?
Thanks in advanced.
<?php
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $testf;
echo( "Bottom " . $a);
}
?>
</body></html>
Edit:
After seeing answers, maybe it can be done with jquery, ajax or javascript?
No, you'll need to move the if statement to the top, and any variables you calculate that need to be in the if statement also to the top.
Something like:
<?php
if ("submit" == $submit) {
$a = $testf;
}
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
echo( "Bottom " . $a);
?>
</body></html>
It's considered a good practice to have all the logic before you start outputting the HTML anyways. Your HTML should ideally have as less logic as possible.
More info: https://stackoverflow.com/a/95027/320615 and https://stackoverflow.com/a/1088791/320615
You can probably bend over backwards to make that work somehow.
But the real answer is to handle all your business logic before you start outputting any HTML. You need to decide at the beginning of your code whether the current request is a form submission or not and set variables and HTML templates accordingly. Never mix business logic into the middle of your HTML templates.
You have to use the $_POST['testf'] and $_POST['submit'] variable.
And also check if it exists with isset : php manual isset
<?php
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $_POST['testf'];
echo( "Bottom " . $a);
}
?>
</body></html>
You can't do it in PHP, but you can't change HTML once it's fully loaded using javascript (and jquery to make it easier).
EDIT : ok I read your code a bit quickly the first time :
I don't understand the echo before the <html> tag, doesn't seem right. Also you want to give the value of a POST var to $a, so just :
if(isset($_POST['testf'])) {
echo $_POST['testf'];
}

Beginner Help: Creating and Storing Text Using PHP

I'm trying to write a program where the basic idea is I ask the user for input in a textarea, and then the text gets stored into a word file. Here is the code I'm trying to use:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1>
<br>
<form method = "post"
action = "mysite.php">
<textarea name = "text"
rows = "10"
cols = "20">Write Here</textarea>
<input type = "submit"
value = "Submit Comment">
</form>
<?
if($_POST['text'] !== NULL){
$comment = $_POST['text'];
$file = fopen("texttest.txt", "a");
fputs($file, "<br>\n$comment");
fclose($file);
}
?>
</body>
</html>
I can't seem to get this to work properly. I was also thinking about somehow making the form action store the text and then reload the site, but I haven't gotten that to work (the original file is mysite.php, so the action is to just reload the page).
If anyone has any better ideas of an algorithm to use/different syntax to use, please let me know, as I just started learning basic PHP syntax.
Thanks
Check the following:
Does php have the permission to write files in that directory?
Is that php file called "myfile.php"?
Anyway, when something does not work and you want to know what's causing the arror, place error_reporting(-1); at the beginning of your php - it will output any error or warning, including the ones trown by fopen().
Also, you might want to check whether the variable has been correctly submitted: echo $comment right after you assign it.
Something like this might work.
You might want to do more with the values they are entering and all, but this will basically do what you are asking.
You will also want to make sure that you have the correct path of the file you are trying to write to and that that file has the correct permissions to allow it to be written to:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1><br>
<?php
if (isset($_POST['submit'])) {
if (strlen(trim($_POST['comment']))) {
$file = fopen("texttest.txt", "a");
fputs($file, "$_POST['comment'])\n");
fclose($file);
}
} else {
?>
<form method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">
<label>Leave your comment
<textarea name="comment" rows="10" cols="20"></textarea>
</label>
<input type="submit" name="submit" value="Submit Comment" />
</form>
<?php
}
?>
</body>
Also, since you are returning to the same page you may want to put some kind of message letting the person know that they succeeded in entering something into your address book.

Categories