使用php

时间:2017-01-25 10:28:41

标签: php function validation model-view-controller

我已创建了valid()和insert()函数,但我不知道如何检查表单是否有效。我想在验证检查后调用insert()函数。 我删除了我的基本html表单代码 的 view.php

<?php
include('../controller/control.php');
if(isset($_REQUEST['submit']))
{
    $u = $_REQUEST['fname'];
    $p = $_REQUEST['lname'];
    $i = $_FILES['pic']['name'];
    $g = $_REQUEST['gender'];
    $c = $_REQUEST['country'];
    $s = $_REQUEST['state'];
    $ci = $_REQUEST['city'];
    $z = $_REQUEST['zipcode'];
    $cno = $_REQUEST['cno'];    
    $h = $_REQUEST['hobby'];
    $chk="";  
    if($h)
    {   
        foreach($h as $chk1)  
        {  
            $chk .= $chk1.",";  
        }
    }       
    $a = $_REQUEST['address'];
    $pdf =  $_FILES['pdf']['name']; 
    move_uploaded_file($_FILES['pic']['tmp_name'],"upload/".$_FILES['pic']['name']);
    move_uploaded_file($_FILES['pdf']['tmp_name'],"upload/".$_FILES['pdf']['name']);    
    $obj = new control();
    $obj->validate($u,$p,$i,$c,$s,$ci,$z,$cno,$a,$pdf); 
    $obj->insert($u,$p,$i,$g,$c,$s,$ci,$z,$cno,$chk,$a,$pdf);
    header("location:form.php");
}
?>

Controll.php

<?php
include('../model/model.php');
class control
{
    public function validate($u,$p,$i,$c,$s,$ci,$z,$cno,$a,$pdf)
    {
        $obj = new model();
        $obj->validate($u,$p,$i,$c,$s,$ci,$z,$cno,$a,$pdf);
    }
    public function insert($u,$p,$i,$g,$c,$s,$ci,$z,$cno,$chk,$a,$pdf)
    {
        $obj = new model();
        $obj->insert($u,$p,$i,$g,$c,$s,$ci,$z,$cno,$chk,$a,$pdf);
    }   
}

?>

model.php

<?php

class model
{   

    public function validate($u,$p,$i,$c,$s,$ci,$z,$cno,$a,$pdf)
    {
        if(empty($u))
        {
            echo "<script>alert('Please enter Username')</script>";
        }
        else if(empty($p))
        {
            echo "<script>alert('Please enter password')</script>";
        }

    }
    public function model()
    {
        $mysqli = new mysqli("localhost", "root", "", "php_mvc");

    }
    public function insert($u,$p,$i,$g,$c,$s,$ci,$z,$cno,$chk,$a,$pdf)
    {
        $mysqli = new mysqli("localhost", "root", "", "php_mvc");
        $mysqli->query("INSERT INTO `php_mvc`.`form` (`fname`, `lname`, `pic`, `gender`, `country`, `state`, `city`, `zipcode`, `cno`, `hobby`, `address`, `pdf`) VALUES ('$u', '$p', '$i', '$g', '$c', '$s', '$ci', '$z', '$cno', '$chk', '$a', '$pdf');");

        if($mysqli)
        {
            echo "<script>alert('Inserted Successfully')</script>";
        }
            header("location:form.php");

    }

}

?>

1 个答案:

答案 0 :(得分:1)

<强> View.php

    RelativeLayout toastLayout = new RelativeLayout(context);
    toastLayout.setBackground(getBLLShape());
    toastLayout.addView(getImageView());

<强> Controll.php

<?php
include('../controller/control.php');
if(isset($_REQUEST['submit']))
{
    $u = $_REQUEST['fname'];
    $p = $_REQUEST['lname'];
    $i = $_FILES['pic']['name'];
    $g = $_REQUEST['gender'];
    $c = $_REQUEST['country'];
    $s = $_REQUEST['state'];
    $ci = $_REQUEST['city'];
    $z = $_REQUEST['zipcode'];
    $cno = $_REQUEST['cno'];    
    $h = $_REQUEST['hobby'];
    $chk="";  
    if($h)
    {   
        foreach($h as $chk1)  
        {  
            $chk .= $chk1.",";  
        }
    }       
    $a = $_REQUEST['address'];
    $pdf =  $_FILES['pdf']['name']; 
    move_uploaded_file($_FILES['pic']['tmp_name'],"upload/".$_FILES['pic']['name']);
    move_uploaded_file($_FILES['pdf']['tmp_name'],"upload/".$_FILES['pdf']['name']);    
    $obj = new control();

    if($obj->validate($u,$p,$i,$c,$s,$ci,$z,$cno,$a,$pdf)){ 
    $obj->insert($u,$p,$i,$g,$c,$s,$ci,$z,$cno,$chk,$a,$pdf);
    }
    header("location:form.php");
}
?>

<强> model.php

<?php
include('../model/model.php');
class control
{
    public function validate($u,$p,$i,$c,$s,$ci,$z,$cno,$a,$pdf)
    {
        $obj = new model();
        return $obj->validate($u,$p,$i,$c,$s,$ci,$z,$cno,$a,$pdf);
    }
    public function insert($u,$p,$i,$g,$c,$s,$ci,$z,$cno,$chk,$a,$pdf)
    {
        $obj = new model();
        $obj->insert($u,$p,$i,$g,$c,$s,$ci,$z,$cno,$chk,$a,$pdf);
    }   
}

?>