-4

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>';
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Im786
  • 25
  • 5

2 Answers2

4

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.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • But I read in W3Schools that if I want to keep the data secure I must use "post" instead of "get" method. – Im786 Dec 16 '17 at 04:30
  • No data going over the internet can hope to be secure unless it's been encrypted with HTTPS. But GET parameters are part of the URL, which has different exposure than POST (which is typically logged, for example). They're both available under `$_REQUEST`, so if you want to be able to provide URLs containing the parameter _and_ process POST requests from forms, it's an option. In my experience this is rarely necessary. Don't forget that you can also use the PHP `$_SESSION` to store data that will automatically be associated to site users via cookie, without passing back and forth. – erik258 Dec 16 '17 at 04:39
  • @DanFarrell Thanks, I might look stupid asking this but is it possible not use a form and still send data across to the other page? I know about $_SESSION but I need a bit of clarity, if we could at all send variables to another page without using a form or $_SESSION? – Im786 Dec 16 '17 at 04:47
  • @nogad Dear could you plz suggest some resource better than W3Schools? – Im786 Dec 16 '17 at 04:48
  • @Im786 In general, yes, you should post sensitive data rather than putting it in a URL, as other commenters have explained. I’m not saying what you should do as a best practice for whatever your actual use case is. I’m answering your question. You said, “I am trying to transport a value via URL using PHP.” – elixenide Dec 16 '17 at 05:02
  • Thanks Ed, wanted to upvote you but am down on points. I am facing a confusion, can we transport value to another page without using session or a form? – Im786 Dec 16 '17 at 05:04
  • Having been doing web programming stuff for over 10 years at this point, and having used it ever since 2006 or whenever, I'd defend w3schools from being "full of crap information". I would not consider it the best resource, but their "try it editor" certainly predated fiddles and snippets, and I can't think of any examples off hand of blatant inaccuracy. Of course, beginner tutorials will never contain production code. Still, it's certainly not my go-to. – erik258 Dec 16 '17 at 05:11
  • 2
    @Im786 Yes, using AJAX or web sockets, but even that will be hard to do without session identifiers. If you don’t want to get into a lot of JavaScript, your only options are forms, sessions, or links with embedded parameters. It’s hard to tell from your question exactly what you want to do, but using forms is almost certainly the best way for you to get started. Other options are mostly much more difficult to get right. – elixenide Dec 16 '17 at 05:12
  • 1
    @DanFarrell I think it has gotten much better, but it used to have lots of invalid (not just non-production) code and outright incorrect information. I started using it probably 15 years ago, and it was notoriously unreliable. The few times I’ve been back in the last decade, it seems to have been much, much better. – elixenide Dec 16 '17 at 05:13
  • http://www.w3fools.com/ would disagree i have seen 100% crap on that stie –  Dec 16 '17 at 05:16
-2

You have to use 'GET'.so your data pass With Append into URL.

  1. 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>
    
  2. Page 2

    <?php $fname=$_GET['fname']; $lname=$_GET['lname']; echo $fname."-".$lname; ?>
    
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140