2

I have a code like this:

<body>
<?php
// define variables and set to empty values
$namaErr = $nikErr = $shiftErr = "";
$nama = $nik = $shift = $keterangan = $tgl = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["nama"])) {
    $namaErr = "<br><i>Nama tidak boleh kosong</i>";
  } else {
    $nama = test_input($_POST["nama"]);
    // cek nama harus pake huruf tanpa simbol
    if (!preg_match("/^[a-zA-Z ]*$/",$nama)) {
      $namaErr = "<br><i>Nama harus diisi dengan Huruf dan tanpa karakter simbol</i>";
    }
  }

 if (empty($_POST["nik"])) {
    $nikErr = "<br><i>NIK tidak boleh kosong</i>";
  } else {
    $nik = test_input($_POST["nik"]);
    // cek nik harus pake angka tanpa simbol
    if (!preg_match("/^[0-9]*$/",$nik)) {
      $nikErr = "<br><i>NIK harus diisi dengan Angka</i>";
    }
  }

  if (empty($_POST["keterangan"])) {
    $keterangan = "";
  } else {
    $keterangan = test_input($_POST["keterangan"]);
  }

  if (empty($_POST["shift"])) {
    $shiftErr = "<i>Pilih salah satu Shift Kerja</i>";
  } else {
    $shift = test_input($_POST["shift"]);
  }
}
    function test_input($data) {
        $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
?>
    <div class="container">
        <form name="fmk" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
</html>

I want to send the form data to Proses.php to show the form data, but when I change the section form action from <form name="fmk" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post"> to <form name="fmk" action="<?php echo htmlspecialchars(proses.php);?>" method="post"> or <form name="fmk" action="proses.php" method="post">, it succeeds in submitting the form data to Proses.php, but the PHP code inside Proses.php fails to check the form data for validation. My objective is when I click the Submit button, it will go to another page and show the result from form data with PHP syntax when the input field is not empty. If some input fields are empty, it will show the red sign and not go to the other page (still on the first page).

Please help me to solve this problem. Sorry for my bad english, Love from Indonesia :)

matthias_h
  • 11,356
  • 9
  • 22
  • 40
  • Add a class that is defined as `.alert { color:#400000; background-color:#ffcccb }` `.success { color: #006400; background-color: #90EE90; }` in your css, then switch those in your conditional for error and success. – dale landry Apr 02 '20 at 06:14
  • I did. The question is not my css. but my php or html. I want when I click Submit button, it will go to other page and show the result from form data with PHP syntax while the input field not empty. If the some input field empty, will show the red sign and not go to other page(still on the first page). – Kevin123 Kocak Apr 02 '20 at 06:50
  • See https://stackoverflow.com/questions/871858/php-pass-variable-to-next-page – Martin Zeitler Apr 03 '20 at 03:23

2 Answers2

0

If you want the form validation on the client side to happen as (show the red sign if empty) you must add the required attribute to your input fields.In this way you can successfully check your form inputs and in addition if you want to set the input pattern for your input you can also use pattern attribute in your inputs.Both of these will validate your form on the client side. Hope this might help you.

Kunal Raut
  • 2,495
  • 2
  • 9
  • 25
0

how to send form data to different .php file

There are a couple of ways to do this depending on how you want to process the information. You can use the form to send over post by setting your action attribute to the desired page. Setting the method attribute to post allows the name of the input fields to be carried over to that page through the server. On the target page you check the $_POST globals to make certain they are set and if they are set, you can then define them or call on their global variables in your code.

The way I do this on my target page is to first check if the submit button is set in the global POST array. If I have a button that submits my form and that name is name="submit" the $_POST will store that as a value in the global array.

if(isset($_POST['submit'])){
    //submit is set, I can now check for the other values in the global $_POST array
    if (empty($_POST["nama"])) {
        $namaErr = "<br><i>Nama tidak boleh kosong</i>";
    } else {
        $nama = test_input($_POST["nama"]);
        // cek nama harus pake huruf tanpa simbol
        if (!preg_match("/^[a-zA-Z ]*$/",$nama)) {
            $namaErr = "<br><i>Nama harus diisi dengan Huruf dan tanpa karakter simbol</i>";
        }
    }
}

If I have an issue here. For example I am testing and I know the things are set in the form, I can var_dump($_POST) and make certain that $_POST values are set by looking at the results of my $_POST array and checking the key/value pairs.

The other way to direct a user once they submit a form is by having the form action set to self or leaving your action attribute out completely. You can check if the submit button is set and then parse through the global $_POST array within the if(isset($_POST['submit])){ //form has been submitted check inputs, run validations and set errors, etc, etc... } conditional. You can do all the work on the same page the form is on using this method and then once all has been successfully completed use a header redirect to send the user to the desired page you wish for them to visit with a success url post.

It would be something to the effect:

if(isset($_POST['submit'])){
    if(isset($_POST['name'])){
        $name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); 
        $msg = "Success, thank you for submitting";   
        // maybe check validation of inputs and run other inputs through sanitation etc...
        // once you finish your checks if all is good set your url params
        $host  = $_SERVER['HTTP_HOST']; // sets your server name
        $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // PHP_SELF
        $root = 'proses.php';
        $urp = '?success';// adds a url post to your url and this can be checked on the other page
        header("Location: http://$host$uri/$root$urp");
        exit;    
    }else{
        // run error code here
        $error = true;
        $msg = "Error please try again";
    }
}
dale landry
  • 7,831
  • 2
  • 16
  • 28