0

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>
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Hi, so `chooseTheAmountOfTickets` should be an input field in the form html. This might be of interest https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_and_retrieving_form_data – IronMan Oct 21 '20 at 20:18
  • 3
    You cannot and do not need to Nest Forms – RiggsFolly Oct 21 '20 at 20:20
  • `` Inputs do not have a `method` attribute – RiggsFolly Oct 21 '20 at 20:21
  • This isn't your issue, looks like other people have that covered, but with this line ```print("

    Your total estimated cost is $$total.

    "); ```, you don't need $$total, just make it $total. $$total turns it into a variable variable which you can read about here: https://www.php.net/manual/en/language.variables.variable.php
    – Fly_Moe Oct 21 '20 at 20:26

2 Answers2

2

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

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
1

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.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • Thank you so much! This helped tremendously, honestly it's been hard due to COVID with my learning at home. I like being in a class setting and every other site I asked for help, they made me feel really stupid for even asking. I'll make sure to follow up on the other links as well. Thank you!! – Alex canales Oct 21 '20 at 21:03