0

I will have a query that return a set of results, and these results will be in hyperlink form as shown below:

echo "<td><a href='abc.php?cif=" . $row['cif'] .  "'>{$row['cif']}</td>";

Now user get to click on this hyperlink and get routed to abc.php?cif=$cif..

My question is, is it possible to only show abc.php to user, just like a POST method, and $cif remains available at abc.php?

SuicideSheep
  • 4,542
  • 15
  • 54
  • 99
  • 1
    Perhaps you may want to do something like that: http://stackoverflow.com/questions/3418044/setting-post-variable-without-using-form .. Setting them manually and using, then, an header, should be working, no? Or, perhaps, you should use sessions instead.Why aren't you using sessions? – briosheje Mar 10 '15 at 14:31
  • You have to use either post or get and you can do it via ajax.. – Mustafa Toker Mar 10 '15 at 14:31
  • 1
    If you want to get the browser to send a post request on an anchor, it will require some javascript. Unlike faking a post request on the server side however. it does look like its possible to create a virtual form: http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – Flosculus Mar 10 '15 at 14:35

2 Answers2

1

As @Flosculus said above, the "best" solution to simulate a post request is doing something like proposed here: JavaScript post request like a form submit

However, despite it's surely a reliable solution, I'm wondering you just don't use sessions instead, something like:

From the page where you set the cif variable:

session_start();
$_SESSION['cif'] = $row['cif'];

In abc.php:

session_start();
if (isset($_SESSION['cif'])) {
   // Do what you need
}

EDIT::

Another (possible) solution is setting an hidden input and silently submit a form when you click on an anchor, like this:

From your example, instead of:

echo "<td><a href='abc.php?cif=" . $row['cif'] .  "'>{$row['cif']}</td>";

You do this:

When you print all the entries, please add this first (from PHP):

<?php
echo <<<HEADER
    <form action="abc.php" method="post" id="submitAble">
    <input type="hidden" name="cif" id="cif" value="{$row['cif']}">
    <table>
HEADER;
     // Get data from your query.. Here is an example:
     while ($row = mysli_fetch_assoc($query)) {
         echo <<<ENTRY
      <tr>
          <td><a href="#" class="cifSetter" data-cif="{$row['cif']}">{$row['cif']}</a></td>
       </tr>
ENTRY;
     }
echo "</table> <!-- \table collapse --></form> <!-- \form collapse -->";
?>

Then, if you're using jQuery (thing that I'm recommending), simply add an event listener in javascript, like this:

$('.cifSetter').on('click', function(e) {
   e.preventDefault();
   $('#cif').val($(this).data('cif'));
   $('#submitAble').submit();
});

If you don't have jQuery, use this instead:

var cifSetter = document.getElementsByClassName('cifSetter');
for (var i = 0; i < cifSetter.length; i++) {
   cifSetter[i].addEventListener('click', function(e) {
      e.preventDefault();
      var cif = document.getElementById('cif');
      cif.value = this.dataset.cif;
      document.getElementById('submitAble').submit();
   });
}

In both ways, whenever an anchor gets clicked, it will prevent its standard behavior (redirecting) and will instead set the value of an hidden field to the value of the CURRENT "cif" and submit the form with the desired value.

To retrieve the desired value from abc.php, just do this:

$cif = $_POST['cif'];

However, keep in mind that the hidden field is editable by the client (most persons won't be able to edit it, though), therefore you should also sanitize your data when you retrieve it.

Community
  • 1
  • 1
briosheje
  • 6,538
  • 2
  • 31
  • 48
  • I think session is not applicable because I will not know which row of $cif will user clicks. There will be N rows of $cif, each with different value – SuicideSheep Mar 10 '15 at 14:48
  • Wait wait wait wait. So there are more anchors that the user can click? I mean, are there more links that the user can click with different values? – briosheje Mar 10 '15 at 14:49
  • Yes..depending on the results return from the query – SuicideSheep Mar 10 '15 at 14:50
  • @Mr.SuicideSheep: I've updated my answer. Please keep in mind that I can't be literally inside your mind and you script, so take inspiration from that, but don't take it as a working code, just get the idea from it ;) – briosheje Mar 10 '15 at 15:10
  • I'm sorry that I'm a new beginner in this area. I've added an alert statement in the JQuery section but it seems like it wont be trigger? – SuicideSheep Mar 10 '15 at 15:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72685/discussion-between-mr-suicidesheep-and-briosheje). – SuicideSheep Mar 10 '15 at 15:32
0

Sessions could do it but I'd recommend to just use $_POST. I dont get why you wouldn't want to use POST.

Loko
  • 5,956
  • 11
  • 43
  • 75