SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Post Method

  1. #1
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Method

    Hi,

    I am using the book Build Your Own Database Driven Web Site Using PHP & MYSQL. I am trying to use the POST method to pass through a value from one page to another. It works fine using the GET method, but POST is not working. Does anybody know why this might be?

    On the first page my code is:

    Code PHP:
    <form action="two" method="post">
    	<div>
    	    <label for="longtextbox">Message:
    	        <input type="text" name="longtextbox" id="longtextbox" />
    	    </label>
    	<div>
    	<input type="submit" value="Go" />
    </form>

    And one the second page I have:

    Code PHP:
    <p style="text-align:left">
      <?php
        $longtextbox = $_POST['longtextbox'];   // $POST_['array value called longtextbox']
        echo 'This is what I put in my message:' . htmlspecialchars($longtextbox, ENT_QUOTES, 'UTF-8');
      ?>
    </p>

    If I use the exact same code on exactly the same pages, and just change the POST to GET then it works.

    Thank you in advance for your help.

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    444
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I create two small files, the first containing the html and the second containing your PHP, modify the 'action' string to suit, it works just fine to me. When you say it "doesn't work", can you be more specific - is the string empty, for example, or do you get an error and if so, what is it? Have you tried displaying the text without callign htmlspecialchars() on it first?
    http://www.firenza.net - my homage to a car from the 1970s

  3. #3
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    The only thing I can see wrong is

    Code:
    <form action="two" method="post">
    where two should be your target php page (two.php ?) Unless you've just used that as a placeholder? Are you getting any error messages or is the variable just not outputting anything?

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    444
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I assumed that would work as the OP says it works if he changes $_POST to $_GET. Does look a bit weird though.
    http://www.firenza.net - my homage to a car from the 1970s

  5. #5
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by droopsnoot View Post
    I assumed that would work as the OP says it works if he changes $_POST to $_GET. Does look a bit weird though.
    Indeed I wasn't sure either but the rest of the code seems fine so I'm not sure what else the problem could be.

  6. #6
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, two is the name of my folder containing a page called index.php.

    The webpage loads just fine if I use GET. The text inputted into the form displays correctly. I've tried this several times now. I've even tried it on two computers, one mac and the other PC (win8).

    I have also tried removing the htmlspecialchars() bit as suggested, as follows:

    input page
    PHP Code:
    <form action="two" method="post">
        <
    div>
            <
    label for="longtextbox">Message:
                <
    input type="text" name="longtextbox" id="longtextbox" />
            </
    label>
        <
    div>
        <
    input type="submit" value="Go" />
    </
    form
    and on the second page

    PHP Code:
    <p style="text-align:left">
      <?php
        $longtextbox 
    $_POST['longtextbox'];
        echo 
    'This is what I put in my message:' $longtextbox;
      
    ?>
    </p>

  7. #7
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ha, I think I have got it.

    My <form action="two"...> is pointing to the folder two, and I want the index of that folder i.e. two/index.php. When I change it to <form action="two/index.php"...> then it works.

    I think it must work with GET and not post because the information is stored in the URL, so it's right there for it to get, whereas with POST it gets left at two the folder, and doesn't get passed through to two the index.php page.

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    444
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, possibly some redirection thing going on in the server when the config tells it which page to open on form submission. Glad it's OK now.
    http://www.firenza.net - my homage to a car from the 1970s


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •