0

I have some code:

 <?php

$findItem = null;
if(!empty(isset($_POST["data"])))
{
  
    $findItem = 1;
}

if($findItem != null){
    echo '<td>Find YOU</td>';
}

?>

Echo doesnt work at all. I don`t know why. isset triggered by js:

<script>
function init() {
        var data = new FormData();
        data.append("data" , 'init');
        var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
        xhr.open( 'post', 'index.php', true );
        xhr.send(data);
    }
</script>
<script src="track.min.js" onload="init()">

Whats problem?

  • 2
    Will the return from `isset()` __ever__ be `empty()`? As `isset()` returns true or false, I suspect not. – droopsnoot Jan 19 '21 at 09:39
  • It works. I debbuged. Problem about echo. $findItem change value. In output in PHPStorm I see text but in browser not. – Alex Penvik Jan 19 '21 at 09:43
  • 1
    Where is the JS code that puts the output returned from your PHP into the page somewhere? – droopsnoot Jan 19 '21 at 09:53
  • `!empty(isset...` makes no sense. Don't pass the output of isset into empty. Check the difference between them (see https://stackoverflow.com/a/1219550/5947043) and decide which one you actually want to use in this scenario. Using both isn't necessary or logical (especially not the way you've written it) – ADyson Jan 19 '21 at 10:04
  • JS in top of index.php. All of this code in index.php. Problem not in isset!! Code with isset works fine. Problem about echo after isset. – Alex Penvik Jan 19 '21 at 10:13
  • 2
    @droopsnoot Using `!empty` here is redundant, but doesn't make the code wrong (`false` is considered to be empty). Alex, the problem isn't in PHP. The problem is in JS, as droopsnoot pointed out in his second comment. The `echo` is working just fine, but it's only returning data to the XHR (not outputting to the page). You have to manually place those contents where you want them by fetching the DOM element where you want this to display and changing it's `innerHTML`. – El_Vanja Jan 19 '21 at 10:37
  • @El_Vanja thanks for the clarification on `empty()` - after typing that I did go to phpfiddle to test it out, but there was a problem with the site so I didn't get any further with it. – droopsnoot Jan 19 '21 at 11:30
  • @El_Vanja I think no because when I try echo only text without if all works fine – Alex Penvik Jan 19 '21 at 12:27
  • The PHP code itself is fine. See [fiddle](https://3v4l.org/SoQpNh). – El_Vanja Jan 19 '21 at 12:31
  • Problem when load js. JS send post and trigger isset and after that I have problem. – Alex Penvik Jan 19 '21 at 20:21

0 Answers0