Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pass a variable through a hyperlink to a new page

Posted on 2008-10-01
12
Medium Priority
?
1,337 Views
Last Modified: 2010-04-21
Hi.  I'd like to pass a variable with/through a hyperlink to another page that will execute a mysql query using the passed variable.  The variable is a result from a query executed.   Page 1 works well (enough for me at least).  I get the following error when I click a hyperlink to go to Page 2:  
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in ...projectsearch_grid.php on line 200.

Page 1 executes a query and displays the results in a simple table.  I want to make the results in the "Project" column hyperlinks that point toward Page 2.  Page 2 would then execute a second query and display the results using the selected project.  Page 1 displays results from a search to find a project(s) for whatever criteria.  Page 2 will display more details about the specific project that the user clicked on.

Page 1 query: SELECT Name, Address, City FROM Projects WHERE City LIKE '%city%'
Page 1 results:  Project 1
                          Project 2
                          Project 3
User clicks on "Project 3"

Page 2 query:  SELECT * FROM `Projects` WHERE `Project_ID`='Project 3'

My best but still probably horrible attempt is below.  Thanks in advance.
197   while($row = mysql_fetch_array($result))
198    {
199	echo '<tr><td valign="top" align="left">';
200	echo '<a href="project_detail.php"  target="_blank">'.$row['Project_Name'].'</a> <form
                     action="project_detail.php" method="post"> <input type="hidden" name="projnumber" value="<?php
                     $row['PROJECT_#']; ?>"></form>';
201	echo '</td><td valign="top" align="left">';
202	echo $row['Type_And_Team'];
203	echo '</td><td valign="top" align="left">';
204	echo $row['Address'];
 
	}

Open in new window

0
Comment
Question by:foxymoron7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 2

Expert Comment

by:idealws
ID: 22620237
At first glance looks like you just forgot to format your echo on line 200. Try the below it should solve your error problem.
// You have:
value="<?php $row['PROJECT_#']; ?>"
 
// Should be:
value="'.$row['PROJECT_#'].'"

Open in new window

0
 
LVL 1

Author Comment

by:foxymoron7
ID: 22621133
Thanks for taking a look idealws.  That change didn't work.  Same error message.  Any other thoughts?
0
 

Assisted Solution

by:ijaaacek
ijaaacek earned 600 total points
ID: 22621852
What about this?

echo "<a href=\"project_detail.php\" target=\"_blank\">$row['Project_Name']</a><form action=\"project_detail.php\" method=\"post\"><input type=\"hidden\" name=\"projnumber\" value=\"$row['PROJECT_#']\"></form>";

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:idealws
ID: 22626059
Modifying it this way should handle the error. A few changes to the code posted by ijaaacek
echo "<a href=\"project_detail.php\" target=\"_blank\">".$row['Project_Name']."</a><form action=\"project_detail.php\" method=\"post\"><input type=\"hidden\" name=\"projnumber\" value=\"".$row['PROJECT_#']."\"></form>";

Open in new window

0
 
LVL 1

Author Comment

by:foxymoron7
ID: 22626233
Thanks to both of you.  That works to make the new window open.  Now I can't seem to get the variable in the "project_detail.php" page.  To test if I was passing the variable through, project_detail.php is only the code below.  The page returns "variable empty".

If you don't mind taking a look to see if you can help I would appreciate it.

Thanks again, idealws and ijaaacek.
<?php
 
 
$projnumber = $_POST['projnumber'];
 
If(!$projnumber)
	{echo "variable empty";}
	print $projnumber;
 
?>

Open in new window

0
 
LVL 2

Accepted Solution

by:
idealws earned 1400 total points
ID: 22626359
You can do it using get instead like so:
<?php
while($row = mysql_fetch_array($result))
   {
echo '<tr><td valign="top" align="left">';
echo "<a href=\"project_detail.php?projnumber=".$row['PROJECT_#']."\" target=\"_blank\">".$row['Project_Name']."</a>";
echo '</td><td valign="top" align="left">';
echo $row['Type_And_Team'];
echo '</td><td valign="top" align="left">';
echo $row['Address'];
 
}
 
 
?>
 
 
project_detail.php
<?php
 
 
$projnumber = $_GET['projnumber'];
 
If(!$projnumber)
	{echo "variable empty";}
	print $projnumber;
 
?>

Open in new window

0
 
LVL 2

Assisted Solution

by:idealws
idealws earned 1400 total points
ID: 22626411
Or like this for using post:
<?php
while($row = mysql_fetch_array($result))
   {
echo '<tr><td valign="top" align="left">';
echo "<form action=\"project_detail.php\" method=\"post\"><input type=\"hidden\" name=\"projnumber\" value=\"".$row['PROJECT_#']."\"><input type=\"submit\" value=\"".$row['Project_Name']."\"></form>";
echo '</td><td valign="top" align="left">';
echo $row['Type_And_Team'];
echo '</td><td valign="top" align="left">';
echo $row['Address'];
 
}
 
?>
 
project_detail.php
<?php
 
 
$projnumber = $_POST['projnumber'];
 
If(!$projnumber)
	{echo "variable empty";}
	print $projnumber;
 
?>

Open in new window

0
 
LVL 1

Author Comment

by:foxymoron7
ID: 22626876


Thanks again idealws.  Both methods worked.  I'm going to go with the GET method.  Two follow up questions is you have time.  Why is it necessary to escape the quotes?  In theory, is it possible to use POST along with a hyperlink?  The GET method works for this specific example because the project # isn't sensitive information but I can imagine wanting to hide some info in the future but use a similar method.  In no way is this a criticism of your POST solution but I didn't like the look of the Submit button in the table of results.  Thanks for all of your help.
0
 
LVL 1

Author Closing Comment

by:foxymoron7
ID: 31502219
Thanks  idealws and ijaaacek
0
 
LVL 2

Expert Comment

by:idealws
ID: 22626993
You can use some java to submit the form with a text link like so I was simply using a simple example of how you can do what your trying to do. Hope this helps:
<script language="JavaScript" type="text/javascript">
<!--
function submitNumber (selectednum)
{
  document.form1.projnumber.value = selectednum ;
  document.form1.submit() ;
}
-->
</script>
 
<?php
while($row = mysql_fetch_array($result))
   {
echo '<tr><td valign="top" align="left">';
echo "<form name=\"form1\" action=\"project_detail.php\" method=\"post\" target=\"_Blank\"><input type=\"hidden\" name=\"projnumber\"><a href=\"javascript:submitNumber('".$row['PROJECT_#']."')\">".$row['Project_Name']."</a></form>";
echo '</td><td valign="top" align="left">';
echo $row['Type_And_Team'];
echo '</td><td valign="top" align="left">';
echo $row['Address'];
 
}
 
?>
 
project_detail.php
<?php
 
 
$projnumber = $_POST['projnumber'];
 
If(!$projnumber)
	{echo "variable empty";}
	print $projnumber;
 
?>

Open in new window

0
 
LVL 2

Expert Comment

by:idealws
ID: 22627013
By the way the above will only work for one form submittion. Im not the best with Java but it is a siomple solution to get your pointed in the right direction.
0
 
LVL 1

Author Comment

by:foxymoron7
ID: 22627151
Thanks!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question