我如何php验证我的表单数据,以便它必须包含内容?

时间:2014-09-03 09:31:24

标签: php html validation

所以我创建了一个html表单,它将信息提供给包含html表单的文档,我想要一个php检查以确保特定字段中包含信息,我的代码如下所示。

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors = array();
    if ( ! isset($_POST['dbname'])) {
        array_push($errors,'dbname');
    }
        if ( ! isset($_POST['dbpass'])) {
        array_push($errors, 'dbpass');
        }
            if ( ! isset($_POST['dbhost'])) {
        array_push($errors, 'dbhost');
        }
            if ( ! isset($_POST['prefix'])) {
        array_push($errors, 'prefix');
        }
            if (isset($_POST['dbname']) && trim($_POST['dbname']) != '' {
            echo "something";
            }
            else
            {
            echo "nothing";
            }

    if (count($errors) > 0) {
        $error_msg = implode('<br>', $errors);
        show_form($error_msg);
    }
    else {
        do_install();
        echo 'Install Done';
    }
}
else {
    // show the form
    show_form();
}

给我带来麻烦的是

if (isset($_POST['dbname']) && trim($_POST['dbname']) != ''

基本上我认为这一行基本上会说“如果设置字段dbname并且修剪它时它的值等于某些东西然后执行此操作,如果没有则执行此操作”但是当我在整个事物中添加此行时打破并停止在浏览器中显示任何内容。

如果你需要它,我的文件的其余部分就像这样

function show_form($msg = '') {
?>
<html>
    <body>
        <?php if (isset($msg) && trim($msg) != '') { echo $msg; } ?>
        <P> HI </P>
        <form action="hi.php" method="POST">
            <p>Your Database name and user: <input type="text" name="dbname" /><br />
            Your database password <input type="text" name="dbpass" /> <br/>
            Your DB_HOST (ip address) <input type ="text" name = "dbhost" /> <br/> 
            Your desired table prefix <input type ="text" name = "prefix" /> <br />
            </p>

            <p><input type="submit" value="Send it!"></p>
        </form>
<?php
        }
        ?>
<?php
function recurseRmdir($di) {
  $files = array_diff(scandir($di), array('.','..'));
  foreach ($files as $file) {
    (is_dir("$di/$file")) ? recurseRmdir("$di/$file") : unlink("$di/$file");
  }
  return rmdir($di);
}
?>
<?php
function do_install() { 
    chdir(__DIR__);
    $dir = getcwd(); /** stores the directory that the php file is in as a variable */
    file_put_contents($dir . '/wordpress.zip', file_get_contents('http://wordpress.org/latest.zip'));
    $zip = new ZipArchive;
    $zip->open('wordpress.zip');
    $zip->extractTo($dir);
    $zip->close();
    unlink($dir . '/wordpress/wp-content/plugins/hello.php');
    recurseRmdir($dir . '/wordpress/wp-content/plugins/akismet');
    recurseRmdir($dir . '/wordpress/wp-content/themes/twentyfourteen');
    recurseRmdir($dir . '/wordpress/wp-content/themes/twentythirteen');
    recurseRmdir($dir . '/wordpress/wp-content/themes/twentytwelve');
    file_put_contents ($dir . '/wpbfboilerplate.zip', file_get_contents ('/home/wpbfboilerplate.zip'));
    $zip = new ZipArchive;
    $zip->open('wpbfboilerplate.zip');
    $zip->extractTo($dir);
    $zip->close();
    unlink($dir . '/wordpress.zip');
    unlink($dir . '/wpbfboilerplate.zip');
    rename($dir . '/wordpress/wp-config-sample.php', $dir . '/wordpress/wp-config.php');
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('database_name_here',$dbname, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('username_here', $dbname, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('localhost', $dbhost, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace('password_here', $dbpass, $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    $contents = file_get_contents($dir . '/wordpress/wp-config.php');
    $new_contents = str_replace("\$table_prefix  = 'wp_';", "\$table_prefix  =" . "'" . $prefix . "'", $contents);
    file_put_contents('wordpress/wp-config.php', $new_contents);
    }
?>

2 个答案:

答案 0 :(得分:0)

应该是if(isset($ _ POST ['dbname'])&amp;&amp; trim($ _ POST ['dbname'])!=''){我只是忘了支架!的xD

答案 1 :(得分:0)

你可以试试这个

if(!$_POST['dbname']){
    $data['Error']='Please enter DB Name.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['dbpass']){
    $data['Error']='Please enter DB password.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['dbhost']){
    $data['Error']='Please enter DB Host.';
    show_form($data['Error']); // show the form
}elseif(!$_POST['prefix']){
    $data['Error']='Please enter DB Prefix.';
    show_form($data['Error']); // show the form
}else{
        do_install();
        echo 'Install Done';
}

做这样的事情,我希望上面会有所帮助。