简体   繁体   中英

Need help regarding PHP and HTML

I'm currently working on an assignment for my web development course. The premise is to create a simple "concert ordering site". Everything is well, but I keep running into this error message:

Notice: Undefined index: chooseTheAmountOfTickets in C:\xampp\htdocs\webtech\coursework\chapter05\event.php on line 22

Here's my PHP (Note: I do have the <?php at the beginning):

$firstName = $_POST['firstName'];
$phoneNumber = $_POST['phoneNumber'];
$chooseTheAmountOfTickets = $_POST['chooseTheAmountOfTickets'];
$costOfTickets = 35;

$total = $chooseTheAmountOfTickets * $costOfTickets;

print("<p><b>Your total estimated cost is $$total. </b></p>"); 


?>] 

Here's my HTML:

    <form action = "event.php" method = "post">
    <p>First Name
    <input type = "text" size = "10" name = "firstName">
    </p>

     <form action = "event.php" method = "post">
    <p>Phone Number
    <input type = "text" size = "10" name = "phoneNumber">
    
    
    <form action = "event.php" method = "post" >
    <p>Choose the amount of tickets
    <input type= "number" method = "post">


    
    
    </p>
    <p><i>Tickets will be $35 each not including tax and fees.</i></p>
    <p>
    <input type = "submit" value = "Calculate Tickets">
    </p>
    </form>
    

    
    
    </p></td>
    <td><b>$35 PER TICKET</b></td>

You don't have a form control with a name="chooseTheAmountOfTickets" value. I'm assuming that's what this was meant to be:

<input type= "number" method = "post">

You also have multiple forms on the page. If these are all supposed to be one form submission, you want something like this:

<form action="event.php" method="post">
    <p>First Name
    <input type="text" size="10" name="firstName">
    </p>

    <p>Phone Number
    <input type="text" size="10" name="phoneNumber">
    </p>
    
    <p>Choose the amount of tickets
    <input type="number" name="chooseTheAmountOfTickets">
    </p>
    
    <p><i>Tickets will be $35 each not including tax and fees.</i></p>
    
    <p>
    <input type="submit" value="Calculate Tickets">
    </p>
</form>

Might find this helpful: https://developer.mozilla.org/en-US/docs/Learn/Forms/Your_first_form

Seems like it's struggling with this line here:

$chooseTheAmountOfTickets = $_POST['chooseTheAmountOfTickets'];

Looking at your HTML code, for the first two inputs you defined a name for them, but for the final input you did not. All you should have to do is give that input a name like so:

<input type= "number" name = "chooseTheAmountOfTickets">

Where "chooseTheAmountOfTickets" is what the php file is looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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