$ _POST未获取

时间:2017-07-25 06:11:45

标签: php jquery html css sql-server

$_POST没有得到任何值,我已经尝试了很多已经提到的堆栈溢出程序,但它们对我不起作用。我试过打印$_POST它是空的。我需要一些建议..请帮助

以前工作时它是在mysql数据库但我试图将数据库更改为sqlserver,现在它无法工作,但我不明白我没有对这个特定的代码做任何更改,我也看到了它也没有受到其他文件的影响。

在空状态下没有错误我自己写了检查它是否为空而且它总是显示为空我是否提交数据

我附上了一些与此相关的代码。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<?php
ob_start();
session_start();
require_once 'config.php'; 
?>
<?php 
	if(empty($_POST)){
		echo "hello";
		try {
			$user_obj = new Cl_User();
			$data = $user_obj->registration( $_POST );
			if($data){
				$_SESSION['success'] = USER_REGISTRATION_SUCCESS;
				header('Location: index.php');exit;
			}
		} catch (Exception $e) {
			$_SESSION['error'] = $e->getMessage();
		}
	}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="php quiz script, php quiz code, php quiz application, quiz php code, php quiz system, online quiz using php, quiz using php, how to make quiz in php, quiz system in php, php programming quiz, online quiz using php and sqlsrv, create online quiz using php and sqlsrv, create quiz using php sqlsrv, php quiz script free">
    <meta name="keywords" content="php quiz script, php quiz code, php quiz application, quiz php code, php quiz system, online quiz using php, quiz using php, how to make quiz in php, quiz system in php, php programming quiz, online quiz using php and sqlsrv, create online quiz using php and sqlsrv, create quiz using php sqlsrv, php quiz script free">
    <title>PHP Quiz Script</title>
    <link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <link href="css/login.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </head>
  <body>
	<div class="container">
		<div class="login-form">
			<?php require_once 'templates/message.php';?>
			
			<h1 class="text-center">PHP Quiz Application</h1>
			<div class="form-header">
				<i class="fa fa-user"></i>
			</div>
			<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="form-register" role="form" id="register-form">
				<div>
					<input name="name" id="name" type="text" class="form-control" placeholder="Name"> 
					<span class="help-block"></span>
				</div>
				<div>
					<input name="email" id="email" type="email" class="form-control" placeholder="Email address" > 
					<span class="help-block"></span>
				</div>
				<div>
					<input name="password" id="password" type="password" class="form-control" placeholder="Password"> 
					<span class="help-block"></span>
				</div>
				<div>
					<input name="confirm_password" id="confirm_password" type="password" class="form-control" placeholder="Confirm Password"> 
					<span class="help-block"></span>
				</div>
				<button class="btn btn-block bt-login" type="submit" id="submit" name="submit">Sign Up</button>
			</form>
			<div class="form-footer">
				<div class="row">
					<div class="col-xs-6 col-sm-6 col-md-6">
						<i class="fa fa-lock"></i>
						<a href="forget_password.php"> Forgot password? </a>
					
					</div>
					
					<div class="col-xs-6 col-sm-6 col-md-6">
						<i class="fa fa-check"></i>
						<a href="index.php"> Sign In </a>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- /container -->

	
    <script src="js/jquery.validate.min.js"></script>
    <script src="js/register.js"></script>
  </body>
</html>
<?php unset($_SESSION['success'] ); unset($_SESSION['error']);  ?>    

<?php

	$server="NIKUNJ";
			$ci = array("Database" => "My database","UID"=>"sa", "PWD"=>"sql@123","Characterset"=>"UTF-8") or die( "check db connect1" );
			$conn = sqlsrv_connect($server,$ci) or die ( "check db connect2" ) ;

function mssql_escape($str)
{
   if(get_magic_quotes_gpc())
   {
    $str= stripslashes($str);
   }
   return str_replace("'", "''", $str);
}

function mssql_insert_id() { 
    $id = 0; 
    $res = sqlsrv_query("SELECT @@identity AS id"); 
    if ($row = sqlsrv_fetch_array($res, MSSQL_ASSOC)) { 
        $id = $row["id"]; 
    } 
    return $id; 
} 

class Cl_User
{
	/**
	 * @var will going contain database connection
	 */
	protected $_con;
	
	/**
	 * it will initalize DBclass
	 */
	public function __construct()
	{
		$db = new Cl_DBclass();
		$this->_con = $db->con;
	}
	
