ngreenwood6 Posted May 28, 2009 Share Posted May 28, 2009 I am trying to create a page that has hyperlinks that can post data. I want a user just to be able to click a link on a page but have it send some "$_POST" data to another page. I know that I could just pass it through the url but I do not want the user to see the data passed. Is this possible and if so how would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/ Share on other sites More sharing options...
anupamsaha Posted May 28, 2009 Share Posted May 28, 2009 I am trying to create a page that has hyperlinks that can post data. I want a user just to be able to click a link on a page but have it send some "$_POST" data to another page. I know that I could just pass it through the url but I do not want the user to see the data passed. Is this possible and if so how would I do that? Create a form and set hidden elements in it in your page. On the click event of the links, call a javascript function, that will set values to those form hidden elements and call the post() method. Its a pure JavaScript solution. No, PHP required. Hope this will help. Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844120 Share on other sites More sharing options...
Dathremar Posted May 28, 2009 Share Posted May 28, 2009 What anupamsaha said or try using AJAX. If you are expecting some kind of response to the post method Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844122 Share on other sites More sharing options...
GingerRobot Posted May 28, 2009 Share Posted May 28, 2009 Or you could link to a PHP page that uses cURL to post the data. In short, there's a few different ways to do what you're after. Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844127 Share on other sites More sharing options...
ngreenwood6 Posted May 28, 2009 Author Share Posted May 28, 2009 anupamsaha could you give me an example. I know php but I am not too good with javascript. I know how to do some javascript but I dont know how to post data with it. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844130 Share on other sites More sharing options...
anupamsaha Posted May 28, 2009 Share Posted May 28, 2009 anupamsaha could you give me an example. I know php but I am not too good with javascript. I know how to do some javascript but I dont know how to post data with it. Any help is appreciated. OK. Here we go... <html> <head> <title>page title</title> <script type="text/javascript"> <!-- function doPost() { var objForm = document.getElementById('idForm'); objForm.getElementById('element1').value = 'some value'; objForm.method = 'post'; objForm.action = 'name_of_the_script_where_data_to_be_posted'; objForm.post(); } //--> </script> </head> <body> <a href="javascript:void(0)" onClick="javascript:doPost()">Some text</a> <form id="idForm"> <input type="hidden" id="element1" name="element1" value="" /> </form> </body> </html> Hope this will help. Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844147 Share on other sites More sharing options...
ngreenwood6 Posted May 28, 2009 Author Share Posted May 28, 2009 Thanks for the reply. I understand the code that you gave me but I have one last question. The reason I wanted to do this was because I want to pull a list from my database. I want the titles to be hyperlinks that the user can click on and it will post the id of the title to the next page to get the full article. However I am not sure how to do this using the method you have shown me. I can make it do one hyperlink with your method but not multiple ones? Can you help me understand? Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844170 Share on other sites More sharing options...
ngreenwood6 Posted May 28, 2009 Author Share Posted May 28, 2009 Also I tried this code: <html> <head> <title>page title</title> <script type="text/javascript"> <!-- function doPost() { var objForm = document.getElementById('idForm'); objForm.getElementById('element1').value = 'some value'; objForm.method = 'post'; objForm.action = 'test.php'; objForm.post(); } //--> </script> </head> <body> <a href="javascript:void(0);" onClick="javascript:doPost()">Some text</a> <form id="idForm"> <input type="hidden" id="element1" name="test" value="test" /> </form> </body> </html> And it is not working. When ever I try to submit it I get a "Object doesn’t support this property or method" error on line 8 char 18. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844173 Share on other sites More sharing options...
ngreenwood6 Posted May 28, 2009 Author Share Posted May 28, 2009 Can anyone offer me a better solution? Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-844403 Share on other sites More sharing options...
anupamsaha Posted May 31, 2009 Share Posted May 31, 2009 Sorry for the late reply. Please try the following: <?php // MySQL Connection $conn = mysql_connect('localhost', 'username', 'password') or die('Database connection failed'); // Select DB mysql_select_db('dbname') or die('Database selection failed'); // SQL to get list (title and ids) $sql = 'SELECT `id`, `title` FROM `table_name` ORDER BY `title`'; // Run query $rs = mysql_query($sql) or die('Error in SQL: ' . $sql . '<br />Reason: ' . mysql_error()); ?> <html> <head> <title>page title</title> <script type="text/javascript"> <!-- function doPost(intId) { var objForm = document.getElementById('idForm'); objForm.getElementById('articleId').value = intId; objForm.method = 'post'; objForm.action = 'article_details.php'; objForm.post(); } //--> </script> </head> <body> <?php // Create links while ( $row = mysql_fetch_assoc($rs)) { echo '<a href="javascript:void(0);" onClick="doPost(' . $row['id'] . ')">' . $row['title'] . '</a><br />'; } ?> <form id="idForm"> <input type="hidden" id="articleId" name="articleId" value="" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/160019-post-with-hyperlink/#findComment-846181 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.