Do not clear text field value after error occurs

I want that after sending data, if an error occurs, it does not delete the data already recorded in the text fields or check boxes. How can i do this? This is the html code:

<form action="adaugare-elev.php?actiune=validare" method="post">
    Date de contact <br>
    <table>
       <tr><td>Nume: <th><input type="text" name="nume" required></th></td></tr>
       <tr><td>Prenume: <th><input type="text" name="prenume" required></th></td></tr>
       <tr><td>Seria: <input type="text" style="width:25px;" name="seria" required></td> <td>Nr: <input type="text" name="nr" style="width:50px;" name="nr" required></td></tr>
       <tr><td>CNP: <th><input type="text" name="cnp" required></th></td></tr>
       <tr><td>Mail: <th><input type="text" name="mail" required></th></td></tr>
       <tr><td>Loc de munca: <th><input type="text" name="locmunca" required></th></td></tr>
    </table><br>
</form>

This is the php code for the form:

if(!isset($_GET['actiune'])) 
    $_GET['actiune'] = '';
switch($_GET['actiune'])
{
    case 'validare':
       $_SESSION['nume'] = $_POST['nume'];
       $_SESSION['prenume'] = $_POST['prenume'];
       $_SESSION['seria'] = $_POST['seria'];
       $_SESSION['nr'] = $_POST['nr'];
       $_SESSION['cnp'] = $_POST['cnp'];
       $_SESSION['mail'] = $_POST['mail'];
       $_SESSION['locmunca'] = $_POST['locmunca'];
       $inserare = "insert into `elevi` (`nume`,`prenume`,`nr`,`cnp`,`mail`,`locmunca`,) values ('".addentities($_SESSION['nume'])."','".addentities($_SESSION['prenume'])."','".addentities($_SESSION['nr'])."','".addentities($_SESSION['cnp'])."','".addentities($_SESSION['mail'])."','".addentities($_SESSION['locmunca'])."')";
}

I tried using the popup alert (echo "<script>alert('Something wrong')</script>";after the if clause, but it didn't work.

+4
source share
3 answers

PHP is a one-way language on the server and to store your data, you must save your values ​​in a session in order to store them. if you want users not to leave fields empty, use javascript.

, , mysql_error() mysqli; mysqli_error(). , . , , . :

if ( isset ( $_SESSION['error'] ) ) {
    // echo the form with the values in there
}

, , - :

<tr><td>Nume: <th><input type="text" name="nume" value="<?php ( ( isset ( $_SESSION['error'] ) ) ? echo $_SESSION['nume'] : "" ) ?>" required></th></td></tr>

$_SESSION['error'] -, mysql_error() .

POST, $_POST [ "nume" ] .


$_SESSION['error'], , $inserare . mysql, .

, , , !

$query = mysql_query($inserare);

MySQLi:

if ( mysql_error ( $query ) ) {
    $_SESSION['error'] = 1;
}

MySQL:

if ( mysqli_error ( $query ) ) {
    $_SESSION['error'] = 1;
}
+2

, , , . :

<tr><td>Nume: <th><input type="text" name="nume" value="<?php if (!empty($_POST["nume"])) { echo $_POST["nume"]; } else { echo ''; };  ?>" required></th></td></tr>
+3

or in addition to lolka_bolka post use tertiary operator

<tr><td>Nume: <th><input type="text" name="nume" value="<?= !empty($_POST["nume"]) ?$_POST["nume"] : ''   ?>" required></th></td></tr>
+1
source

Source: https://habr.com/ru/post/1568133/


All Articles