Chris.P Posted May 12, 2007 Share Posted May 12, 2007 I'm trying to get a variable from one page to another using variables in the URL. I have this working fine from some pages but not others. So starting on one page I have this section of code. else { header ("Location: user.php?band=$bandid"); } The variables work fine and it takes me to the user.php page. On user.php I am using the following to get the variable in the URL to a variable in the page which also works fine because I can echo the values out just fine. $bandid = $_GET['band']; echo $bandid; My problem comes when I try use the variable on user.php in a link on that page like this. <a href="image_upload.php?band=$bandid">Upload Image</a><br /> Instead of the link showing the contents of the variable it shows me the variable name. Any help I'm baffled. ??? Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/ Share on other sites More sharing options...
Karl33to Posted May 12, 2007 Share Posted May 12, 2007 is that link being used within a code block ? if not it needs to be echo'd, using the short notation is preferable .. <a href="image_upload.php?band=<?=$bandid;?>">Upload Image</a><br /> Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251653 Share on other sites More sharing options...
Chris.P Posted May 12, 2007 Author Share Posted May 12, 2007 Not entirely sure what you mean but the the casting of the variable and the url the variable is being used in are all within the same block of php code if that answers your question. Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251660 Share on other sites More sharing options...
chronister Posted May 13, 2007 Share Posted May 13, 2007 Post the PHP block of code here. I have a feeling you have it like this... <?php code code code echo ' some code some code <a href="image_upload.php?band=$bandid">Upload Image</a><br /> some code'; ?> If you have it in a single quote string, it is a literal string and it will print out as $bandid not the value you have set for bandid. Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251691 Share on other sites More sharing options...
PC Nerd Posted May 13, 2007 Share Posted May 13, 2007 ok: echo "<a .........../var_name=".$variable."'>"; that shoudl work Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251742 Share on other sites More sharing options...
Chris.P Posted May 13, 2007 Author Share Posted May 13, 2007 Post the PHP block of code here. Ok in my user.php page I have the following section of PHP. <?php include_once 'functions.php'; //This includes some common functions $bandid = $_GET['band']; echo $bandid; showLogin(); //This is a function in 'functions.php' ?> Heres the functions.php file that conatins the showlogin() function that called form the user.php file above and contains the link concerned. <?php //login details function loginDetails(){ //db access details $hostname = "xxxxx"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; // (2) Open the database connection $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect to database!");; // (3) Select your database mysql_select_db($dbname) or die ("Unable to select database!"); } //login script function showLogin() { // Check if we have established an authenticated session if (isset($_SESSION["authenticatedUser"])) { $_SESSION["message"] = "You are logged in as ". $_SESSION['authenticatedUser']; echo '<h2><a href="user.php">Control Panel</a><br /> <a href="update_details.php">Update details</a><br /> <a href="image_upload.php?bandid=".$bandid."">Upload Image</a><br /> <a href="logout.php">Logout</a></h2>'; } else { echo '<h1>Login</h1> <form name="form1" id="form1" method="post" action="loginaction.php"> <h2>Username: <input name="loginUser" type="text" id="loginUser" size="8" /> <br /> Password: <input name="loginPass" type="password" id="loginPass" size="8"/> <br /> <input type="submit" name="Submit" value="Go!" class="button"/> </h2> </form> <h2><a href="register_details.php">Register</a><br /> <a href="recoverpass.php">Forgotten password?</a></h2>'; } echo "<h2>". $_SESSION["message"]. "</h2>"; } //End showLogin ?> Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251920 Share on other sites More sharing options...
Chris.P Posted May 13, 2007 Author Share Posted May 13, 2007 ok: echo "<a .........../var_name=".$variable."'>"; that shoudl work That stops the variable name being used but it shows an empty variable in the url now. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251921 Share on other sites More sharing options...
chigley Posted May 13, 2007 Share Posted May 13, 2007 <?php //login details function loginDetails(){ //db access details $hostname = "xxxxx"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; // (2) Open the database connection $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect to database!");; // (3) Select your database mysql_select_db($dbname) or die ("Unable to select database!"); } //login script function showLogin() { // Check if we have established an authenticated session if (isset($_SESSION["authenticatedUser"])) { $_SESSION["message"] = "You are logged in as ". $_SESSION['authenticatedUser']; echo "<h2><a href=\"user.php\">Control Panel</a><br /> <a href=\"update_details.php\">Update details</a><br /> <a href=\"image_upload.php?bandid=$bandid\">Upload Image</a><br /> <a href=\"logout.php\">Logout</a></h2>"; } else { echo '<h1>Login</h1> <form name="form1" id="form1" method="post" action="loginaction.php"> <h2>Username: <input name="loginUser" type="text" id="loginUser" size="8" /> <br /> Password: <input name="loginPass" type="password" id="loginPass" size="8"/> <br /> <input type="submit" name="Submit" value="Go!" class="button"/> </h2> </form> <h2><a href="register_details.php">Register</a><br /> <a href="recoverpass.php">Forgotten password?</a></h2>'; } echo "<h2>". $_SESSION["message"]. "</h2>"; } //End showLogin ?> Try that Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251923 Share on other sites More sharing options...
Chris.P Posted May 13, 2007 Author Share Posted May 13, 2007 Try that Hmm that gives the same result as above, just shows an empty variable. Makes me think the variable is empty but like I say it can't be because I can echo its contents out just fine. ??? Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-251946 Share on other sites More sharing options...
chronister Posted May 13, 2007 Share Posted May 13, 2007 <?php //login details function loginDetails(){ //db access details $hostname = "xxxxx"; $username = "xxxxx"; $password = "xxxxx"; $dbname = "xxxxx"; // (2) Open the database connection $connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect to database!");; // (3) Select your database mysql_select_db($dbname) or die ("Unable to select database!"); } //login script function showLogin() { // Check if we have established an authenticated session if (isset($_SESSION["authenticatedUser"])) { $_SESSION["message"] = "You are logged in as ". $_SESSION['authenticatedUser']; ?> <h2><a href="user.php">Control Panel</a><br /> <a href="update_details.php">Update details</a><br /> <a href="image_upload.php?bandid=<?=$bandid ?>">Upload Image</a><br /> <a href="logout.php">Logout</a></h2> <?php } else { ?> <h1>Login</h1> <form name="form1" id="form1" method="post" action="loginaction.php"> <h2>Username: <input name="loginUser" type="text" id="loginUser" size="8" /> <br /> Password: <input name="loginPass" type="password" id="loginPass" size="8"/> <br /> <input type="submit" name="Submit" value="Go!" class="button"/> </h2> </form> <h2><a href="register_details.php">Register</a><br /> <a href="recoverpass.php">Forgotten password?</a></h2> <?php } echo "<h2>". $_SESSION["message"]. "</h2>"; } //End showLogin ?> Try that. I am very much against echo'ing large chunks of HTML. It makes the code more difficult to find errors and such. If you can echo the $bandid variable then that should work. If it shows up blank, then make sure that variable is set with data Quote Link to comment https://forums.phpfreaks.com/topic/51112-variable-in-url/#findComment-252149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.