Modify and save text files with PHP

2

I have some problems to be able to save and open text files with PHP, I am using the following code:

<?php
if($_POST['submit']){ 
$open = fopen("frases/frase_del_dia.txt","w+"); 
$text = $_POST['update']; 
fwrite($open, $text); 
fclose($open); 
echo "<p>El Contenido se Actualizo!</p>";  
$file = file("frases/frase_del_dia.txt");
foreach($file as $text) { 
echo $text."<br />";  
} 
}else{ 
$file = file("frases/frase_del_dia.txt"); 
echo "<form action='".$_SERVER['PHP_SELF'] ."' method='post'>"; 
echo "<br><textarea name='update' style='height:250px'>"; 
foreach($file as $text) { 
echo $text; 
}  
echo "</textarea><br>"; 
echo "<input name='submit' type='submit' value='Update' />\n 
</form>"; 
} 
?> 

As a result I get the following error:

  

Notice: Undefined index: submit in   /var/www/html/relaxedmind/editor.php on line 2

and I do not see what the file frase_del_dia.txt has in <textarea></textarea> .

The content of my file frase_del_dia.txt is as follows (one sentence for each line):

Solo hay una felicidad en la vida – amar y ser amado
Cuanto más hacemos, más podemos hacer
Cáete siete veces y levántate ocho
El fracaso es éxito si aprendemos de el
Todo tiene belleza, pero no todo el mundo puede verla
El hombre necesita dificultades porque son necesarias para disfrutar el éxito
Es impresionante. La vida cambia muy rápido, de un modo positivo, si la dejas
Tienes que pensar de todas formas. ¿Por qué no pensar en grande?
Rodeate de gente positiva y serás una persona positiva
Cuando tienes un sueño tienes que agarrarlo y nunca dejarlo ir

I would appreciate if you can give me a hand, Thanks!

    
asked by Joseph Gonzaga 11.05.2017 в 18:55
source

3 answers

0

Without modifying the error display (it is useful to have them enabled to debug) you can avoid this error message by changing the if($_POST['submit']){ line to:

if (isset($_POST['submit'])) {

The explanation is that if (VARIABLE) ... evaluates the value of the variable ; but since you have not yet submitted the form, then the variable $ _ POST ['submit'] does not exist, and its evaluation triggers an error stopping the script.

On the other hand, if (isset(VARIABLE)) ... checks if the variable exists . As it does not exist (you have not submitted the form) the script continues with the else section of your script.

    
answered by 11.05.2017 / 20:16
source
2

This is not exactly an error, it is a "notice". Warn that something did not work well at all, but there was no error. It happens because you make a call to an index of an array or a variable (in this case submit ) which has not been initialized. The deactivation of the errores , warning , notice and deprecated of PHP, you can do it in the following way:

Open the php.ini file found on your web server.

In the error_reporting directive you can indicate which types of messages you want to appear, and which ones do not.

For example:

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

This way you are indicating that all the errors are shown except the notice and the deprecated . Therefore in your case the only thing you have to add to this line is: & ~E_NOTICE You restart the server and you're done. I hope it helps you. Greetings

    
answered by 11.05.2017 в 20:06
1

Here is an improvement to your code. Try it and tell me

<?php
if(isset($_POST["submit"]) && $_POST['submit'] && isset($_POST['update'])){ 
$text = $_POST['update']; 

$open = fopen("frases/frase_del_dia.txt","w+"); //abres el fichero en modo lectura/escritura
fwrite($open, $text);//escribes el contenido en el fichero
echo "<p>El Contenido se Actualizo!</p>"; 

$text = fread($open, filesize($open)) //recuperas el contenido del fichero
echo $text;

fclose($open);//cierras el fichero
}
else{ 
echo "<form action='".$_SERVER['PHP_SELF'] ."' method='post'>"; 
echo "<br><textarea name='update' style='height:250px'>"; 

$open = fopen("frases/frase_del_dia.txt","r"); //abres el fichero en modo lectura
$text = fread($open, filesize($open)) //recuperas el contenido del fichero
echo $text;
fclose($open);//cierras el fichero  

echo "</textarea><br>"; 
echo "<input name='submit' type='submit' value='Update' /></form>";
} 
?>
    
answered by 11.05.2017 в 20:14