0

I'm trying to find how to retain my text value after submit, so the text that i submit is still keep in the textbox, there's so many reference in internet but too hard for me to understand (newbie here), so i'd like to ask here, if anyone have some solution.

Here's my form code:

echo
"<form method='post' action='process.php'>
    <tr>
        <td>Nama Jurusan</td>
        <td>:</td>
        <td><input type='text' name='jurusan' size='50%'></td>
    </tr>
    <tr>
        <td>Nama Laboratorium</td>
        <td>:</td>
        <td><input type='text' name='lab' size='50%></td>
    </tr>
    <input name='submit' type='submit' id='ajukan' value='Ajukan'>
</form>";

As you can see, i was placing the form inside echo.

Here's my process.php code:

<?php
if(isset($_REQUEST['submit'])) {
    include "../conf/koneksi.php";
    $jurusan = $_POST['jurusan'];
    $lab = $_POST['lab'];

    $urutkan= "ALTER TABLE tb_pengusul AUTO_INCREMENT = 1";
    mysql_query($urutkan);

    $input = mysql_query("INSERT INTO tb_pengusul (nama_jurusan,nama_laboratorium)
        VALUES ('$jurusan','$lab')") or die (mysql_error());

    echo "<script language=\"Javascript\">\n";
    echo "window.alert('Input sukses !')";
    echo "</script>";
    echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL='../koordinator.php?url='\">";

}
?>
Ujang Hardman
  • 105
  • 1
  • 4
  • 19
  • you already retain with $_POST? – Rakesh Sharma Sep 12 '14 at 07:07
  • mysql functions are deprecated, please use mysqli_* or PDO – Alex Szabo Sep 12 '14 at 07:08
  • Don't echo plain static HTML code. Just write it outsite the `` tags. What do you mean by retain? Tell us what you are trying to do. – Traian Tatic Sep 12 '14 at 07:08
  • @Alex The most useless comment on SO in the PHP section. Please tell me how does that affect him at his level of knowledge? I find your post to be totally off-topic – Traian Tatic Sep 12 '14 at 07:09
  • I think even if he is new to PHP he shouldn't develop applications that are not safe from SQL injection attacks. If he's going to put it on the web, he will soon find his whole database missing. Apart from this, almost on every question with mysql_* is a comment/note stating that is old and should not be used. I don't see many of them flagged as off-topic, etc. – Alex Szabo Sep 12 '14 at 07:11
  • i'm sorry, i can't speak or write english very well. So here's the scheme: 1. fill the textbox 2. Submit it 3. After submit success, the textbox is still filled with the text that i fill before submit. – Ujang Hardman Sep 12 '14 at 07:13
  • @TraianTatic: well, just before, i write the plain html static outside php syntax too, but because i use that code inside "if ($_REQUEST['url']=='Forminput'){ }", i have to use php syntax.. – Ujang Hardman Sep 12 '14 at 07:18
  • @TrainTatic - I still don't think it's off-topic, you can learn more here: http://stackoverflow.com/questions/13944956/the-mysql-extension-is-deprecated-and-will-be-removed-in-the-future-use-mysqli . I think it's never a bad habit to encourage someone not to use old methods, if newer and better ones are in place. Next time, please just flag my comment if you want to, but don't start an argument (at least not in the comments - perhaps in a chat room), as it's not what this section is intended for. – Alex Szabo Sep 12 '14 at 07:20
  • @MuhammadFahmy You can write plain HTML inside PHP `{..}`. Like `

    works

    won't display

    `
    – Traian Tatic Sep 12 '14 at 07:22
  • @Alex There's no argument. I just had to made you understand that `mysql_query()` is not vulnerable by itself; it's deprecated indeed but it won't be removed in the following year anyway. – Traian Tatic Sep 12 '14 at 07:27
  • @MuhammadFahmy its possible you could device a flash session behaviour in your coding – Kevin Sep 12 '14 at 07:30
  • @Alex: well, i still don't understand about mysql functions are deprecated, but it must be something important for my web, thanks. – Ujang Hardman Sep 12 '14 at 07:34
  • @TraianTatic: is that so?, well, it must be easier if i can do that, i will try next time, thanks. – Ujang Hardman Sep 12 '14 at 07:34

3 Answers3

1

Use Post value as, This will works only if you form page and submit code are in same page ( process.php )

<?php  echo"<form method='post' action='process.php'>

<tr><td>Nama Jurusan</td>
        <td>:</td>
        <td><input type='text' name='jurusan' value='".$_POST['jurusan']."' size='50%'></td>
        </tr>
        <tr>
        <td>Nama Laboratorium</td>
        <td>:</td>
        <td><input type='text' name='lab' value='".$_POST['lab']."' size='50%'></td>
        </tr>
  <input name='submit' type='submit' id='ajukan' value='Ajukan'>
  </form>";?>

Also you have error in your code in line

<td><input type='text' name='lab' size='50%></td>

it should be <td><input type='text' name='lab' size='50%'></td>

coDe murDerer
  • 1,858
  • 4
  • 20
  • 28
1

If your form is still available in the process.php file, change it like this:

 echo "<form method='post' action='process.php'>
      <tr><td>Nama Jurusan</td>
      <td>:</td>
      <td><input type='text' name='jurusan' size='50%'";
 if (isset($_POST['jurusan']))
 {
     echo " value=\'$_POST['jurusan']\'";
 }
 echo "></td>
      </tr>
      <tr>
      <td>Nama Laboratorium</td>
      <td>:</td>
      <td><input type='text' name='lab' size='50%'";
 if (isset($_POST['lab']))
 {
     echo " value=\'$_POST['lab']\'";
 }
 echo "></td>
      </tr>
      <input name='submit' type='submit' id='ajukan' value='Ajukan'>
      </form>";
Traian Tatic
  • 681
  • 5
  • 18
0

you need to use PHP sessions.

add the following string to your process.php page

session_start();
$_SESSION['prev_values'] = $_POST;

and the following to your form page

$lab = "";
$jur = "";
session_start();
if(isset($_SESSION['prev_values'])){
    $jur = $_SESSION['prev_values']['jurusan'];
    $lab = $_SESSION['prev_values']['lab'];
}
echo
"<form method='post' action='process.php'>
    <tr>
      <td>Nama Jurusan</td>
      <td>:</td>
      <td><input type='text' name='jurusan' size='50%' value='$jur'></td>
    </tr>
    <tr>
      <td>Nama Laboratorium</td>
      <td>:</td>
      <td><input type='text' name='lab' size='50%'  value='$lab'></td>
    </tr>
    <input name='submit' type='submit' id='ajukan' value='Ajukan'>
</form>";

SECURITY NOTICE: Please note: this script is basic and it is vulnerable to XSS (just to name one). As a rule of thumb, you should NEVER display directly user inputs without some form of sanitation

STT LCU
  • 4,348
  • 4
  • 29
  • 47