Values sent to PHP via POST are all empty strings

2020-05-06 05:51发布

问题:

I have the following form:

<FORM action="http://url.to.mysite.com/doc.php" method="post">
    <INPUT type="text" id="name">
    <INPUT type="text" id="id">
    <INPUT type="submit" value="Send">
    </P>
 </FORM>

Unfortunately, whatever is typed into the text fields, the PHP script receives only empty strings in every $_POST variable. All of the variables are set, just empty. What can be the cause of that?

回答1:

You are missing the name properties in your html:

<FORM action="http://url.to.mysite.com/doc.php" method="post">
    <INPUT type="text" name="name" id="name">
    <INPUT type="text" name="id" id="id">
    <INPUT type="submit" value="Send">
</FORM>


回答2:

Only form elements with a name attribute will be sent to the PHP script (same with POST) Try this:

<form action="http://url.to.mysite.com/doc.php" method="post">
    <input type="text" id="name" name="name">
    <input type="text" id="id" name="id">
    <input type="submit" value="Send">
</form>


回答3:

You need to add the name attribute to your input tags. Example: <input type="text" name="name" id="name" />