0

I want to get data by the current URL, so I have set a post value. But it is set for all pages, and I want to unset it if another URL opens. How can I do it?

Please see my code:

if ($currenturl = 'https://example.com/delhi.html') {
    $this->request->setPostValue('location', '289');
}
  • 2
    How can a question based on a typo or lack of basic understanding of PHP get two upvotes? (also counts for the answers b.t.w.) – 7ochem Dec 28 '17 at 13:34
2

Doing currenturl = 'https://example.com/delhi.html' you are setting currenturl to that string which is why it triggers for all pages as you are returning true for that if statement.

What Key Shang said should work however when comparing strings you should use === instead of == as == is ambiguous and could return unexpected results.

So the result should be

if ($currenturl === "https://example.com/delhi.html") {
      $this->request->setPostValue('location','289');
}
3

Try to change = to ==, like below

if (currenturl == "https://example.com/delhi.html") {
      $this->request->setPostValue('location','289');
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.