Jump to content

Pass Specific Infomation On A Button Click


Recommended Posts

Greets!

 

I am interested on how to pass a variable through a button click to a different page. I do not want to do this through the url as shown below

>http://w.x.com/test.php?var=12

I figured I could set a session variable, but not sure how to set one based on the click of a button. The basic idea is I have a grid with (say 5) selections. Each row has a button (or something similar). Any button click goes to the same page (different than the page they are oin, but ending at the same page). And the resulting page will show information based on the button that was clicked.

 

I probably overexplained for the majority of you, but I wanted to explain it well enough.

Thanks!

Link to post
Share on other sites

You could submit a form value using method="post". That way the variable would not show in the url.

 

Something like this:

 

><form id="var" action="test.php" method="post">
<button type="submit" name="var" value="12">Click Here</button>
</form>

 

Then in test.php, assign $_POST[var] to whatever variable that you use in your script for the value passed to it.

 

Like:

 

><?php
$input = $_POST['var'];
echo $input;
?>

 

It would also be a good idea to write some code to test the contents of the $_POST value to make sure that nothing is being passed to your script that you are not expecting and don't want.

Link to post
Share on other sites

So you are saying to create a FORM for each button visible? Hm. That will probably work! I keep thinking that there is only one form on a page allowe, but that is crazy talk. Thanks! I guess I have been spoiled in ASP. I could write this page in less than 2 minutes in ASP... But PHP is a little easier on the wallet.

Thanks again!

Link to post
Share on other sites

You could have one form, with multiple inputs or buttons.

 

><form id="var" action="test.php" method="post">
This button will pass the value var=12  <button type="submit" name="var" value="12">Click Here</button><br />
This button will pass the value var=14  <button type="submit" name="var" value="14">Click Here</button><br />
This button will pass the value var=16  <button type="submit" name="var" value="16">Click Here</button><br />
This button will pass the value var=18  <button type="submit" name="var" value="18">Click Here</button><br />
This button will pass the value var=20  <button type="submit" name="var" value="20">Click Here</button>
</form>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...