PHP $ _POST返回空值

时间:2014-04-06 07:03:01

标签: php html post

这是我的HTML代码:

<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
<fieldset>
<legend>Subscribe:</legend>
<label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
<label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail"   /><br />
<input style="width: inherit;" type="submit" value="Subscribe" />
</fieldset>
</form>

这是subscribe.php文件:

<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');
print_r($_POST);
if (isset($_POST["subName"]) && isset($_POST["subEmail"])){
$subUser = $_POST["subName"];
$subEmail = $_POST["subEmail"];
echo "$subUser"."<br />"."$subEmail";
}
?>

我在互联网上真的尝试了很多东西,似乎没有什么对我有用。任何想法?
看起来get方法也适用于此...

1 个答案:

答案 0 :(得分:0)

这应该有效:

<form style="width: 20%; margin: auto;" action="subscribe.php" method="post" id="subscribeToNews">
    <fieldset>
        <legend>Subscribe:</legend>
        <label for="subName">First Name:</label><br /><input type="text" id="subName" name="subName" /><br />
        <label for="subEmail">Email:</label><br /><input type="text" id="subEmail" name="subEmail"   /><br />
        <input style="width: inherit;" type="submit" value="Subscribe" />
    </fieldset>
</form>

<?php
$con = mysqli_connect('95.76.197.98','root','','accounts');

if($_POST) {
    $subName = mysqli_real_escape_string($con, strip_tags($_POST['subName']));
    $subEmail = mysqli_real_escape_string($con, strip_tags($_POST['subEmail']));

    if(isset($subName) && !empty($subName) && isset($subEmail) && !empty($subMail)) {
        echo 'Name: '.$subName.'<br> Email: '.$subEmail;
    }
}
?>