	/**
	 * this will handles user registration process
	 * @param array $data
	 * @return boolean true or false based success 
	 */
	public function registration( array $data )
	{
		echo "hello";
		if( !empty( $data ) ){
			
			// Trim all the incoming data:
			$trimmed_data = array_map('trim', $data);
			
			
			
			// escape variables for security
			$name = mssql_escape( $trimmed_data['name'] );
			$password = mssql_escape( $trimmed_data['password'] );
			$cpassword = mssql_escape( $trimmed_data['confirm_password'] );
			
			
			// Check for an email address:
			if (filter_var( $trimmed_data['email'], FILTER_VALIDATE_EMAIL)) {
				$email = mssql_escape( $trimmed_data['email']);
			} else {
				throw new Exception( "Please enter a valid email address!" );
			}
			
			
			if((!$name) || (!$email) || (!$password) || (!$cpassword) ) {
				throw new Exception( FIELDS_MISSING );
			}
			if ($password !== $cpassword) {
				throw new Exception( PASSWORD_NOT_MATCH );
			}
			$password = md5( $password );
			$query = "INSERT INTO users (id, name, email, password, created) VALUES (NULL, '$name', '$email', '$password', CURRENT_TIMESTAMP)";
			if(sqlsrv_query($this->_con, $query)){
				sqlsrv_close($this->_con);
				return true;
			};
		} else{
			throw new Exception( USER_REGISTRATION_FAIL );
		}
	}
	/**
	 * This method will handle user login process
	 * @param array $data
	 * @return boolean true or false based on success or failure
	 */
	public function login( array $data )
	{
		$_SESSION['logged_in'] = false;
		if( !empty( $data ) ){
			// Trim all the incoming data:
			$trimmed_data = array_map('trim', $data);
			
			// escape variables for security
			$email = mssql_escape( $this->_con,  $trimmed_data['email'] );
			$password = mssql_escape( $this->_con,  $trimmed_data['password'] );
			if((!$email) || (!$password) ) {
				throw new Exception( LOGIN_FIELDS_MISSING );
			}
			$password = md5( $password );
			$query = "SELECT id, name, email, created FROM users where email = '$email' and password = '$password' ";
			$result = sqlsrv_query($this->_con, $query);
			$data = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC);
			$count = SQLSRV_num_rows($result);
			echo $count;
			sqlsrv_close($this->_con);
			if( $count == 1){
				$_SESSION = $data;
				$_SESSION['logged_in'] = true;
				return true;
			}else{
				throw new Exception( LOGIN_FAIL );
			}
		} else{
			throw new Exception( LOGIN_FIELDS_MISSING );
		}
	}
	
	/**
	 * This will shows account information and handles password change
	 * @param array $data
	 * @throws Exception
	 * @return boolean
	 */
	
	public function account( array $data )
	{
		if( !empty( $data ) ){
			// Trim all the incoming data:
			$trimmed_data = array_map('trim', $data);
			
			// escape variables for security
			$password = mssql_escape( $this->_con, $trimmed_data['password'] );
			$cpassword = $trimmed_data['confirm_password'];
			$user_id = $_SESSION['id'];
			if((!$password) || (!$cpassword) ) {
				throw new Exception( FIELDS_MISSING );
			}
			if ($password !== $cpassword) {
				throw new Exception( PASSWORD_NOT_MATCH );
			}
			$password = md5( $password );
			$query = "UPDATE users SET password = '$password' WHERE id = '$user_id'";
			if(sqlsrv_query($this->_con, $query)){
				sqlsrv_close($this->_con);
				return true;
			}
		} else{
			throw new Exception( FIELDS_MISSING );
		}
	}
	
	/**
	 * This handle sign out process
	 */
	public function logout()
	{
		session_unset();
		session_destroy();
		session_start();
		$_SESSION['success'] = LOGOUT_SUCCESS;
		header('Location: index.php');
	}
	
	/**
	 * This reset the current password and send new password to mail
	 * @param array $data
	 * @throws Exception
	 * @return boolean
	 */
	public function forgetPassword( array $data )
	{
		if( !empty( $data ) ){
			
			// escape variables for security
			$email = mssql_escape( $this->_con, trim( $data['email'] ) );
			
			if((!$email) ) {
				throw new Exception( FIELDS_MISSING );
			}
			$password = $this->randomPassword();
			$password1 = md5( $password );
			$query = "UPDATE users SET password = '$password1' WHERE email = '$email'";
			if(sqlsrv_query($this->_con, $query)){
				sqlsrv_close($this->_con);
				$to = $email;
				$subject = "New Password Request";
				$txt = "Your New Password ".$password;
				$headers = "From: rahul.ranjan72@hotmail.com" . "\r\n" .
						"CC:rahul.ranjan72@hotmail.com";
					
				mail($to,$subject,$txt,$headers);
				return true;
			}
		} else{
			throw new Exception( FIELDS_MISSING );
		}
	}
	
	/**
	 * This will generate random password
	 * @return string
	 */
	
	private function randomPassword()
	{
		$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
		$pass = array(); //remember to declare $pass as an array
		$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
		for ($i = 0; $i < 8; $i++) {
			$n = rand(0, $alphaLength);
			$pass[] = $alphabet[$n];
		}
		return implode($pass); //turn the array into a string
	}
	
	public function pr($data = '' )
	{
		echo "<pre>"; print_r($data); echo "</pre>";
	}
	
	public function getCategory()
	{
		$query = "SELECT * FROM categories";
		$results = sqlsrv_query($conn, $query)  or die(SQLSRV_errors());
		$categories = array();
		while ( $result = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC) ) {
			echo $result['id'];
			$categories[$result['id']] = $result['category_name'];
		}
		return $categories;
	}
	
	public function getQuestions(array $data)
	{
		if( !empty( $data ) ){
				
			// escape variables for security
			$category_id = mssql_escape( $this->_con, trim( $data['category'] ) );
			if((!$category_id) ) {
				throw new Exception( FIELDS_MISSING );
			}
			$user_id = $_SESSION['id'];
			$query = "INSERT INTO scores ( user_id,right_answer,category_id)VALUES ( '$user_id',0,'$category_id')";
			sqlsrv_query( $this->_con, $query);
			$_SESSION['score_id'] = mssql_insert_id();
			$results = array();
			$number_question = $_POST['num_questions'];
			$total_question = $_POST['total_num_questions'];
			$row = sqlsrv_query( $this->_con, "select * from questions where category_id=$category_id ORDER BY RAND()");
			$check=SQLSRV_num_rows($row);
			if($check<$total_question)
				$rowcount=$check;
			else
			$rowcount = $total_question;
			$remainder = $rowcount/$number_question;
			$results['number_question'] = $number_question;
			$results['remainder'] = $remainder;
			$results['rowcount'] = $rowcount;
			while ( $result = SQLSRV_FETCH_ASSOC($row) ) {
				$results['questions'][] = $result;
			}
			sqlsrv_close($this->_con);
			return $results;
		} else{
			throw new Exception( FIELDS_MISSING );
		}
	}
	
	public function getAnswers(array $data)
	{
		if( !empty( $data ) ){
			$right_answer=0;
			$wrong_answer=0;
			$unanswered=0;
			$total_question = $_POST['total_num_questions'];
			$keys=array_keys($data);
			$order=join(",",$keys);
			$query = "select id,answer from questions where id IN($order) ORDER BY FIELD(id,$order)";
			$response=sqlsrv_query( $this->_con, $query)   or die(SQLSRV_errors());
			
			$user_id = $_SESSION['id'];
			$score_id = $_SESSION['score_id'];
			while($result=sqlsrv_fetch_array($response)){
				if($result['answer']==$_POST[$result['id']]){
					$right_answer++;
				}else if($data[$result['id']]=='smart_quiz'){
					$unanswered++;
				}
				else{
					$wrong_answer++;
				}
			}
			$results = array();
			$results['right_answer'] = $right_answer;
			$results['wrong_answer'] = $wrong_answer;
			$results['unanswered'] = $unanswered;
			$update_query = "update scores set right_answer='$right_answer', wrong_answer = '$wrong_answer', unanswered = '$unanswered' where user_id='$user_id' and id ='$score_id' ";
			sqlsrv_query( $this->_con, $update_query)   or die(SQLSRV_errors());
			sqlsrv_close($this->_con);
			return $results;
		}	
	}
}

