Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在另一个php脚本中验证表单?_Php_Forms - Fatal编程技术网

在另一个php脚本中验证表单?

在另一个php脚本中验证表单?,php,forms,Php,Forms,我想知道,如果表单中的操作设置为另一个脚本,是否可以管理错误 ajoudesign.php <FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'> <label for="client">Client</label> <input type='text' id='client' name='client'> <span class="error">

我想知道,如果表单中的操作设置为另一个脚本,是否可以管理错误

ajoudesign.php

<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>
那么,如果输入为空,是否可以在范围中显示我的错误?还是说没有办法用其他脚本作为动作

谢谢

session_start();
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if(empty($_POST['client'])) {
            $clientErr = "ERROR";
            $_SESSION['error] = $clentErr;
            header('location:ajoutDevis.php');

        } else $client = $_POST['client'];

In your ajoutDevis.php file


session_start();
$clientErr = ($_SESSION['error']!='')?$_SESSION['error']:'';
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
<label for="client">Client</label>
<input type='text' id='client' name='client'>
<span class="error">* <?php echo $clientErr;?></span>
</FORM>
如果($\服务器[“请求\方法”]=“发布”){ if(空($_POST['client'])){ $clienter=“错误”; $\会话['error]=$clenter; 标题('location:ajoutdesign.php'); }else$client=$_POST['client']; 在您的ajoudesign.php文件中 会话_start(); $clientErr=($\u会话['error']!=“”)?$\u会话['error']:“”; 客户 *
您需要执行以下操作:

在页面底部的ajoutdesignscript.php页面上添加表单代码

<?php if(!empty($clientErr)) { ?>
    <FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
    <label for="client">Client</label>
    <input type='text' id='client' name='client'>
    <span class="error">* <?php echo $clientErr;?></span>
    </FORM>
<?php } ?>

客户
* 

这将检查错误变量中是否有任何错误。它将在同一页上显示有错误的表单。

是的,可以使用
$\u会话
变量在会话期间存储值,并使用
header()
函数将用户从第二个脚本重定向回第一个脚本。 假设两个文件都在根目录中,ajoudevident.php

<?php
session_start();
?>
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
    <label for="client">Client</label>
    <input type='text' id='client' name='client'>
    <?php if (isset($_SESSION['clientErr'])) : ?>
        <span class="error">* <?php echo $_SESSION['clientErr'] ?></span>
    <?php endif; ?>
</FORM>
<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(empty($_POST['client'])) {
        $_SESSION['clientErr'] = "ERROR";
        header('Location: ajoutDevis.php');
    } else {
        $client = $_POST['client'];
    }
}
?>

客户
* 
ajoutdesignscript.php

<?php
session_start();
?>
<FORM name='devis' method='POST' action='ajoutDevisScript.php' id='form'>
    <label for="client">Client</label>
    <input type='text' id='client' name='client'>
    <?php if (isset($_SESSION['clientErr'])) : ?>
        <span class="error">* <?php echo $_SESSION['clientErr'] ?></span>
    <?php endif; ?>
</FORM>
<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if(empty($_POST['client'])) {
        $_SESSION['clientErr'] = "ERROR";
        header('Location: ajoutDevis.php');
    } else {
        $client = $_POST['client'];
    }
}
?>


我建议您仅在需要时打印
span
。我还建议您检查括号以避免PHP错误。

如果要使用会话变量显示错误,请记住在显示后取消链接/删除会话变量,否则错误将继续显示。请尝试:取消链接($\u session['error')); ;)