jonwish Posted July 13, 2013 Share Posted July 13, 2013 Hi, My first post so please excuse if im ake any mistakes. I have a drop down that I am getting a post variable from with i am then trying to append an URL with. It works but only for the first option. Any help much appreciated. When I echo the $pagelink it shows the correct value but when I add it to the header location it just goes to the homepage of the site (except the first option)! My index.php: <?php // connect to db mysql_connect ("xxxxxx", "xxxxx","") or die (mysql_error()); mysql_select_db ("xxxxxx"); //query $sql = mysql_query('SELECT product_id, name FROM CubeCart_inventory'); $models = array(); while ($row = mysql_fetch_array($sql)){ $models[] = $row; } ?> <form action="config.php" method="POST"> <select name="battery"> <?php foreach ($models as $model) { ?> <option value="<?php echo $model['name']?>"><?php echo $model['name']?></option> <?php } ?> </select> <input type="submit" name="submit" value="Submit" /> </form> my config.php: <?php //var_dump($_REQUEST); $name = $_POST['battery']; $pagelink = str_replace(" ","-",$name); $pagelink = strtolower($pagelink); $pagelink = $pagelink.".html"; header ('Location: http://localhost/carrotcycles/test-category/'$pagelink); ?> Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/ Share on other sites More sharing options...
trq Posted July 13, 2013 Share Posted July 13, 2013 That should be casuing an error. You need to concatinate the variable to your string: header ('Location: http://localhost/carrotcycles/test-category/' . $pagelink); [/code[ Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/#findComment-1440594 Share on other sites More sharing options...
jonwish Posted July 13, 2013 Author Share Posted July 13, 2013 Hi, Thanks for the fast response. I have added the . to concatenate but it is still doing the same thing (only first option works, others just go to index page) Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/#findComment-1440596 Share on other sites More sharing options...
jonwish Posted July 13, 2013 Author Share Posted July 13, 2013 Sorry. I don't understand. If I echo $pagelink I get the post variable shwoing okay? Appologies, im a bit rusty with all this. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/#findComment-1440645 Share on other sites More sharing options...
web_craftsman Posted July 14, 2013 Share Posted July 14, 2013 Try this: <option value="<?php echo htmlspecialchars($model['name'])?>"><?php echo htmlspecialchars($model['name'])?></option> And maybe you need also urlencode($pagelink) Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/#findComment-1440660 Share on other sites More sharing options...
jonwish Posted July 14, 2013 Author Share Posted July 14, 2013 Thanks for the reply. I have added the alterations as you mentioned to secure the data but I think maybe I have not explained myself properly (my fault). The dropdown populates okay and if I echo $pagelink it shows the correct link but when I add $pagelink to the header location it only allows the first item from the db to link okay, the rest just go to the homepage with http://www.xxxxxxxx/index.php I am completely stuck with this as it is echoing the results exactly as it should. Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/#findComment-1440667 Share on other sites More sharing options...
fastsol Posted July 14, 2013 Share Posted July 14, 2013 Maybe the issue isn't the header on the config but rather a header on the page you are linking to. Where does the $pagelink go to and what does that page do with the URL var? If there is a header on that page to go back to the index if something is wrong, maybe that's where the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/#findComment-1440671 Share on other sites More sharing options...
jonwish Posted July 14, 2013 Author Share Posted July 14, 2013 The drop down links to an ecommerce site. Basically: there is a dropdown with a certain categories products. Once a product is selected it would take you to that product page. Quote Link to comment https://forums.phpfreaks.com/topic/280125-passing-variable-to-url/#findComment-1440674 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.