我使用表单注册用户并将表单值发送到php。但我一直在上面而不是真正的价值观。
我的Html:
<div class="third">
<h2 class="section-title">Register now</h2>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3">
<div class="box-style cont-pad">
<form method = "post" action = "register.php" >
<h2>Please Sign Up <small>It is free and always will be.</small></h2>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group form-actions">
<input type="text" name="first_name" id="first_name" class="form-control input-lg" placeholder="First Name" tabindex="1">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="last_name" id="last_name" class="form-control input-lg" placeholder="Last Name" tabindex="2">
</div>
</div>
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address" tabindex="3">
</div>
<div class="form-group">
<input type="age" name="Age" id="Age" class="form-control input-lg" placeholder="Age" tabindex="4" >
</div>
<div class="col-xs-2 col-sm-2 col-md-2"> <span class="radio">
<input type="radio" name="gender" id="male" class="o-hidden" value="1" tabindex="5" align="middle">Male
</span> </div>
<div class="col-xs-10 col-sm-10 col-md-10"> <span class="radio">
<input type="radio" name="gender" id="female" class="o-hidden" value="0" tabindex="6" align="middle">Female
</span> </div>
<div class="col-xs-2 col-sm-2 col-md-2"> <span class="radio">
<input type="radio" name="status" id="female" class="o-hidden" value="1" tabindex="7" >Enduser
</span> </div>
<div class="col-xs-3 col-sm-3 col-md-3"> <span class="radio">
<input type="radio" name="status" id="female" class="o-hidden" value="0" tabindex="8" >Company
</span> </div>
<div class="col-xs-2 col-sm-2 col-md-2"> <span class="radio">
<input type="radio" name="status" id="female" class="o-hidden" value="10" tabindex="9" >Both
</span> </div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="10">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password_confirmation" id="password_confirmation" class="form-control input-lg" placeholder="Confirm Password" tabindex="11">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-1 col-sm-1 col-md-1"> <span class="button-checkbox">
<input type="checkbox" name="t_and_c" id="t_and_c" class="o-hidden" value="1" tabindex="12" >
</span> </div>
<div class="col-xs-11 col-sm-11 col-md-11">
<p>By clicking <strong class="label label-primary">Register</strong>, you agree to the <a href="#" data-toggle="modal" data-target="#t_and_c_m">Terms and Conditions</a> set out by this site, including our Cookie Use.</p>
</div>
</div>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-6 col-md-6">
<input type="submit" value="Register" class="btn btn-primary btn-block btn-lg" tabindex="13">
</div>
<div class="col-xs-6 col-md-6"><a href="#" class="btn btn-success btn-block btn-lg" tabindex="14" >Sign In</a></div>
</div>
</form>
</div>
</div>
这是我的php代码:
<?php
print_r($_POST);
require_once 'include/Functions-reg-log.php';
$db = new DB_Functions();
// json response array
try{
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['surname']) && isset($_POST['email']) && isset($_POST['age']) && isset($_POST['sex']) && isset($_POST['status']) && isset($_POST['password']) && isset($_POST['t_and_c']))
{
// receiving the post params
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$age = $_POST['age'];
$sex = $_POST['sex'];
$status = $_POST['status'];
$password = $_POST['password'];
// check if user is already existed with the same email
if ($db->isUserExisted($email)) {
// user already existed
$response["error"] = TRUE;
$response["error_msg"] = "User already existed with " . $email;
echo json_encode($response);
}
else
{
// create a new user
$user = $db->storeUser($name,$surname, $email, $age, $sex, $status, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["surname"] = $user["surname"];
$response["user"]["email"] = $user["email"];
$response["user"]["age"] = $user["age"];
$response["user"]["sex"] = $user["sex"];
$response["user"]["status"] = $user["status"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters is missing!";
$response["name"] = $_Post["name"];
echo json_encode($response);
}
}catch (Exception $e) {
System.out.println("Exception : " + $e.getMessage());
}
?>
毕竟这是我收到的错误:
数组([first_name] =&gt; neshat [last_name] =&gt; korivand [email] =&gt; neshatkorivand@yahoo.com [年龄] =&gt; 25 [性别] =&gt; 1 [状态] =&gt; 1 [密码] =&gt; 123456 [password_confirmation] =&gt; 123456 [t_and_c] =&gt; 1){“error”:true,“error_msg”:“缺少必需参数!”,“名称”:null}
答案 0 :(得分:2)
根据您的输入回复:
Array ( [first_name] => neshat [last_name] => korivand [email] => neshatkorivand@yahoo.com [Age] => 25 [gender] => 1 [status] => 1 [password] => 123456 [password_confirmation] => 123456 [t_and_c] => 1 ) {"error":true,"error_msg":"Required parameters is missing!","name":null}
这些索引:
$name = $_POST['name'];
$surname = $_POST['surname'];
$age = $_POST['age'];
$sex = $_POST['sex'];
应该是:
$name = $_POST['first_name'];
$surname = $_POST['last_name'];
$age = $_POST['Age'];
$sex = $_POST['gender'];
同样遵循 IF CONDITION 中的相同索引名称。
正如@arkascha在他的评论中提到的那样:
$response["name"] = $_Post["name"];
应该是:
$response["name"] = $_POST["first_name"];