Hi to All,

Is this code can be done with link <a hef=''>?

<?php
 
  echo "<form action='page.php' method='post'>";
  echo "<input type='submit' name='submit' value='Go to Page'>";
  echo "</form>";
  
  if($_POST['submit']=="Go to Page")
  {

  }

?>

in the code above, once the user click the button, the page load and execute whatever script inside *if($_POST=="Go to Page")*. My question is that, instead of using button, i want to use a link <a href='page.php'>?

Any Idea? Please help.

Thanks a lot.

Recommended Answers

All 3 Replies

If you want to process in the same page, use form. Otherwise you can use link. But you can't retrieve the data with '$_POST[]' by pass with the link, whether the form can be retrieve both '$_POST' and '$_GET'. Here is example below:
With the anchor link:

<a href="page.php?param1=blabla&param2=blabla">Go to page</a>

# page.php

$param1 = $_GET['param1'];
$parem2 = $_GET['param2'];
//other statements for processing

With form:

<form name="myform" action="page.php" method="post">
<input type="text" name="param1" />Param1<br />
<input type="text" name="param2" />Param2<br />
<input type="submit" value="Submit" name="submit" />
</form>

#page.php

$param1 = $_POST['param1'];
$param2 = $_POST['param2']; 

//you can also use $param1= $_GET['param1'] and $param2 = $_GET['param2'] respectively when you are using the get method with that form

//further processing

Hope this help.

Hi,

if you want to submit the form by clicking a link (if you want your link works as a submit button) then you have to submit the form by using javascript.

<script>
function myformsubmit()
{
   document.myform.submit();
   location.href='submitpage.php';
}
</script>
<?php
 
  echo "<form action='page.php' name='myform' method='post'>";
   echo "<input type='text' name='username'>";
  echo "<a href='Javascript:void(0);' onclick='myformsubmit();'>Go To Page</a>";
  echo "</form>"; 

?>

//submitpage.php file

 if($_POST['submit'])
  {
     $username = $_POST['username']; 
  }

Hope this helps you. :)

commented: You point me the way ... :) Zero13 +6
commented: useful +4

Thanks Zero13 and pbcomput. Your suggestion really helps. ^^,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.