1

I want to know how to pass a (or couple) variable from one page to another in php without any form and by using a link.

I know that session is the best option but how to I implement it in the following scenario:

Delete -> process.php Add -> process.php (same page like above)

I want to pass a hidden variable that tells me what link is clicked and hence what function to call. Is there anyway not use $_GET and use sessions?!

Thanks,

tafteh
  • 17
  • 1
  • 1
  • 8

4 Answers4

4

All HTTP requests are independent of one another. Data that you have access to includes:

  1. Variables passed through the URL ($_GET)
  2. Variables posted through a form ($_POST)
  3. Cookies
  4. Session data

Since you are opposed to using a $_GET request, and both links go to the same page, you will need some other way to determine which link is clicked. I would recommend using a form, and passing the data through a hidden input type.

Example:

<form action='process.php' method='post'>
   <input type='hidden' name='action' value='add' />
   <button onClick='submit();'>
      Add
   </button>
</form>

<form action='process.php' method='post'>
   <input type='hidden' name='action' value='delete' />
   <button onClick='submit();'>
      Delete
   </button>
</form>

<style type='text/css'>
button {
   background:none!important;
   border:none; 
   padding:0!important;
   border-bottom:1px solid #444; 
   cursor: pointer;
}
</style>

In your process.php script, you will then check against $_POST['action'] to see if it's equal to "add" or "delete". The style tag I added is something I found from SO user adardesign (*) on how to style a button like a link.

EDIT: To answer your question on whether or not you can use sessions to accomplish this, I would say that it's possible, but probably not worth it.

I would probably never decide to go this route, but what you could do is have an AJAX request which gets triggered when the user clicks one of those links. The AJAX request would call a PHP script which sets the session equal to "add" or "delete". In your success callback, you would then redirect the user to the process.php page, where you then be able to check the value of your $_SESSION variable. Again, it's probably not worth doing it this way, but it is possible.

Community
  • 1
  • 1
Joe Majewski
  • 1,611
  • 3
  • 18
  • 31
0

At some point, you will have to pass some data back to the server to indicate what item you want to manipulate, but after that you can keep it in the session.

For instance, if you are starting with a list of articles and the users selects one, you have to have a way to communicate what article the user has chosen. This is typically done with a parameter on the link, like article.php?id=5. When this id value is submit to the server, you can add it to the $_SESSION and keep track of it that way. You can then decide to edit, delete, etc using that id stored in the session. When your user wants to work on a new article, they click a link with a new id parameter, and you assign that new id to the session.

If you're concerned about security, and user's rights to edit or view certian content, this is not sufficient - you will need to check that the user has rights to perform the action with that content at every step. The interface requests to view an article, and your backend verifies that the user is permitted to do so before the content is served. The interface requests to delete the article, amnd your backend verifies that's OK before it's done.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
0

I think ajax would be an option(I use my own libraries for ajax requests, but i think they are easily understable, feel free to ask the code), but you need a PHP File to handle the ajax request:

<script type='text/javascript'>
function navigate(event){
   event.preventDefault();
   var action=this.innerHTML;
   var link=this.href;
   jx.post("controller.php", {action:action}, {onComplete:function(response){
      location.href=link;
   }});
}
</script>

<a href="process.php" onclick='navigate(event)' action="add">Add</a>
<a href="process.php" onclick='navigate(event)' action="delete">Delete</a>

The PHP file would be like this:

<?php session_start();
$_SESSION["action"] = $_POST["action"];
?>

Then, when the navigate function redirects to process.php, you will already have the link stored in a session variable.

Ayway, if you explain the purpose of this behavior, we could, maybe, find a better solution.

sergio0983
  • 1,232
  • 8
  • 15
-1

If you you to use hidden variable so $_GET is not the solution. What I suggest if you still want to use $_GET just use md5 hash to pass your variables.

<a href="process.php?Action=<?php echo md5('Delete');?>">Delete Action</a>
<a href="process.php?Action=<?php echo md5('Add');?>">Add Action</a>

Using md5 you can be sure that your variable is in some how crypted at least for your users.

How to get action?

$action = $_GET['Action'];
if(md5('Delete')==$action){
/* Delete action */
}elseif(md5('Add')==$action){
/* Add action */
}
Bassem Rabia
  • 206
  • 2
  • 7
  • Thanks, I Like the hashing method. Any chance of not using $_GET method? – tafteh Sep 08 '14 at 17:35
  • You can use $_POST but in this case you need to do more steps..do you need to keep your variables away from users? May be I can find a better solution for you – Bassem Rabia Sep 08 '14 at 18:12
  • Yes. I would like to take away all the variables needed to pass from the users. I prefer nothing shows up in the address bar. – tafteh Sep 08 '14 at 19:21
  • I recommend you then to use ajax and $_POST, so you will send your variables to your new page, check it and feel free to ask me for any help :D – Bassem Rabia Sep 08 '14 at 20:03