Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.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_Html_Submit - Fatal编程技术网

表单提交前php显示错误

表单提交前php显示错误,php,html,submit,Php,Html,Submit,我有一个简单的php脚本: <!doctype html> <html> <head> <meta charset="utf-8" /> <title>MY WEB</title> </head> <body> <form action="" method="get"> <input type="text" name="textval" val

我有一个简单的php脚本:

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>MY WEB</title>
</head>
<body>
    <form action="" method="get">
        <input type="text" name="textval" value="<?php if( isset($_GET['textval']) ) echo htmlspecialchars($_GET['textval']) ?>" />
        <input type="submit" value="Check" />
    </form>
</body>
</html>
<?php
if ( empty($_GET['textval']) ) {
    echo "Please Fill The TextBox" . PHP_EOL;  // displays this
}
else{
    echo "OKAY" . PHP_EOL;
}
?>

我的网络
试试这个

<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>MY WEB</title>
</head>
<body>
    <form action="" method="get">
        <input type="text" name="textval" value="<?php if( isset($_GET['textval']) ) echo htmlspecialchars($_GET['textval']) ?>" />
        <input type="submit" value="Check" />
    </form>
</body>
</html>
<?php
if(isset($_GET['submitted'])){
  if ( empty($_GET['textval']) ) {
    echo "Please Fill The TextBox" . PHP_EOL;
  }
  else{
    echo "OKAY" . PHP_EOL;
  }
}
?>

我的网络

您的
$\u GET['textval']
最初为空,因此
为空($\u GET['textval'])
条件为
true


这就是为什么在第一页加载时出现错误。

当您编辑代码时,如果
条件返回
true
,则显然会将该语句显示为
,因此在这种情况下,请为
提交
按钮指定一个
名称
,然后使用like

<input type="submit" value="Check" name="submitted" />

<?php
if(isset($_GET['submitted'])) {
   if (empty($_GET['textval'])) {
       echo "Please Fill The TextBox" . PHP_EOL;  // displays this
   }
   else{
       echo "OKAY" . PHP_EOL;
   }
}
?>

注意:最好使用
POST
而不是
GET
,因为这样做不正确 似乎是
$\u GET
合适的东西,通常
$\u GET
用于搜索 页数

根据你的密码

empty($_GET['textval'])
在提交表格之前,将始终提供TRUE。因此,首先检查您的表单是否已提交

第一次更改提交给

<input type="submit" name="submit" value="Check" />

或者为您的代码添加一个名为“submitted”的隐藏输入,并进行检查。

您还可以使用JQuery验证字段是否为空。这将不会重新加载页面

$("form").submit(function (e){
    if($("input[type='text']").val().length == 0)
    {
       alert('fill form');
       return false;
    }
});

抛出此错误什么错误?但如何停止此操作?首先使用条件if($_GET){if(empty($_GET['textval']){}像这样,您复制粘贴了我的代码,但忘记为提交按钮指定一个
名称:)是的,我尝试了这样做,但我必须在html页面上回显此错误,而不是警告它。但是jquery代码不应允许表单在使用$()时提交。文本函数应改为警报:)
$("form").submit(function (e){
    if($("input[type='text']").val().length == 0)
    {
       alert('fill form');
       return false;
    }
});