Javascript 表单提交保存数据

Javascript 表单提交保存数据,javascript,php,html,forms,Javascript,Php,Html,Forms,我制作了一个表单,其中只需要5个字段,其他字段是可选的 我的表单操作为空,但onSubmit为: function validateInput() { var ifEmpty = (document.forms['orderform'].naam.value == "" || document.forms['orderform'].woonplaats.value == "" ||

我制作了一个表单,其中只需要5个字段,其他字段是可选的

我的表单操作为空,但onSubmit为:

function validateInput()
    {
        var ifEmpty =   (document.forms['orderform'].naam.value == "" ||
                        document.forms['orderform'].woonplaats.value == "" ||
                        document.forms['orderform'].straatnaam.value == "" ||
                        document.forms['orderform'].huisnummer.value == "" ||
                        document.forms['orderform'].telefoonnummer.value == "");

        if(ifEmpty)
        {
            document.orderform.action ="index.php?page=pizzaorder";
        }else{
            document.orderform.action ="index.php?page=pizza_bestelling";
        }
    }
因此,每当这5个字段中的1个为空时,操作将是
index.php?page=pizzaorder
如果一切正常,操作将是
index.php?page=pizza\u bestalling
这一切都正常工作。但是,如果其中一个字段为空,它将提交给自己,即page=pizzaorder

(我不能使用
$\u SERVER['PHP\u SELF']
的原因是我使用的是一个包含菜单。这意味着我总是在同一个页面上,它只会根据菜单选项包含不同的文件。因此,如果我使用
$\u SERVER['PHP\u SELF']
我会被发送到默认的index.PHP,这将是我的主页,这不是我想要的)

无论如何,如果其中一个字段没有填写,它将发送到同一页面,并在空字段旁边显示错误消息,这也可以正常工作

if ($_SERVER["REQUEST_METHOD"] == "POST")
 {
    if (empty($_POST["naam"]))
    {$naamErr = "Vul uw naam in";}
    else
    {$naam = test_name($_POST["naam"]);}

    if (empty($_POST["woonplaats"]))
    {$woonplaatsErr = "Vul uw Woonplaats in";}
    else
    {$woonplaats = test_name($_POST["woonplaats"]);}

    if (empty($_POST["straatnaam"]))
    {$straatErr = "Vul uw straat in";}
    else
    {$straatnaam = test_name($_POST["straatnaam"]);}

    if (empty($_POST["huisnummer"]))
    {$huisnummerErr = "Vul uw Huisnummer in";}
    else
    {$huisnummer = test_name($_POST["huisnummer"]);}

    if (empty($_POST["telefoonnummer"]))
    {$telefoonErr = "Vul uw Telefoonnummer in";}
    else
    {$telefoonnummer = test_name($_POST["telefoonnummer"]);}
}
但是,如果发生这种情况,所有字段都是空的,这不是我想要的。如果用户正确地填写了他的所有信息,但忘记了他的街道,比如说,他只看到错误消息street是必需的,但是他的所有其他数据也是空的。唯一的区别是没有错误消息

所以我想要的是,如果他被发送回这个页面,并带有错误信息,其他信息也不应该是空的

我希望我的问题有点清楚,否则请随时要求澄清


提前谢谢

因此,基本上,每当用户看到要重新输入的表单时,您都需要它显示他以前输入的值。这些被称为粘性表单。 因此,在表单的每个输入标记中,对照值检查$\u POST super global是否为空,并在其value属性中回显它

<input type="text" name="telefoonnummer" value="<?php echo (isset($_POST['telefoonnummer']) && !empty($_POST['telefoonnummer']))?$_POST['telefoonnummer']:'';?>"/>

完全符合我的要求!谢谢我还有最后一个问题,但其实没有什么关系。有没有一种方法可以在用户返回后自动关注我的表单?所以他/她不必一直向下滚动?再次感谢!是的,在要首先填充的输入标记中,添加autofocus=“autofocus”
<input type="text" name="telefoonnummer" value="
<?php 
    echo (isset($_POST['telefoonnummer']) && !empty($_POST['telefoonnummer']))?$_POST['telefoonnummer']:'';
?>
"/>