dqdz6464 2015-01-28 11:36
浏览 42
已采纳

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

Hello im new to web designing,i create one form to give statecode and statename. validation for this form in php and the validation done in another file.i dont know how to include the validation.please help me to validate and after to store in database.here i will attach all the files please help me friends.

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;
}
?>
<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>

</div>
  • 写回答

1条回答 默认 最新

  • dongzhuifeng1843 2015-01-29 09:00
    关注

    It probably won't help too much if i just tell you how to do it all, you're probably best to spend some time getting familiar with HTML, PHP and Javascript, it seems like you might be missing a few key concepts.

    For now, i'd say ignore the javascript part and just focus on the form submission and validation on the server.

    TL;DR

    I suggest starting with a video like this: Build a PHP contact form, that should cover some of the basics of form submission and error checking (validation).

    Then start looking at how to combine javascript and PHP with something like this: Submitting a form with AJAX

    Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