-1

Hello i am bit struggling to find the solution or there is other effective way to do it, so here is the case, i have file named display.php in display folder, then i have add_data.php in kas folder, is there way when i input/edit data in add_data.php then display.php reload itself ? i tried to learn ajax but looks like i find dead end to resolve it.

in Display.php i put this ajax

$.ajax({
  type: "GET",
  url: "../kas/add_data.php",
  dataType: "json",
  data: "simpan",
}).done(function(data) {
  location.reload();
}).fail(function(msg) {
  console.log(msg);
});

this is the add_data.php

if (isset ($_POST['Simpan'])){
    $masuk=$_POST['masuk'];
    $masuk_hasil=preg_replace("/[^0-9]/", "", $masuk);

    //mulai proses simpan data
    $sql_simpan = "INSERT INTO kas_masjid (tgl_km,uraian_km,masuk,keluar,jenis) VALUES (
        '".$_POST['tgl_km']."',
        '".$_POST['uraian_km']."',
        '".$masuk_hasil."',
        '0',
        'Masuk')";
    $query_simpan = mysqli_query($koneksi, $sql_simpan);
    mysqli_close($koneksi);

    if ($query_simpan) {
        echo "<script>
      Swal.fire({title: 'Tambah Data Berhasil',text: '',icon: 'success',confirmButtonText: 'OK'
      }).then((result) => {if (result.value){
          window.location = 'index.php?page=i_data_km';
          }
      })</script>";
    }else{
        echo "<script>
      Swal.fire({title: 'Tambah Data Gagal',text: '',icon: 'error',confirmButtonText: 'OK'
      }).then((result) => {if (result.value){
          window.location = 'index.php?page=i_add_km';
          }
      })</script>";
    }}
?>

so i when i add input to add_data.php, display.php doing nothing, but when i see from console display.php, echo all add_data.php script.

8
  • 3
    data: "simpan" is not the correct way to send parameters in AJAX. It should be something like data: {simpan: 1, masuk: value1, tgl_km: value2, uraian_km: value3} Commented Aug 5 at 20:58
  • 2
    And when you use type: "GET" you need to use $_GET in PHP to read the parameters, not $_POST. Commented Aug 5 at 21:00
  • 5
    Your script is vulnerable to SQL Injection Attack. Even if you are escaping variables, its not safe! You should always use prepared statements and parameterized queries in either MYSQLI or PDO instead of concatenating variables into the query. Commented Aug 5 at 21:03
  • 2
    Please be more specific in where this is going wrong. location.reload() will, without fail, reload the page. I find it likely that it's not running at all, i.e. the request is failing. The PHP is probably raising some 4XX or 5XX, you should use the Network tab in your browser's debugger to find out for sure. Commented Aug 5 at 21:30
  • 2
    Welcome to SO. You'll need to fix the other errors already described before this matters, but when you get there, be careful, simpan != Simpan. Open your browser's devtools, check the console and network tabs, you will errors and feedback about the requests and responses you are making, those will help you move forward. Commented Aug 5 at 22:45

1 Answer 1

1

In an AJAX request, the dataType parameter specifies the type of data that is expected back from the server in the response.

Now in your original code, the dataType you have specified is "json", but the return value of add_data.php is not of json type, hence the fail is triggered causing the msg to be displayed in console log

So say if the following is the add_data.php, the calling page will be able to reload:

<?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);?>

So to tackle the problem, either remove the following line:

dataType: "json",

or simply return a data of type json in your php script

So, in short, the issue that the page does not reload (as in your original code) is:

The dataType option in $.ajax() specifies the type of data you expect back from the server. If the server returns data in a format that does not match the specified dataType, jQuery will attempt to parse or interpret the response according to the dataType. If this parsing or interpretation fails, jQuery treats it as an error, even if the HTTP status code indicates success. This parsing error then triggers the fail() callback.

Of course, there are also additional issues you need to address, that are :

  1. please amend your sql query in the php file to parameterized prepared statement which is resilient against SQL injection (as stated by other volunteers too)

  2. I believe you want to trigger Swal.fire in your calling page, in that case the javascript block should be in the calling page instead of the php script

  3. please use POST in the ajax in your calling page, otherwise the php page block if (isset ($_POST['Simpan'])){ .... } will never get executed, and you should properly pass the POST parameters too (as stated by other volunteers)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.