<?php
/**
@author vetripandi
@copyright http:www.vetbossel.in
 */

require_once 'messages.php';

//site specific configuration declartion

define( 'DB_HOST', 'NIKUNJ' );
define( 'DB_USERNAME', 'sa');
define( 'DB_PASSWORD', 'sql@123');
define( 'DB_NAME', 'user_login');

function __autoload($class)
{
	$parts = explode('_', $class);
	$path = implode(DIRECTORY_SEPARATOR,$parts);
	require_once $path . '.php';
}

its the image of the data i am sending but $_POST is not getting any values and nothing happens after signup button is pressed

5 个答案:

答案 0 :(得分:1)

只有''数组为空时才会运行代码。

将您的代码更改为以下内容。

null

除此之外,我认为没有问题。

答案 1 :(得分:1)

最好将提交按钮作为执行服务器端编码执行的关注中心。

因此,请检查是否已使用isset发送$ _POST数据:

if (isset($_POST['submit']))
{
    // the data has successfully been sent
}

答案 2 :(得分:0)

这可能不是您的问题,但通常提交按钮是

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

而不是

<button type="submit">Submit</button>

来自:W3schools.com

答案 3 :(得分:0)

你确定没问题吗?

if(empty($_POST))

如果$ _POST为空,你总是执行代码

if(!empty($_POST))

在$ _POST NOT 为空时执行

答案 4 :(得分:0)

我弄错了。我不知道如何,但表格的价值不仅转移到这个PHP文件,而且还转移到另一个php文件名check-email.php,这是我的项目的一部分,在register.php中没有提到。

我通过在堆栈溢出中查看与此类问题相关的帖子来了解问题,他说要检查PHP_error_log和Apache错误日志。那里的错误清楚地说明了。通过对check-email.php进行一些更改,它现在正常工作。无论如何,谢谢大家的帮助