[SOLVED] html hyper links to send vars to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

[SOLVED] html hyper links to send vars to php

Post by fresh »

hey, I can not think of a way to explain this besides showing you some syntax:

Code: Select all

<?php
//-------------------------
//      mypage.php
//-------------------------
$w_name = $_POST['who'];  //get which link they clicked

if($w_name != "") {  //check to see if who var is empty
echo " You clicked the ".$w_name." link!";
} else {
echo "No name specified!";  //deny if who var empty
}
?>
<form action='http://mypage.php' method='post'>
<a href='#' name='who' onclick='javascript:submit()'>Todd</a>
<a href='#' name='who' onclick='javascript:submit()'>John</a>
<a href='#' name='who' onclick='javascript:submit()'>Bill</a>
</form>

I haven't tried doing this yet in any of my projects, so if someone could show me the proper syntax to accomplish the above therory, I would be much appreciative.. thanks :D
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You would need to create a field and value to be sent in the form with javascript before submitting.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

well how does phpbb do it.. I mean if you goto memberslist and click a name it brings you to that users profile.. which is basically what I am trying to accomplish with links.. so that say some one clicks the link jason.. the user will be taken to mypage.php where Jason's info from the DB will be populated.. or if they click Bill, they will goto mypage.php where Bill's info from the DB will be poulated.. can a field be a link?? I just need to send the php script the users name and it will know where to look.. could someone show me an example.. thanks :D
Last edited by fresh on Mon Nov 01, 2004 7:08 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

instead of just doing javascript:submit() do something like

Code: Select all

<script language="javascript">
function seeWho(name)
&#123;
   url='mypage.php?name='+name;
   document.myform.action=url;
&#125;

</script>
<form name='myform' id='myform' action='http://mypage.php' method='post'>
<a href='#' name='who' onclick='javascript:seeWho('todd')'>Todd</a>
<a href='#' name='who' onclick='javascript:seeWho('john)'>John</a>
<a href='#' name='who' onclick='javascript:seeWho('bill')'>Bill</a>
</form>
you will have to of course dynamically create the name in the function call.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

dynamically create the name: do you mean whilst pulling them from the DB and writing the names to the page??
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

yes. I am assuming that the list won't be fixed so you can't create it in just straight hmtl. If they are fixed then ignore me as I am just rambling.

How are you going to create the list of users that will be displayed?
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Arent the links to the users in phpbb just images inside of links? Dont see why you need to complicate things.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

sorry forgot you where going to be using phpbb.
never mind me. :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

http://uk.php.net/FAQ.html

PHP Manual: first call when having questions about PHP.
Google: second call when having questions about PHP.
Search Function of this forum: : third call when having questions about PHP.
Posting a new message: fourth call when having questions about PHP

Click on the first link below for further details.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

hey no I was just using phpbb as an example.. and yes, I will be enumerating a list of names from my sql db.. I suppose I will try the javascript.. unless there is a php way of doing this.. I just think the less one sees the better off I will be.. thanks :D
User avatar
irealms
Forum Contributor
Posts: 215
Joined: Mon Apr 28, 2003 7:10 am
Location: Leeds

Post by irealms »

I use something like this:

Code: Select all

<?php
//use a while loop to create a list of names
//put this link inside the while loop and $your variable can be pulled from 
//the row
echo '<a href="mypage.php?userselect='.$yourvariable.'" />Link to page</a>';

//on the page where you need it to show the right profile
if (isset($_GET['userselect']))
{
$selectuser = "SELECT * FROM usertable WHERE user='$_GET[userselect]'";
@mysql_query($selectuser, $connect);
}
else
{
//the pages usual code
}



?>
At least this is what i usually do, unless i'm not following what your getting at.
User avatar
fresh
Forum Contributor
Posts: 259
Joined: Mon Jun 14, 2004 10:39 am
Location: Amerika

Post by fresh »

thats what I was looking for, something like that.. thanks alot :D
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

fresh wrote:hey no I was just using phpbb as an example.. and yes, I will be enumerating a list of names from my sql db.. I suppose I will try the javascript.. unless there is a php way of doing this.. I just think the less one sees the better off I will be.. thanks :D
If you follow the link I posted and read the link, you'll understand that this is one of the most frequently asked questions. The PHP manual has dedicated an entire page just to that. So, I'd advise you to read: http://uk.php.net/FAQ.html
Post Reply