Jump to content

post with hyperlink


ngreenwood6

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.