表单提交不起作用 - PHP

时间:2016-10-07 15:03:33

标签: php html validation

我正在进行表单验证,我正在对我的代码进行一些调试。我将在下面插入必要的代码片段:

表格代码:

echo '<h1> Seismic Recording </h1>
<div>
<form action="validate2.php" method="POST">';

echo "Latitude: <input type='text' name='latitude'>";
echo "Longitude: <input type='text' name='longitude'required>";
echo 'Wave Type: <select id="wave_type" name="wave_type" required>
        <option selected value="s">S</option>
        <option value="p">P</option>
        <option value="surface_wave">Surface Wave</option>
        </select>';
echo "Wave Value: <input type='text' name='wave_value' required>";
echo 'Upload CSV File: <input type="file" name="file">';
echo " <input type='submit' name='submit' value='submit'>";
echo "</form>
</div> ";

验证码:

echo "w";

if (isset($_POST['submit'])) {
    echo "t";
    $latitude = $_POST['latitude'];
    if ($latitude  == NULL) {
        echo "Please insert a latitude . <br>";
        $error = $error + 1;
    }
}

echo "q";

我在if语句中放置的表单验证不起作用。我尝试通过插入'echo“q”'和'echo“w”'来调试代码,以查看问题所在。这些陈述起作用(它们输出字符)。但if语句中的'echo“t”'不起作用。为什么会这样?

var_dump($ _ POST)的结果:

  

array(5){[“latitude”] =&gt; string(0)“”[“longitude”] =&gt; string(1)“g”   [ “wave_type”] =&GT; string(1)“s”[“wave_value”] =&gt; string(1)“g”   [ “文件”] =&GT; string(0)“”}

我的问题不在于获得验证的范围,而是为什么'echo t'不起作用?

5 个答案:

答案 0 :(得分:0)

为什么在使用纬度时检查'submit'post变量?

if(isset($_POST['latitude']))
{
     echo "t";
     $latitude=$_POST['latitude'];
}
else
{
     echo "Please insert a latitude . <br>";
     $error = $error + 1;
}

此外,提交按钮

上不需要名称
<input type="submit" value="Submit">

答案 1 :(得分:0)

<!--form code start-->
<?php
if(isset($_REQUEST['req']))
{
    echo "Please enter any input";
}
?>
<form action="validate2.php" method="POST">
    Latitude: <input type='text' name='latitude'>
    <input type='submit' name='submit' value='submit'>
</form>

<!--validate2.php code start-->
<?php
if (isset($_POST['submit']))
{
    $latitude = $_POST['latitude'];
    if ($latitude  == NULL)
    {   
        header("Location:frm.php?req=error");
    }
    else
    {
        echo "Your input Text is: ".$latitude;      
    }
}
?>

答案 2 :(得分:0)

你的表格渲染搞砸了,这很有效:

echo "<h1> Seismic Recording </h1>";
echo "<div>";
echo "<form action='' method='POST'>";

echo "Latitude: <input type='text' name='latitude'>";
echo "Longitude: <input type='text' name='longitude'd>";
echo 'Wave Type: <select id="wave_type" name="wave_type">';
echo '<option selected value="s">S</option>';
  echo '<option value="p">P</option>';
  echo '<option value="surface_wave">Surface Wave</option>';
echo "</select>";
echo "Wave Value: <input type='text' name='wave_value' d>";
echo 'Upload CSV File: <input type="file" name="file">';
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";
echo "</div>";

if(isset($_POST)){
  if(isset($_POST['submit'])){
   echo "Submit is alive!!";
  }
}

为了避免这种问题,请记住,你可以用这种方式写简单的html:

<?php
  //Mi php code blablabla
?>
/* My html code */
<h1>Wonderful day, isn't it?<h1>

<?php
  //Another php tasks and etc
?>  

答案 3 :(得分:0)

<!--Second Method--> 
<!--form code start-->
<form action="validate2.php" method="POST">
    Latitude: <input type='text' name='latitude'>
    <input type='submit' name='submit' value='submit'>
</form>

<!--validate2.php code start-->
<?php
if (isset($_POST['submit']))
{
    $latitude = $_POST['latitude'];
    if ($latitude  == NULL)
    {   
        echo '<script>alert("Please enter any input");
        window.location.href="frm.php";</script>';
    }
    else
    {
        echo "Your input Text is: ".$latitude;      
    }
}
?>

答案 4 :(得分:0)

试试这个: -

$curl_connection = 
  curl_init('http://www.domainname.com/target_url.php');

链路: - http://www.html-form-guide.com/php-form/php-form-submit.html