[英]500 Internal Server Error on PHP form
我對PHP很陌生。 我正在處理的表格有麻煩。
這是我的HTML
<form class="form-horizontal" action="submit.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name = "name" placeholder="John Doe">
</div>
</div>
<div class = "form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name = "email" placeholder="name@domain.com">
</div>
</div>
<div class="form-group">
<label for="phoneNumber" class="col-sm-2 control-label">Phone:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="phoneNumber" name = "phoneNumber" placeholder="555-555-5555">
</div>
</div>
<div class="form-group">
<label for="major" class="col-sm-2 control-label">Major:</label>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" name = "major" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Dropdown
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1"></ul>
</div>
</div>
<hr>
<div class="form-group">
<label for="itemForSale" class="col-sm-2 control-label">Item for Sale:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="itemForSale1" name = "itemForSale1" placeholder="My old video games, some chairs, some chicken, a pizza.">
</div>
</div>
<div class="form-group">
<label for="quantity" class="col-sm-2 control-label">Quantity:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id = "quantity1" name = "quantity1" placeholder="1,000,000">
</div>
</div>
<div class="form-group">
<label for="major" class="col-sm-2 control-label">Price:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="price1" name = "price1" placeholder="ex. $100.00">
</div>
</div>
</form>
這是我的PHP
<?php
// Variables
$name;
$email;
$phone;
$major;
$itemForSale1;
$quantity1;
$price1;
$itemForSale2;
$quantity2;
$price2;
$itemForSale3;
$quantity3;
$price3;
ini_set('display_errors', 'On'); ini_set('html_errors', 0); error_reporting(-1);
if (isset($_POST["submit"]))
{
// Initiate the variables
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$major = $_POST["major"];
$itemForSale1 = $_POST["itemForSale1"];
$quantity1 = $_POST["quantity1"];
$price1 = $_POST["price1"];
$itemForSale2 = $_POST["itemForSale2"];
$quantity2 = $_POST["quantity2"];
$price2 = $_POST["price2"];
$itemForSale3 = $_POST["itemForSale3"];
$quantity3 = $_POST["quantity3"];
$price3 = $_POST["price3"];
// IF name is empty string
if($name == "")
{
// Alert the user
echo "Please enter your name.";
}
// IF email is invalid
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === true)
{
// Alert the user
echo("$email is an invalid email address");
}
// Strip any occurrences of '-' in phoneNumber
str_replace("-", "", $phoneNumber)
// IF phoneNumber does not equal 10 characters
if(strlen(phoneNumber) != 10)
{
// Alert the user
echo "Invalid phone number. Ex. 315-555-5555";
}
// IF itemForSale is empty string
if($itemForSale == "")
{
// Alert the user
echo "You must enter at least one item.";
}
// IF quantity1 is less than 1
if($quantity1 < 1)
{
// Alert the user
echo "You can't sell anything less than one item.";
}
// Strip any occurrences of '$' in price
str_replace("$", "", $phoneNumber)
// IF price is less than 0.00
if($price1 < 0)
{
echo "What's less than free?";
}
echo $name;
echo $email;
echo $phone;
echo $major;
echo $itemForSale1;
echo $quantity1;
echo $price1;
}
?>
當我在表單上按Submit時,出現500 Internal Server Error。 知道是什么原因造成的嗎? 我對PHP sooo不太熟悉。
一些適合您的解決方案-
1)給表單方法標記為-method =“ post”
2)嘗試改變
<input id="submit" name="submit" type="submit" value="submit" class="btn btn-primary">
至
<input id="btnsubmit" name="btnsubmit" type="submit" value="submit" class="btn btn-primary">
並以
isset($_POST["btnsubmit"])
3)您還錯過了str_replace("$", "", $phoneNumber)
和str_replace("-", "", $phoneNumber)
之后的分號
4)將if(strlen(phoneNumber) != 10)
更改為if(strlen($phoneNumber) != 10)
5)您還用PHP代碼訪問了許多名稱錯誤的值,例如電話號碼,專業名稱等。請確保它們正確無誤,並且您的代碼應能正常工作。
您所有的str_replace
函數都不正確。 您永遠不會終止它們所在的行,並且不會在任何地方設置替換項(不說替換不是錯誤,但如果不使用它,這是沒有意義的)。
您可以這樣做:
$phoneNumber = str_replace(array('$', '-'), '', $phoneNumber);
這將剝奪$
S和-
從s $phoneNumber
。
另外,您的表單正在作為GET
處理,因為您沒有告訴它作為POST
。
更改:
<form class="form-horizontal" action="submit.php">
至
<form class="form-horizontal" action="submit.php" method="POST">
默認的表單方法是GET
, 默認的HTTP方法是什么? 。
因此, if (isset($_POST["submit"]))
永遠都不為真,那么您只會得到一個空白頁。 如果要測試此條件,可以在該條件的末尾添加else。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.