PHP - Web窗体提交按钮不起作用

时间:2016-05-04 11:30:18

标签: php forms web

我正在创建一个使用PHP连接数据库的表单。我的表格是半功能的,但是当我尝试通过按下提交按钮进行测试时,它会显示在网页上找不到文件。

以下是default.php的代码:

<!DOCTYPE HTML> <html> <head>

<title>PHP FORM - 08246 ACW PART 2</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css"> <style> .error {color:
#FF0000;} </style> </head> <body>  

<ul class="w3-navbar w3-black w3">   <li><a href="default.php">Home</a></li>   <li><a href="changelocationtostaffmember.php">Change location to staff member</a></li>   <li><a href="currentlocationofallstaff.php">Current location of all staff</a></li>   <li><a href="editpersonaldetailsofstaffmember.php">Edit personal details of staff member</a></li>   <li><a href="listalllocationsandshowlistofpeopleinselectedlocation.php">List all locations and show list of people in selected location</a></li>   <li><a href="staffmemberandlistlocationsforlast24hours.php">Staff member and list locations for last24 hours</a></li> </ul>

<div class="w3-container"> <h2> Web Form </h2> </div>

<div class="w3-container">  <?php // defining the variables and setting them to empty values $first_nameErr = $SurnameErr = $usernameErr = $passwordErr = $previous_LocationErr = $current_LocationErr = $dateErr = $timeErr = $dErr = $tErr = ""; $first_name = $Surname = $username = $password = $previous_Location = $current_Location = $date = $time = $dErr = $tErr = "";

//----validation----

//first name if($_SERVER["REQUEST_METHOD"] == "POST"){ if(empty($_POST["first_name"])){ $first_nameErr = "First Name is required"; }else{ $first_name = test_input($_POST["first_name"]); //validation checking if(!preg_match("/^[a-zA-Z ]*$/",$first_name)){ $first_nameErr = "Please enter only letter and white space";  } }

//surname if($_SERVER["REQUEST_METHOD"]=="POST"){ if(empty($_POST["Surname"])){ $SurnameErr="Surname is required"; }else{ $Surname=test_input($_POST["Surname"]); //validation checking if(preg_match("/^[a-zA-Z ]*$/",$Surname)){ $SurnameErr = " Please enter only letters and white spaces"; } }

//date and time date_default_timezone_set('UTC');

$d = str_replace('/',',', '03/05/2016'); $t = str_replace(':',',', '13:38'); $date = $t.',0,'.$d; $fulldate = explode(',',$date); echo '<br>'; $h = $fulldate[0]; $i = $fulldate[1]; $s = $fulldate[2]; $m = $fulldate[3]; $d = $fulldate[4]; $y = $fulldate[5];

echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y))."<br>"; echo strtotime ("03/05/2016 13:38");

function test_input($data){ $data=trim($data); $data=stripslashes($data); $data=hmtlspecialchars($data); return $data; } ?>

<?php//database
#server info
#$servername = "SQL2008.net.dcs.hull.ac.uk";
#$username = "ADIR\463142";//userid
#$dbname = "rde_463132"; $servername = "SQL2008.net.dcs.hull.ac.uk"; $username = "username"; $myDB = "examples"; $myLocation = "location";

// Create connection $conn = new mysqli($servername, $username, $myLocation); // Check connection if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error); } 

// Create database $sql = "CREATE DATABASE myDB"; if ($conn->query($sql) === TRUE) {
    echo "Database created successfully";   } else {
    echo "Error creating database: " . $conn->error;    }

$conn->close(); ?>

<p><span class="error">* are required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> First Name: <input type="text" name="first_name"><br> <span class="error">* <?php echo $First_nameErr;?></span> <br> Surname: <input type="text" name="Surname"><br> <span class="error">* <?php echo $SurnameErr;?></span> <br> Username: <input type="text" name="username"><br> <span class="error">* <?php echo $username;?></span> <br> Current Location: <input type="text" name="current_Location"><br> <span class="error">* <?php echo $current_Location;?></span> <br> Date: <input type="text" name="date"><br> <span class="error">* <?php echo $date;?></span> <br> Time: <input type="text" name="time"><br> <span class="error">* <?php echo $time;?></span> <br>

<input type="submit" name="submit" value="Submit"> </form>

</div> </body> </html>

我是这门语言的新手还在学习。 任何帮助或建议将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:0)

文件不正确,表单操作网址指向default.php,但您的文件名是defaul.php

如果使用default.php而不是defaul.php

为了更好地处理:

在浏览器的控制台中,请检查http调用,如果它是500,则可以看到显示的错误(检查日志/启用调试模式)

答案 1 :(得分:0)

您使用什么版本的PHP来运行此脚本?

正如我所知,您正在使用“注册全局”设置来获取$ _POST数据:http://php.net/manual/en/security.globals.php

如果你有PHP版本5.4+,你应该使用$ _POST ['form_field_name1'] ... $ _POST ['form_field_nameN']来获取表单数据。 添加支票:

if (!empty($_POST)) { /* Form validation data goes here */ }