简体   繁体   English

PHP表格上的500 Internal Server Error

[英]500 Internal Server Error on PHP form

I'm pretty new with PHP. 我对PHP很陌生。 I'm having trouble with a form I'm working with. 我正在处理的表格有麻烦。

Here is my HTML 这是我的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>

Here is my PHP 这是我的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;

    }
?>

When I press submit on my form I get the 500 Internal Server Error. 当我在表单上按Submit时,出现500 Internal Server Error。 Any idea what might be causing this? 知道是什么原因造成的吗? I'm not very familiar with PHP sooo. 我对PHP sooo不太熟悉。

Some solutions for you- 一些适合您的解决方案-

1) give form method tag as - method="post" 1)给表单方法标记为-method =“ post”

2) Try changing 2)尝试改变

<input id="submit" name="submit" type="submit" value="submit" class="btn btn-primary">

to

<input id="btnsubmit" name="btnsubmit" type="submit" value="submit" class="btn btn-primary">

and access it as 并以

isset($_POST["btnsubmit"])

3) also you have missed semi-colon after str_replace("$", "", $phoneNumber) and str_replace("-", "", $phoneNumber) 3)您还错过了str_replace("$", "", $phoneNumber)str_replace("-", "", $phoneNumber)之后的分号

4) change if(strlen(phoneNumber) != 10) to if(strlen($phoneNumber) != 10) 4)将if(strlen(phoneNumber) != 10)更改为if(strlen($phoneNumber) != 10)

5) You also have accessed many values with wrong names in PHP code like phone number, major etc. Please make them correct and your code should work. 5)您还用PHP代码访问了许多名称错误的值,例如电话号码,专业名称等。请确保它们正确无误,并且您的代码应能正常工作。

All your str_replace functions are incorrect. 您所有的str_replace函数都不正确。 You never terminated the lines they are on and aren't setting the replacement anywhere (not replacing isn't an error, per say, but doesn't make sense to do if not using it). 您永远不会终止它们所在的行,并且不会在任何地方设置替换项(不说替换不是错误,但如果不使用它,这是没有意义的)。

You could do: 您可以这样做:

$phoneNumber =  str_replace(array('$', '-'), '', $phoneNumber);

Which will strip $ s and - s from $phoneNumber . 这将剥夺$ S和-从s $phoneNumber

Additionally your form is processing as a GET because you haven't told it to process as a POST . 另外,您的表单正在作为GET处理,因为您没有告诉它作为POST

Change: 更改:

<form class="form-horizontal" action="submit.php">

to

<form class="form-horizontal" action="submit.php" method="POST">

The default form method is GET , What is the default form HTTP method? 默认的表单方法是GET默认的HTTP方法是什么? .

So if (isset($_POST["submit"])) is never true and you just get a blank page. 因此, if (isset($_POST["submit"]))永远都不为真,那么您只会得到一个空白页。 You can add an else to the end of that conditional if you want to test this. 如果要测试此条件,可以在该条件的末尾添加else。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM