Gacci
Gacci

Reputation: 1398

$_REQUEST works but $_POST does not

I have a HTML form with post method

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
    <script type='text/javascript' src='scripts/scrollbar.js'> </script>
    <script type='text/javascript' src='scripts/string.js'> </script>

    <link type='text/css' rel='stylesheet' href='styles/defaults.css'/>
    <link type='text/css' rel='stylesheet' href='styles/header.css'/>
    <link type='text/css' rel='stylesheet' href='styles/content.css'/>
    <link type='text/css' rel='stylesheet' href='styles/preview.css'/>
    <link type='text/css' rel='stylesheet' href='styles/key-value.css'/>
    <link type='text/css' rel='stylesheet' href='styles/notice.css' />  
    <link rel='stylesheet' type='text/css' href='styles/footer.css' />

    <meta http-equiv='content-type' content='text/html;charset=utf-8' />
</head>
<body>
<nav id='navigation' class='alternative'>
    <section id='logo'> <a href='/.'>im</a> </section>
</nav>  <div class='content alternative'>

<form class='A4 page' method='post' action='includes/process-book-submission.php?action=add&mid=-1&isbn=0309211794&quality=New&scheduled=Fri Jan 15 2016&comments=&price=77.11&extras=&deal=sell&pendings=YES&seller=1'>
    <table class='im key-value' id='preview'>
        <caption> 
            <h1>Item's Information Review</h1> 
            <small>(You can always make changes in the future)</small>
        </caption>
        <tbody>
            <tr> 
                <td><strong>Title</strong></td> 
                <td id='title'> <label>Approaches for Ecosystem Services Valuation for the Gulf of Mexico After the Deepwater Horizon Oil Spill: Interim Report</label> </td> 
            </tr>
            <tr>
                <td><strong>Authors(s)</strong></td> 
                <td id='authors'> <label>Committee on the Effects of the Deepwater Horizon Mississippi Canyon-252 Oil Spill on Ecosystem Services in the Gulf of Mexico, Ocean Studies Board, Division on Earth and Life Studies, National Research Council</label> </td> 
            </tr>
            <tr> 
                <td><strong>ISBN</strong></td> 
                <td id='isbn'> <label>0309211794<label> </td> 
            </tr>
            <tr> 
                <td><strong>Book Condition</strong></td> 
                <td id='quality'> <label>New<label> </td> 
            </tr>
            <tr> 
                <td><strong>Available Starting</strong></td> 
                <td id='accessible'> <label>January 15th, 2016<label> </td> 
            </tr>
            <tr> 
                <td><strong>Available For</strong></td> 
                <td> </label>Sell</label> </td>
            </tr> 
            <tr>
                <td><strong>Price</strong></td> 
                <td id='price'> <label>$77.11<label> </td> 
            </tr>

            <tr>
                <td><strong>Include My Pending List</strong></td>
                <td>
                    <label>YES</label>
                </td>
            </tr>

            <tr> 
                <td><strong>Comments</strong></td> 
                <td></td> 
            </tr>
        </tbody>
    </table>
    <div style='text-align: center; padding: 25px 0px;'>
        <button class='im2' onclick="location.href='../index.php'">Cancel</button>
        <button class='im2'>Continue</button>
    </div>
</form>

Then I submit the page and go to page.php. If I print_r $_REQUEST or if I print_r $_GET these two work, but when I print_r $_POST it is empty, can anybody please explain what is happening, why I am not able to use $_POST even when I specify method='post' in my form

Upvotes: 0

Views: 511

Answers (3)

cn0047
cn0047

Reputation: 17051

Your form don't have input fields, that's why $_POST is empty.
You don't send anything to server through POST, and your $_GET not empty because you pass parameters at url in action parameter of form, for the same reason $_REQUEST not empty too.

Upvotes: 1

Robert
Robert

Reputation: 20286

The problem is that you use redirect instead of submit

change

<button class='im2'>Continue</button>

to

<input type="submit" value="continue" name="submit">

the other way is to trigger form.submit event on button click.

Edit:

You get variables via $_GET because you set them in action URL. Also there are no inputs to be send via post. So you need create some via <input> html tag or <textarea> etc.. pay attention you need to set the name attribute for these html tags to be properly send to server.

example:

put this code <input type="text" name="isbn" value="0309211794"> inside your <form></form> with code I posted above and click submit

Upvotes: 3

Lelio Faieta
Lelio Faieta

Reputation: 6663

What you posted is not a form but just a link to another page.

You are passing to it some data after the ? In the url that correctly you will find in the target page via $_get.

To use a form and post method you have to remove all the info after the ? In the url and replace each of the variable=value pair with:

<input type="hidden" name="thevariable" value="thevalue">

Where each time thevariable and thename are one of the pair you want to post.

Then you will be able to access the data via $_post

Upvotes: 2

Related Questions