0

i have code, but isn't run in barang.php i have this code :

barang.php

<?php
$p=isset($_GET['act'])?$_GET['act']:null;
switch($p) {
    default :
        echo '':
        break;
    case "edit":
        include "editrefbrg.php";
        break;
}

and in pagination.php i have this code :

paginatio.php

<td>
    <a href="?page=barang&act=edit" target="blank" class="edit" id='.$row["id"].'>
        <i class="glyphicon glyphicon-edit"></i>
    </a>
</td>

for ajax i use this code :

    $(document).on("click",".edit",function(){
     var id = $(this).attr('id');
     
     console.log(id);
     if(confirm("edit Data ?")){
      $.ajax({
       type : "POST",
       data: "id="+id,
       url : "editrefbrg.php?id="+id,
       dataType: "json",
       success: function(result){
           $(this).attr('id'); 
       }
      })
     }else{
      return false;
     }
    });

in file editbrg.php i'm use this code :

include "config/koneksi.php";
$result ="";
$id = $_GET['id'];
$hasil = mysql_query("SELECT concat(f,'.',g,'.',h,'.',i,'.',j) as id,nm_barang as nama,masa_manfaat as umur FROM ref_barang WHERE  f = SUBSTRING('".$id."',1,2) AND g = SUBSTRING('".$id."',4,2) AND h = SUBSTRING('".$id."',7,2) 
     AND i = SUBSTRING('".$id."',10,2) AND j = SUBSTRING('".$id."',13,3) ");
     $d = mysql_fetch_array($hasil);

echo'$d["id"] ';

but in href error code like :

Notice: Undefined index: id in D:\xampp\htdocs\skripsiphp\editrefbrg.php on line 5

Julian Schmuckli
  • 3,681
  • 11
  • 37
  • 64
egi
  • 11
  • 2
  • 1
    You're using `type: 'POST'` in your Ajax call, but trying to find it in a `$_GET` array. Use the same on both ends, either GET or POST. Just one thing to note, `echo'$d["id"] ';` would echo *exactly* that, because its inside singlequotes. – Qirel Jul 24 '17 at 13:42
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/mysql-connect)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) can help you decide which. – Qirel Jul 24 '17 at 13:43
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Qirel Jul 24 '17 at 13:44
  • 1
    Also note that you don't seem to be returning JSON, so when you've corrected the code to use the right method, you'd probably have a parse error on your hands. Once that's fixed, `this` inside the success function isn't what you think it is. Once that's fixed, you have a security issue with SQL injection. – adeneo Jul 24 '17 at 13:48

1 Answers1

0

In your ajax you have type:"POST", that should be ajax type:"GET" in pagination.php file because you are accessing from $_GET.

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • @SudiptaMondal not necessarily. i'm correct in how i wrote my answer. but like a commenter said on the original question, if you're trying to access php variables using `$_POST` then the ajax type should match as `POST`. the same goes for `$_GET` and `type:GET` – gorillagoat Jul 24 '17 at 14:02
  • @Qirel i've used this site for several years now, nearly everyday, but i'm new to the dialogue side of it (ie:answering,commenting, etc). i wasn't aware that users could edit other users posts. i assume you have to have reached a certain level to do so, but this seems a little odd to me. what if the editing user is actually misunderstanding the post they are editing? could the original user just re-edit the edited version to be the original post? if my answer is vague, it seems as though you should be able to comment as such instead of just being able to go and edit someone else's post. – gorillagoat Jul 24 '17 at 14:27
  • Users with over 2.000 reputation can edit posts without having it to be approved (otherwise, the author or 2-3 other users has to approve the edit before its visible). If the edit is conflicting or in any way not useful, the author or 2k+ users can *rollback* to previous versions or edit it further. Users with less than 2k rep may still edit, but it has to be reviewed. [**Helpcenter on editing posts for users over 2k rep**](https://stackoverflow.com/help/privileges/edit). So if you're not happy about my improvement, rollback or edit if you want - but all I did was improve the wording ;-) – Qirel Jul 24 '17 at 16:16
  • @Qirel thanks for clarifying. no problem with you're edits. the edit just became very obvious to me since SudiptaMondal commented about my wording, then i was confused because i knew i had worded it differently. but now i know. thanks again. – gorillagoat Jul 24 '17 at 21:00