This question already has an answer here:
I have gone through some tutorials in W3Schools and others on HTML and PHP but I need a little clarity on how stuff works. I am trying to transport a value via URL using PHP from page1 to page2. Although I have a fair bit of idea on how to do it using HTML
Page1:
<form method="post" action="page2.php">
FIRST NAME <input type="text" name="fname"><br>
LAST NAME <input type="text" name="lname"><br>
<input type="submit" name="sub" value="sub">
</form>
Page2:
<?php
$fname=$_POST['fname'];
$lname=$_POST['lname'];
echo $fname."-".$lname;
?>
I want to use only PHP to transport values, I was planning to use a tag without using a form but I am sure it is not the right way because I won't be able to fetch values on page 2. Plz someone help me out clear these basics.
<?php
$name="hello world";
echo '<a href="page2.php?name='.$name.'"><input type="button name="sub" value="submit"></a>';
?>
</div>
You're going about it backwards. POST data goes in the body of the request, not in the URL. GET data goes in the URL.
If you want to have the data in the URL, you would change your form tag to:
<form method="get" action="page2.php">
and the page2.php code to:
<?php
$fname = $_GET['fname'];
$lname = $_GET['lname'];
echo $fname . "-" . $lname;
?>
Please note that this has a number of vulnerabilities (such as XSS) and is not how you should do these things.
You have to use 'GET'.so your data pass With Append into URL.
Page 1
<form method="GET" action="page2.php">
FIRST NAME <input type="text" name="fname"><br>
LAST NAME <input type="text" name="lname"><br>
<input type="submit" name="sub" value="sub">
</form>
Page 2
<?php $fname=$_GET['fname']; $lname=$_GET['lname']; echo $fname."-".$lname; ?>