【问题标题】:Validating requests in PHP在 PHP 中验证请求
【发布时间】:2021-03-05 19:57:19
【问题描述】:

大家好,他们很难理解如何使用 PHP 验证请求以及如何使用 else/if 语句从我的编码中获取我需要的内容。如果用户未能在我的 HTML 文件的任何三个字段中输入信息,我需要代码来显示活动的错误消息。目前我验证请求的代码如下:

enter code <?php
    $firstName = $_POST['firstName'];
    $phone = $_POST['phone'];       
    $numTickets = $_POST['numTickets'];
    $cost = $numTickets * 35.00;

    $ticketFile = fopen ("ticket-count.txt", "r");
    $totalTickets = fgets($ticketFile);
    fclose ($ticketFile);
        
    
    if (empty($firstName) OR  empty($phone) OR empty($numTickets) )
        print("ERROR: Input is missing!!");

    $availableSeats = 100 - (int)$totalTickets;
    if ($availableSeats < $numTickets) 
    {
        
        if ($availableSeats < 0) 
        {
            $availableSeats = 0;
            print ("<h1>Ticket Purchase</h1>");
            print ("<p>We are sorry. There are $availableSeats tickets available.  We 
                apologize for this inconvenience.</p>");
        }
        
        else
        {
            print ("<h1>Ticket Purchase</h1>");
            print ("<p>We are sorry. There are only $availableSeats tickets available.  Please 
                return to the form using the link below to change your order.</p>");
            print("<p><a href='event.html'>Click here to retun to order form</a></p>");

        }
    }
    else
    {
        print ("<h1>Ticket Purchase</h1>");
        print ("<p>Hi $firstName, You purchased $numTickets tickets. Your cost is $$cost. If we need to contact you we will call you at $phone.</p>");  
        
        $totalTickets = (int)$totalTickets + (int)$numTickets;
        
        $ticketFile = fopen ("ticket-count.txt", "w");
        fputs($ticketFile, "$totalTickets\n" );
        fclose ($ticketFile);
    }
    
    



?>here

现在我知道了我的代码所在的总体思路,它确实会产生错误;但是它仍然显示附加信息。例如,如果我没有在“名字”字段中输入任何信息,它会一直显示在顶部“错误:输入丢失!”然后继续显示电话字段和票数字段。

感谢任何帮助。我觉得我已经阅读了我的书一千遍,并且我尝试使用 youtube 等,但无法真正理解。 再次感谢您的任何意见!

根据要求提供 HTML:

enter code<h1>The Rolling Stones!</h1>
<p>In concert on January 15, 2011. Don't miss this great performance!</p>
<h2>To Purchase Tickets</h2>
<form action = "event.php" method = "post">
<table>
<tr><td>What is your First Name:</td><td><input type = "text" size = "20" name = "firstName"></td><tr>
<tr><td>And your Phone Number?</td><td><input type = "text" size = "20" name = "phone"></td><tr>
<tr><td>How many Tickets do you need?</td><td><input type = "text" size = "20" name = "numTickets"></td><tr>
</table>
<p><input type = "submit" value = "Submit Your Order"></p>
</form>

这里

【问题讨论】:

  • “例如,如果我没有在“名字”字段中输入任何信息,它会一直显示“错误:输入丢失!”然后继续显示电话字段和票数字段。” 那么有什么问题呢?你想让它做什么呢?另外,请提供您的 HTML。
  • 问题是我需要我的 PHP 让 HTML 只显示错误。因此,例如,如果我将“名字”字段留空并仍然填写其他字段,我需要它来产生仅指示的错误。目前,我让它在提供 $phone 和 $numTickets 的值的同时产生错误。如果需要,我也可以上传完整的 PHP。我只是对我的嵌套错误或验证本身是否错误感到困惑。
  • 我解决了!谢谢大家:)

标签: php html validation input python-requests


【解决方案1】:
<?php
    $firstName = $_POST['firstName'];
    $phone = $_POST['phone'];       
    $numTickets = $_POST['numTickets'];
    $cost = (int)$numTickets * 35.00;

    if (empty($firstName) OR  empty($phone) OR empty($numTickets) )
        print("ERROR: Input is missing!!");
    elseif ( !is_numeric($phone) OR !is_numeric($numTickets) )
        print("ERROR: Non-numeric input found!");
    
    else
    {

    
    $ticketFile = fopen ("ticket-count.txt", "r");
    $totalTickets = fgets($ticketFile);
    fclose ($ticketFile);
        
    

    $availableSeats = 100 - (int)$totalTickets;
    if ($availableSeats < $numTickets) 
    {
        
        if ($availableSeats < 0) 
        {
            $availableSeats = 0;
            print ("<h1>Ticket Purchase</h1>");
            print ("<p>We are sorry. There are $availableSeats tickets available.  We 
                apologize for this inconvenience.</p>");
        }
        
        else
        {
            print ("<h1>Ticket Purchase</h1>");
            print ("<p>We are sorry. There are only $availableSeats tickets available.  Please 
                return to the form using the link below to change your order.</p>");
            print("<p><a href='event.html'>Click here to retun to order form</a></p>");

        }
    }
    else
    {
        print ("<h1>Ticket Purchase</h1>");
        print ("<p>Hi $firstName, You purchased $numTickets tickets. Your cost is $$cost. If we need to contact you we will call you at $phone.</p>");  
        
        $totalTickets = (int)$totalTickets + (int)$numTickets;
        
        $ticketFile = fopen ("ticket-count.txt", "w");
        fputs($ticketFile, "$totalTickets\n" );
        fclose ($ticketFile);
    }
    
    }



?>

【讨论】:

    猜你喜欢
    • 2012-04-08
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 2018-02-19
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    相关资源
    最近更新 更多