This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have been trying to create a php script which takes any number from a text form, and puts it into an array. Then I want to loop through the array and output each number.
So far with some searching around on Google, I've come up with this piece of code. Unfortunatly it doesn't work. The strange thing is, I actually got it working at some point, but somehow the code wouldn't function anymore without actually changing anything in the script.
Can anyone help me complete this, I have my version which worked at some point but doesn't anymore.
(I know I should filter input but since this is just an exercise it doesn't really matter.)
<html>
<head>
<title>13.13</title>
<body>
<h2> Inputting numbers into array through form</h2>
<br>
<br>
<form>
<input type="text" name="number" />
<input type="button" value="submit" name="submit" />
</form>
<?php
session_start();
if (isset($_REQUEST['submit'])) {
$number = $_REQUEST['number'];
if (!isSet($_SESSION['number'])) {
$_SESSION['number'] = array();
}
array_push($_SESSION['number'], $number);
foreach($_SESSION['number'] as $key => $val) {
echo $key . ">" . $val;
}
}
?>
</body>
</html>
Move session_start() to the top of your code before any HTML output. You can't start a session after headers have already been sent - and they have been sent because HTML output has been sent.
It's just good practice anyway.
You need to define a method in your form.
Here' the full code, also, always place session_start at the top of the page.
<?php
session_start();
if (isset($_REQUEST['submit'])) {
$number = $_REQUEST['number'];
if (!isSet($_SESSION['number'])) {
$_SESSION['number'] = array();
}
array_push($_SESSION['number'], $number);
/* foreach($_SESSION['number'] as $key => $val) {
echo $key . ">" . $val;
}
*/
var_dump($_SESSION['number']);
}
?>
<html>
<head>
<title>13.13</title>
<body>
<h2> Inputting numbers into array through form</h2>
<br>
<br>
<form method="post" action="#">
<input type="text" name="number" />
<input type="submit" value="submit" name="submit" />
</form>
</body>
Output
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 months ago.
The problem
The $_FILES['file'] array is set, yet it's empty whenever I try to use it.
What I tried
Googling
Setting file upload to On in php.ini (both Xampp and project file root)
Uploading one file at a time (just a wild try at fixing the problem
Debugging the entire code for a month trying to solve this problem
What I know for a fact
The path to the onSubmit is correct
The name of the input in the form and the name after $_FILES['file'] is exactly the same
The form has all it's required attributes
The input has type="file" and multiple in it
My code for the form(HTML) and the file engine(PHP)
<html>
<form method="POST" action="../php/post.php" enctype="multipart/form-data">
<h3>Title</h3>
<input type="hidden" name="case" value=1>
<input type="title" name="pname">
<h3>Message</h3>
<input type="message" name="pmsg">
<h3>Images</h3>
<input type="file" name="pimg[]" multiple>
<input class="submit" type="submit" value="Upload">
</form>
</html>
PHP
<?php
if (!empty($_FILES['file']['pimg'])){
$noFiles = 1;
echo "Files found...\n";
} else {
$noFiles = 0;
echo "Files not found...\n";
echo (!empty($_FILES['file']['pimg']));
echo $_FILES['file']['pimg'][0];
}
?>
Output
The If determines the array is empty, the last echo causes an error
You're not far off.
$_FILES['file']['pimg'] should be $_FILES['pimg']
You should also check to see if there is something selected. $_FILES['pimg']['size'] checks the filesize. If 0, nothing is selected
This way works.
<?php
if ($_FILES['pimg']['size'] != 0){
$noFiles = 1;
echo "Files found...\n";
echo $_FILES['pimg']['name'][0];
} else {
$noFiles = 0;
echo "Files not found...\n";
/* This will always be empty placed here as no files have been found */
//echo (!empty($_FILES['file']['pimg']));
//echo $_FILES['file']['pimg']['name'][0];
}
?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
i am building a multi step login page like that of gmail,
i want the email address the the user typed in to be stored in a global variable so i can echo it in the next step of the form i have been trying but can't figure it out, have seen many error, when i change the code i get a different one the new one i have now is (Notice: Undefined index: email in C:\xampp\htdocs\xxxxx\index.php on line 3)
this is the form
<form action="" method="post" class="form-login">
<div class="step-login step-one">
<input type="text" class="email"/>
<input type="button" class="btn next-step" value="next">
</div>
</form>
this is the php code that i am trying to use and store the input, i think this is where the problem is
<?php
$emails = $_POST['email'];
if(isset($_POST['next'])){
$GLOBALS['email'] = $_GET['email'];
}
?>
this is the code where i am trying to echo the varible
<div class="data-user-find">
<p class="user-email"><?php echo $GLOBALS['email']; ?></p>
</div>
Please guys help me
The "value" of your button is "next", but your button isn't called "next", it should be <button name="next" value="next" type="submit">
Then your error would go away. Also, you define an $email variable outside of your if-clause, please put it inside your if clause, so your code would be this:
<?php if(isset($_POST["next"]) {
$email = $_POST["email"]; (DONT FORGET TO GIVE YOUR MAIL INPUT THE 'name="mail"' TAG)
$GLOBALS["email"] = $email;
} ?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am trying to upload some files on server with PHP and HTML.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<body>
<form action="upload3.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="upload[]"/><br></p>
<p><input type="file" name="upload[]"/><br></p>
<p><input type="file" name="upload[]"/><br></p>
<input type='submit' name ='submit' value='Upload' />
</form>
</body>
</html>
PHP:
if(!empty($_FILES['submit']['name'][0])) {
$name_arr = $_Files['upload']['name'];
$tmp_arr = $_Files['upload']['tmp_name'];
$type_arr = $_Files['upload']['type'];
$error_arr = $_Files['upload']['error'];
echo count($tmp_arr);
for($i = 0; $i < count($tmp_arr); $i++) {
if(move_uploaded_file($tmp_arr[$i], "test_uploads/" . $name_arr[$i])) {
echo "Done";
} else {
echo "Nope";
}
}
} else
echo "What's going on?";
And I always get the "What's going on?" message or "the size of" e.g. $tmp_arr "is 0".
Use a var_dump($_FILES) to find out if the files are correctly uploaded.
If it was, Then you will see the array structure for all info.
This should be avoided: $_Files, always a good practice to use correct case ($_FILES).
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I have this issue that my form obviously doesn't send data with POST method but it sends it with GET method.
Here is my HTML code of the form
<form action="action.php" method="POST">
<input type="text" name="text">
<input type="submit" value="send">
</form>
and here is the php code in the action page
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo $_POST['text'];
var_dump($_POST);
}
if(isset($_POST['text'])){
echo "ok";
}else{
echo "no";
}
when I submit the form I get this error for output
Notice: Undefined index: text in F:\test\action.php on line 9
array(0) { } no
but when I send data with GET method it works correctly without any problem.
I think the problem is for phpstorm because it runs correctly in the xampp server. and the considerable thing is when I run it in mozila or IE it says page not found but xampp is okay.
Try using isset with your input like so:
You have to add name="something" for the isset to pick up that you have clicked it.
<?php
if (isset($_POST['sub']))
{
echo $_POST['text'];
}
?>
<form action="" method="post">
<input type="text" name="text">
<input type="submit" name="sub" value="Submit">
</form
I can only assume that the output you are seeing is before you submit the form. When you submit it, you should check for POST and not for post:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
^^^^ here
echo $_POST['text'];
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have the following code. This is used on my admin panel for administrators to put their notes in so the other admins can read it. I don't want to store this in a database.
The issue is the form is not saving the text in the textarea.
<?php
IF (ISSET($_POST["submit"])) {
$string = '<?php
$notes = "'. $_POST["notes"]. '";
?>';
$fp = FOPEN("includes/notes.php", "w");
FWRITE($fp, $string);
FCLOSE($fp);
echo '<div class="ok">' . $lang["done"] . '</div>';
}
include("includes/notes.php");
?>
<form action="" method="post" id="notesform">
<textarea placeholder="Admin notes" class="notes" id="notes"><?php echo $notes; ?></textarea>
<input type="submit" value="OK">
</form>
What is going wrong? How can I fix this? I have tried most suggestions on Google but it keeps happening!
You did not give name attribute to submit button.
When we submit a form to PHP, only elements get posted with a name attribute.
So, your code is not entering:
IF (ISSET($_POST["submit"])) { // Not satisfying this condition.
Corrected code:
<input type="submit" value="OK" name="submit">
Apply the same for text area also.
You had not given name="notes" to text-area and submit button name. Here is the complete correct code. Try this:
<?php
IF (ISSET($_POST["submit"])) {
$string = $_POST["notes"];
$fp = FOPEN("includes/notes.php", "w");
FWRITE($fp, $string);
FCLOSE($fp);
echo '<div class="ok">' . $lang["done"] . '</div>';
}
include("includes/notes.php");
?>
<form action="" method="post" id="notesform">
<textarea placeholder="Admin notes" class="notes" name="notes" id="notes"><?php echo $notes; ?></textarea>
<input type="submit" name="submit" value="OK">
</form>