如何使用post方法获取值

时间:2019-06-13 11:24:52

标签: php post

我正在尝试使用post方法打印在表单中提交的值。但是我什么都不能显示。提交表单后,将显示任何内容。连回声中的句子都没有。我无法弄清楚我的代码出了什么问题。预先感谢。

我尝试使用if($_SERVER["REQUEST_METHOD"]=="POST"){},尝试将变量放入isset(),但仍然无法正常工作。

register.php

<?php
session_start();
include('config.php'); // this is database connection file
error_reporting(0);
echo $_POST["name"]; 
?>

config.php

 <?php

$con = mysqli_connect("localhost","root","","music_pro");

if(mysqli_connect_errno()){

echo"Failed to connect : " . mysqli_connect_error(); 

}

?>

html代码

<form method="post" name="register" action="register.php">
    <label for="name"><b>Name</b></label>
    <input type="text" name="name" placeholder="Enter your name" required>
    <label for="email"><b>Email</b></label>
    <input type="text" name="email" placeholder="Enter your email id" 
     required>
    <label for="uname"><b>User ID</b></label>
    <input type="text" name="uname" placeholder="Enter your user id" 
     required>
    <label for="password"><b>Password</b></label>
    <input type="password" name="password" placeholder="Enter your 
     password" 
    id="pwd" required>
    <label for="confirmPassword"><b>Confirm Password</b></label>
    <input type="password" name="confirmpassword" placeholder="Please 
    confirm password" id="cofirmpsw" required>
    <input type="submit" value="Register"></form>

2 个答案:

答案 0 :(得分:1)

我认为您缺少什么,请检查$_POST是否为空,

register.php

<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
session_start();
include 'config.php'; // this is database connection file
if (!empty($_POST)) {
    echo "<pre>";
    print_r($_POST);die;
}
?>

config.php

<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$con = mysqli_connect("localhost", "root", "", "music_pro");
if (mysqli_connect_errno()) {
    echo "Failed to connect : " . mysqli_connect_error();
}
?>

yourxyz.html或yourxyz.php

<form method="post" name="register" action="register.php">
    <label for="name"><b>Name</b></label>
    <input type="text" name="name" placeholder="Enter your name" required>
    <label for="email"><b>Email</b></label>
    <input type="text" name="email" placeholder="Enter your email id" required>
    <label for="uname"><b>User ID</b></label>
    <input type="text" name="uname" placeholder="Enter your user id" required>
    <label for="password"><b>Password</b></label>
    <input type="password" name="password" placeholder="Enter your 
     password" id="pwd" required>
    <label for="confirmPassword"><b>Confirm Password</b></label>
    <input type="password" name="confirmpassword" placeholder="Please 
    confirm password" id="cofirmpsw" required>
    <input type="submit" value="Register">
</form>

这应该有效。然后检查输入元素是否具有以name为名称的name属性。

答案 1 :(得分:0)

如果您的表单和php代码在一页中,则如下更改表单的第一行:

<form method="post" name="register" action="<?php echo $_SERVER['SCRIPT_NAME''];?>">
相关问题