-3
<?php
include "global_database_function.php"; //include config file
$nama_kategori = $_GET['nama_kategori']; //get from previous link
//global $nama_kategori; 
//i can't use global $name_kategori

$idkategori = $_GET['idkategori'];
?>
<html>
<head>
</head>
<body>
<?php
//continue only if $_POST is set and it is a Ajax request
if(isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{   
  //here is the code i try to show, i try to use:
   htmlspecialchars($nama_kategori);
}
?>
</body>
</html>

if I put htmlspecialchars($nama_kategori); between body and if(isset($_POST) && isset, the value of $nama_kategori is visible.

Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
  • 1
    I'm confused, could you please clarify your issue? – Epodax Oct 01 '15 at 10:29
  • First, `$_POST` is **always** set. It's a superglobal, it's set at runtime, so your verification will be true no matter what. Test `$_POST` for emptiness (and beware that it may fail as well, since the submit button - if any - count as form element), or use `isset` on a specific post field (eg `if (isset($_POST['idkategori']));`) – al'ein Oct 01 '15 at 10:37
  • @Epodax i'm sorry, i have problem, I can not bring up the value `$nama_kategori` taken from the previous file links. if I put `$nama_kategori` between the body and if, the value fetched from the previous file links can be raised. but after I put `$nama_kategori` in a `IF(isset($_POST)` conditional, the value does not appear – Rifqi Ramadhani Oct 01 '15 at 10:40
  • Second: `isset($_SERVER['HTTP_X_REQUESTED_WITH'])` there is no such field on `$_SERVER` as `HTTP_X_REQUESTED_WITH` unlees you have set it before, hardcoded, and didn't show us. If you did (I hope you did), it will evaluate to true even if it's empty. `isset` evaluates true if the variable **exists**, while `empty` does the same if the var contains something. – al'ein Oct 01 '15 at 10:41
  • Then that's because it doesn't enter the if clause – Epodax Oct 01 '15 at 10:42
  • maybe you are missning `echo` ?? `echo htmlspecialchars($nama_kategori);` – Santa's helper Oct 01 '15 at 10:43
  • 1
    Third: [`strtolower`](http://php.net/manual/en/function.strtolower.php) returns a **string** and not a boolean, so it won't be evaluated as true or false. [**EDIT**: I stand corrected on this one, I didn't scrolled the code to the left to see you were comparing it to a string, my bad...] – al'ein Oct 01 '15 at 10:44
  • Conclusion: you're using `if` statement the wrong way. – al'ein Oct 01 '15 at 10:44
  • Anyway I'm voting your question as *unclear what you're asking*, because it is. Please, consider taking the site's [tour](http://stackoverflow.com/tour) and visit the Help Center to learn about [how to make questions](http://stackoverflow.com/help/on-topic). – al'ein Oct 01 '15 at 10:47

1 Answers1

0

Let it store into session for future as

$nama_kategori = $_GET['nama_kategori'];
session_start();
$_SESSION['t']=$nama_kategori;

and when needed

if(isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{ 
session_start();  
echo $_SESSION['t'];
}
Rohit Kumar
  • 1,948
  • 1
  • 11
  • 16