如何从另一个php文件中包含php验证函数

时间:2015-01-28 11:36:58

标签: javascript php html css json

你好我是网页设计新手,我创建一个表单来给出状态代码和状态名称。在PHP中验证这个表单并在另一个文件中完成验证。我不知道如何包含validation.please帮助我验证并在数据库中存储之后。我将附加所有文件,请帮助我的朋友。



function stateAjaxsubmit()
{		
    var statename = document.forms["addstate"]["statename"].value;
    var statecode = document.forms["addstate"]["statecode"].value;
	 
    var valueJson = {
        "State_Code": statecode,
        "State_Name": statename
    };
    
    console.log("Input");
    console.log(valueJson);
	 
     $.ajax(
 	{ 
        dataType: "json",
        cache: false,		    
        contentType: "application/json; charset=utf-8",
 	url : "tocheck.php",
 	type: "POST",
 	data :JSON.stringify(valueJson),
 	success:function(data, textStatus, jqXHR)
 	{
		console.log("Output");
                console.log(data);
 	},
	 error: function(jqXHR, textStatus, errorThrown)
 	{
 	}			 
    });
    

<?php
include('php/dbconnection.php');
include('validations.php');
include('Samplestates.php');
$data = json_decode(file_get_contents('php://input'));
header('Content-Type: application/json');
 $head = json_encode($data);
 echo $head;
$id = $data->State_Code;
$name = $data->State_Name;

function doSubmit(){       
    if (checkFormvalues()) 
        {
         $value = ('#addstate').submit();
    }
}
$sql="INSERT INTO m_state1(State_Code, State_Name, Created_By, Created_Date) 
VALUES ('$id', '$name', '1000',now())";
$res=mysqli_query($con,$sql) or die('Error: '. mysqli_error($con));

?>


Samplestates.php

<?php
function checkFormvalues($statecode, $statename)
{
		$msg = "";
		$isstatecode = false;
		$isstatename = false;
		$ismsg = false;    
                $data = array();
                
		//function to check null
		if(checkNullvalue($statecode))
		{
			//length must be 3
			if(checkCodeLen($statecode))
			{
				//check alphanumeric
				if(checkAlphabets($statecode))
				{
					$isstatecode = true;
				}
				else
				{
					$isstatecode = false;
					$msg .= "Code must be Alphabet";
                                        array_push($data, array("Code" => $msg));
				}
			}
			else
			{
				$isstatecode = false;
				$msg .= "Length must be three charcter";
                                array_push($data, array("Code" => $msg));
			}
		}
		else
		{
			$isstatecode = false;
			$msg .= "Code must be filled out";
                        array_push($data, array("Code" => $msg));
		}
		
                $msg = "";
                
		//function to check null
		if(checkNullvalue($statename))
		{
				//check alphabet
				if(checkAlphabets($statename))
				{
					$isstatename = true;
				}
				else
				{
					$isstatename = false;
					$msg .= "Name must be Alphabet";
                                        array_push($data, array("Name" => $msg));
				}
		}
		else
		{
			$isstatename = false;
			$msg .= "Name must be filled out";
                        array_push($data, array("Name" => $msg));
		}
		
		if(empty($data))
		{
			$ismsg = true;
                        array_push($data, array("Result" => $ismsg));
		}
		else
		{
			$ismsg = false;                        
                        array_push($data, array("Result" => $ismsg));
		}
		
                $outData = array("Response" => $data);
		
                echo json_encode($outData);
                
		return json_encode($outData);
}
?>

validations.php

<?php
//function to check null values
function checkNullvalue($e)
{
	if (empty($e)) 
	{
        return false;	
	}
	else
	{
		return true;
	}
}

function checkCodeLen($value)
{
	if(strlen($value) > 3)
	{
		return false;
	}
	else
	{
		return true;
  	}			
}

function checkAlphabets($alphabet)
{ 
	if(ctype_alpha($alphabet)) 
	{
   		return true;
	}
	else
	{
    	return false;
    }
}

function toupper($value)
{
	$upper = strtoupper($value);
	return $upper;
}
?>
&#13;
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="css/Addcaste.css" rel="stylesheet" />
<script src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/Validation.js"></script>
<script type="text/javascript" src="js/State.js"></script>
</head>
<body>
    <p><center>State Entry</center></p>
    <form method="post" class="r" name="addstate" id="addstates" onSubmit="return doSubmit()" action="#">
    <div>
    <table align="center">
  
    	<td>State Code</td>
    	<td><input type="text" id="statecode" name="statecode" autocomplete="off" /></td>
    </tr>
    <tr>
    	<td>State Name</td>
    	<td><input type="text" name="statename" id="statename" autocomplete="off"/></td>
    </tr>
    <tr>
    	<td><input type="submit" name="submit" value="Add" id="submit" /></td>
    	<td><input type="reset" name="reset" value="Cancel" /></td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果我只是告诉你如何做到这一点,它可能不会帮助太多,你可能最好花一些时间熟悉HTML,PHP和Javascript,看起来你可能是缺少一些关键概念。

现在,我要说忽略javascript部分,只关注服务器上的表单提交和验证。

TL; DR

我建议从这样的视频开始:Build a PHP contact form,它应该涵盖表单提交和错误检查(验证)的一些基础知识。

然后开始研究如何将javascript和PHP结合起来:Submitting a form with AJAX

希望有所帮助!