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:


<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:

<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.

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?

The only thing I can see wrong is

<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?

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.

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


<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


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

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.

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.