Form Not Saving to PHP File [duplicate] - php

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>

Related

PHP files not detected using the $_FILES array [duplicate]

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];
}
?>

Isset is not working [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
PHP & Case Sensitivity [duplicate]
(3 answers)
Closed 5 years ago.
I have a problem with isset. What I want is some echos after getting, checking and initialising the values from isset. it doesn't work. I don't see any reason behind this. It only refreshes the page after submitting the form.
Below is the code
<?php
if (isset($_post['contact_name'] ) && isset($_post['contact_email']) && isset($_post['contact_text']))
{
echo $contact_name = $_post["contact_name"];
echo $contact_email = $_post['contact_email'];
echo $contact_text = $_post['contact_text'];
//!empty($_post['contact_name'])
//echo 'no value submit';
if(!empty($contact_name) && !empty($contact_email) && !empty($contact_text))
{
echo 'ok';
}
else
{
echo 'all fildes required !!!!!!!!!!!!!';
}
}
?>
<html>
<form action="index.php" method="post">
name : <br> <input type="text" name="contact_name" ><br><br>
Email address : <br> <input type="text" name= "contact_email "><br><br>
Message :<br><textarea name="contact_text" rows="6" cols="30" type="text" ></textarea><br><br>
<input type="submit" value="send">
</form>
</html>
You need to use this;
<?php
if (isset($_POST['contact_name'] ) && isset($_POST['contact_email']) && isset($_POST['contact_text']))
{
...
Use $_POST instead of $_post. Hope this will work.

How can I post form to mysqli without using an action page [duplicate]

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 don't wanna use an action page. I wanna post on the same page.
These are my codes. But there is an error called "Undefined index: baslik on line 5 and aciklama on line 6
<?php
include("baglan.php");
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
<?php
include("baglan.php");
// check for post request here
if(isset($_POST['baslik']) && $_POST['baslik'] != "") {
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik']);
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)){
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>
You are not checking if the POST data actually exists, so when you first load the page there is no $_POST
do something like
if (isset($_POST)){
//your php code here
}
I can see the form IS submitting to the same page.
You just should wrap the PHP code in if condition
<?php
include("baglan.php");
if(!empty($_POST['baslik'])) { // This is the new if condition
$site_basligi = mysqli_real_escape_string($conn, $_POST['baslik'] );
$site_aciklamasi = mysqli_real_escape_string($conn, $_POST['aciklama']);
$ayarsql = "UPDATE ayar SET baslik='$site_basligi', aciklama='$site_aciklamasi' WHERE durum='1'";
if($conn->query($ayarsql)) {
echo "Güncelleme başarılı";
}
}
?>
<form action="" method="post">
Site Başlığı: <input type="text" name="baslik" ><br><br>
Açıklama: <input type="text" name="aciklama"><br><br>
<input type="submit">
</form>

HTML - method POST in form does not send data in phpstorm [duplicate]

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'];
}

PHP $_POST into array and output array [duplicate]

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

Categories