Home > Net >  $_POST has only one value whereas the html document has many elements
$_POST has only one value whereas the html document has many elements

Time:07-04

I have a html document and a php file

HTML doc :

<!doctype html>
<?php
    session_start();
?>
<html lang="en">
    <head>
        <title>Vikram</title>
        <meta charset="utf-8" />
        <meta name="developer" content="Sanmitha V S" />
        <link rel="stylesheet" href="../css/showtimes.css" />
        <script src="../js/showtimes.js"></script>
        
    </head>

    <body>
        
        <header>
            <h1>Vikram</h1>
        </header>
        
        <nav>
            <h2>Showtimes : </h2> 
            <a href="#" name="st"> 12:00 </a>
            <a href="movie1_s2.html"> 15:30 </a>
            <a href="movie1_s3.html"> 19:00 </a>
        </nav>
        
        <section>
        <div>
            <form action="../php/action.php" method="post">
            <label for="no_seats" name="no_seats">No. of seats </label>
                <select id="no_seats">
                    <option name="1" value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                </select>
                
                <label for="selected">Selected seats</label>
                <textarea id="selected" name="selected" disabled></textarea>
                
                <button type="submit" name="submit">Book</button>
                <a href="details.html"><img src="../img/book.jpg" alt="book tickets" id="book" type="submit"/></a>
            </form>
        </div>
        
        <div>
            Platinum section : Rs. 220<br>
            <img src="../img/vaccant.jpg" id="a1" onclick="select(a1)" alt="a1" >
            <img src="../img/vaccant.jpg" id="a2" onclick="select(a2)" alt="a2" >
            <img src="../img/vaccant.jpg" id="a3" onclick="select(a3)" alt="a3" >
            <img src="../img/vaccant.jpg" id="a4" onclick="select(a4)" alt="a4" >
            <img src="../img/vaccant.jpg" id="a5" onclick="select(a5)" alt="a5" >
            <img src="../img/vaccant.jpg" id="a6" onclick="select(a6)" alt="a6" >
            <img src="../img/vaccant.jpg" id="a7" onclick="select(a7)" alt="a7" >
            <img src="../img/vaccant.jpg" id="a8" onclick="select(a8)" alt="a8" >
            <img src="../img/vaccant.jpg" id="a9" onclick="select(a9)" alt="a9" >
            <img src="../img/vaccant.jpg" id="a10" onclick="select(a10)" alt="a10" >
            <img src="../img/vaccant.jpg" id="a11" onclick="select(a11)" alt="a11" >
            <img src="../img/vaccant.jpg" id="a12" onclick="select(a12)" alt="a12" >
            <img src="../img/vaccant.jpg" id="a13" onclick="select(a13)" alt="a13" >
            <img src="../img/vaccant.jpg" id="a14" onclick="select(a14)" alt="a14" >
            <img src="../img/vaccant.jpg" id="a15" onclick="select(a15)" alt="a15" >
            <br>
            <img src="../img/vaccant.jpg" id="b1" onclick="select(b1)" alt="b1" >
            <img src="../img/vaccant.jpg" id="b2" onclick="select(b2)" alt="b2" >
            <img src="../img/vaccant.jpg" id="b3" onclick="select(b3)" alt="b3" >
            <img src="../img/vaccant.jpg" id="b4" onclick="select(b4)" alt="b4" >
            <img src="../img/vaccant.jpg" id="b5" onclick="select(b5)" alt="b5" >
            <img src="../img/vaccant.jpg" id="b6" onclick="select(b6)" alt="b6" >
            <img src="../img/vaccant.jpg" id="b7" onclick="select(b7)" alt="b7" >
            <img src="../img/vaccant.jpg" id="b8" onclick="select(b8)" alt="b8" >
            <img src="../img/vaccant.jpg" id="b9" onclick="select(b9)" alt="b9" >
            <img src="../img/vaccant.jpg" id="b10" onclick="select(b10)" alt="b10" >
            <img src="../img/vaccant.jpg" id="b11" onclick="select(b11)" alt="b11" >
            <img src="../img/vaccant.jpg" id="b12" onclick="select(b12)" alt="b12" >
            <img src="../img/vaccant.jpg" id="b13" onclick="select(b13)" alt="b13" >
            <img src="../img/vaccant.jpg" id="b14" onclick="select(b14)" alt="b14" >
            <img src="../img/vaccant.jpg" id="b15" onclick="select(b15)" alt="b15" >
            <br>
            Gold section : Rs. 180<br>
            <img src="../img/vaccant.jpg" id="c1" onclick="select(c1)" alt="c1" >
            <img src="../img/vaccant.jpg" id="c2" onclick="select(c2)" alt="c2" >
            <img src="../img/vaccant.jpg" id="c3" onclick="select(c3)" alt="c3" >
            <img src="../img/vaccant.jpg" id="c4" onclick="select(c4)" alt="c4" >
            <img src="../img/vaccant.jpg" id="c5" onclick="select(c5)" alt="c5" >
            <img src="../img/vaccant.jpg" id="c6" onclick="select(c6)" alt="c6" >
            <img src="../img/vaccant.jpg" id="c7" onclick="select(c7)" alt="c7" >
            <img src="../img/vaccant.jpg" id="c8" onclick="select(c8)" alt="c8" >
            <img src="../img/vaccant.jpg" id="c9" onclick="select(c9)" alt="c9" >
            <img src="../img/vaccant.jpg" id="c10" onclick="select(c10)" alt="c10" >
            <img src="../img/vaccant.jpg" id="c11" onclick="select(c11)" alt="c11" >
            <img src="../img/vaccant.jpg" id="c12" onclick="select(c12)" alt="c12" >
            <img src="../img/vaccant.jpg" id="c13" onclick="select(c13)" alt="c13" >
            <img src="../img/vaccant.jpg" id="c14" onclick="select(c14)" alt="c14" >
            <img src="../img/vaccant.jpg" id="c15" onclick="select(c15)" alt="c15" >
            <br>
            <img src="../img/vaccant.jpg" id="d1" onclick="select(d1)" alt="d1" >
            <img src="../img/vaccant.jpg" id="d2" onclick="select(d2)" alt="d2" >
            <img src="../img/vaccant.jpg" id="d3" onclick="select(d3)" alt="d3" >
            <img src="../img/vaccant.jpg" id="d4" onclick="select(d4)" alt="d4" >
            <img src="../img/vaccant.jpg" id="d5" onclick="select(d5)" alt="d5" >
            <img src="../img/vaccant.jpg" id="d6" onclick="select(d6)" alt="d6" >
            <img src="../img/vaccant.jpg" id="d7" onclick="select(d7)" alt="d7" >
            <img src="../img/vaccant.jpg" id="d8" onclick="select(d8)" alt="d8" >
            <img src="../img/vaccant.jpg" id="d9" onclick="select(d9)" alt="d9" >
            <img src="../img/vaccant.jpg" id="d10" onclick="select(d10)" alt="d10" >
            <img src="../img/vaccant.jpg" id="d11" onclick="select(d11)" alt="d11" >
            <img src="../img/vaccant.jpg" id="d12" onclick="select(d12)" alt="d12" >
            <img src="../img/vaccant.jpg" id="d13" onclick="select(d13)" alt="d13" >
            <img src="../img/vaccant.jpg" id="d14" onclick="select(d14)" alt="d14" >
            <img src="../img/vaccant.jpg" id="d15" onclick="select(d15)" alt="d15" >
            <br>
            <img src="../img/vaccant.jpg" id="e1" onclick="select(e1)" alt="e1" >
            <img src="../img/vaccant.jpg" id="e2" onclick="select(e2)" alt="e2" >
            <img src="../img/vaccant.jpg" id="e3" onclick="select(e3)" alt="e3" >
            <img src="../img/vaccant.jpg" id="e4" onclick="select(e4)" alt="e4" >
            <img src="../img/vaccant.jpg" id="e5" onclick="select(e5)" alt="e5" >
            <img src="../img/vaccant.jpg" id="e6" onclick="select(e6)" alt="e6" >
            <img src="../img/vaccant.jpg" id="e7" onclick="select(e7)" alt="e7" >
            <img src="../img/vaccant.jpg" id="e8" onclick="select(e8)" alt="e8" >
            <img src="../img/vaccant.jpg" id="e9" onclick="select(e9)" alt="e9" >
            <img src="../img/vaccant.jpg" id="e10" onclick="select(e10)" alt="e10" >
            <img src="../img/vaccant.jpg" id="e11" onclick="select(e11)" alt="e11" >
            <img src="../img/vaccant.jpg" id="e12" onclick="select(e12)" alt="e12" >
            <img src="../img/vaccant.jpg" id="e13" onclick="select(e13)" alt="e13" >
            <img src="../img/vaccant.jpg" id="e14" onclick="select(e14)" alt="e14" >
            <img src="../img/vaccant.jpg" id="e15" onclick="select(e15)" alt="e15" >
            <br>
            <img src="../img/vaccant.jpg" id="f1" onclick="select(f1)" alt="f1" >
            <img src="../img/vaccant.jpg" id="f2" onclick="select(f2)" alt="f2" >
            <img src="../img/vaccant.jpg" id="f3" onclick="select(f3)" alt="f3" >
            <img src="../img/vaccant.jpg" id="f4" onclick="select(f4)" alt="f4" >
            <img src="../img/vaccant.jpg" id="f5" onclick="select(f5)" alt="f5" >
            <img src="../img/vaccant.jpg" id="f6" onclick="select(f6)" alt="f6" >
            <img src="../img/vaccant.jpg" id="f7" onclick="select(f7)" alt="f7" >
            <img src="../img/vaccant.jpg" id="f8" onclick="select(f8)" alt="f8" >
            <img src="../img/vaccant.jpg" id="f9" onclick="select(f9)" alt="f9" >
            <img src="../img/vaccant.jpg" id="f10" onclick="select(f10)" alt="f10" >
            <img src="../img/vaccant.jpg" id="f11" onclick="select(f11)" alt="f11" >
            <img src="../img/vaccant.jpg" id="f12" onclick="select(f12)" alt="f12" >
            <img src="../img/vaccant.jpg" id="f13" onclick="select(f13)" alt="f13" >
            <img src="../img/vaccant.jpg" id="f14" onclick="select(f14)" alt="f14" >
            <img src="../img/vaccant.jpg" id="f15" onclick="select(f15)" alt="f15" >
            <br>
            Silver section : Rs. 100<br>
            <img src="../img/vaccant.jpg" id="g1" onclick="select(g1)" alt="g1" >
            <img src="../img/vaccant.jpg" id="g2" onclick="select(g2)" alt="g2" >
            <img src="../img/vaccant.jpg" id="g3" onclick="select(g3)" alt="g3" >
            <img src="../img/vaccant.jpg" id="g4" onclick="select(g4)" alt="g4" >
            <img src="../img/vaccant.jpg" id="g5" onclick="select(g5)" alt="g5" >
            <img src="../img/vaccant.jpg" id="g6" onclick="select(g6)" alt="g6" >
            <img src="../img/vaccant.jpg" id="g7" onclick="select(g7)" alt="g7" >
            <img src="../img/vaccant.jpg" id="g8" onclick="select(g8)" alt="g8" >
            <img src="../img/vaccant.jpg" id="g9" onclick="select(g9)" alt="g9" >
            <img src="../img/vaccant.jpg" id="g10" onclick="select(g10)" alt="g10" >
            <img src="../img/vaccant.jpg" id="g11" onclick="select(g11)" alt="g11" >
            <img src="../img/vaccant.jpg" id="g12" onclick="select(g12)" alt="g12" >
            <img src="../img/vaccant.jpg" id="g13" onclick="select(g13)" alt="g13" >
            <img src="../img/vaccant.jpg" id="g14" onclick="select(g14)" alt="g14" >
            <img src="../img/vaccant.jpg" id="g15" onclick="select(g15)" alt="g15" >
            <br>
            <img src="../img/vaccant.jpg" id="h1" onclick="select(h1)" alt="h1" >
            <img src="../img/vaccant.jpg" id="h2" onclick="select(h2)" alt="h2" >
            <img src="../img/vaccant.jpg" id="h3" onclick="select(h3)" alt="h3" >
            <img src="../img/vaccant.jpg" id="h4" onclick="select(h4)" alt="h4" >
            <img src="../img/vaccant.jpg" id="h5" onclick="select(h5)" alt="h5" >
            <img src="../img/vaccant.jpg" id="h6" onclick="select(h6)" alt="h6" >
            <img src="../img/vaccant.jpg" id="h7" onclick="select(h7)" alt="h7" >
            <img src="../img/vaccant.jpg" id="h8" onclick="select(h8)" alt="h8" >
            <img src="../img/vaccant.jpg" id="h9" onclick="select(h9)" alt="h9" >
            <img src="../img/vaccant.jpg" id="h10" onclick="select(h10)" alt="h10" >
            <img src="../img/vaccant.jpg" id="h11" onclick="select(h11)" alt="h11" >
            <img src="../img/vaccant.jpg" id="h12" onclick="select(h12)" alt="h12" >
            <img src="../img/vaccant.jpg" id="h13" onclick="select(h13)" alt="h13" >
            <img src="../img/vaccant.jpg" id="h14" onclick="select(h14)" alt="h14" >
            <img src="../img/vaccant.jpg" id="h15" onclick="select(h15)" alt="h15" >
            <br>
            <img src="../img/vaccant.jpg" id="i1" onclick="select(i1)" alt="i1" >
            <img src="../img/vaccant.jpg" id="i2" onclick="select(i2)" alt="i2" >
            <img src="../img/vaccant.jpg" id="i3" onclick="select(i3)" alt="i3" >
            <img src="../img/vaccant.jpg" id="i4" onclick="select(i4)" alt="i4" >
            <img src="../img/vaccant.jpg" id="i5" onclick="select(i5)" alt="i5" >
            <img src="../img/vaccant.jpg" id="i6" onclick="select(i6)" alt="i6" >
            <img src="../img/vaccant.jpg" id="i7" onclick="select(i7)" alt="i7" >
            <img src="../img/vaccant.jpg" id="i8" onclick="select(i8)" alt="i8" >
            <img src="../img/vaccant.jpg" id="i9" onclick="select(i9)" alt="i9" >
            <img src="../img/vaccant.jpg" id="i10" onclick="select(i10)" alt="i10" >
            <img src="../img/vaccant.jpg" id="i11" onclick="select(i11)" alt="i11" >
            <img src="../img/vaccant.jpg" id="i12" onclick="select(i12)" alt="i12" >
            <img src="../img/vaccant.jpg" id="i13" onclick="select(i13)" alt="i13" >
            <img src="../img/vaccant.jpg" id="i14" onclick="select(i14)" alt="i14" >
            <img src="../img/vaccant.jpg" id="i15" onclick="select(i15)" alt="i15" >
            <br>
            <img src="../img/vaccant.jpg" id="j1" onclick="select(j1)" alt="j1" >
            <img src="../img/vaccant.jpg" id="j2" onclick="select(j2)" alt="j2" >
            <img src="../img/vaccant.jpg" id="j3" onclick="select(j3)" alt="j3" >
            <img src="../img/vaccant.jpg" id="j4" onclick="select(j4)" alt="j4" >
            <img src="../img/vaccant.jpg" id="j5" onclick="select(j5)" alt="j5" >
            <img src="../img/vaccant.jpg" id="j6" onclick="select(j6)" alt="j6" >
            <img src="../img/vaccant.jpg" id="j7" onclick="select(j7)" alt="j7" >
            <img src="../img/vaccant.jpg" id="j8" onclick="select(j8)" alt="j8" >
            <img src="../img/vaccant.jpg" id="j9" onclick="select(j9)" alt="j9" >
            <img src="../img/vaccant.jpg" id="j10" onclick="select(j10)" alt="j10" >
            <img src="../img/vaccant.jpg" id="j11" onclick="select(j11)" alt="j11" >
            <img src="../img/vaccant.jpg" id="j12" onclick="select(j12)" alt="j12" >
            <img src="../img/vaccant.jpg" id="j13" onclick="select(j13)" alt="j13" >
            <img src="../img/vaccant.jpg" id="j14" onclick="select(j14)" alt="j14" >
            <img src="../img/vaccant.jpg" id="j15" onclick="select(j15)" alt="j15" >
            <br><br><br>
            <img src="../img/screen_this_way.png" alt="Screen this way" id="stw"/>
        </div>
        </section>
        
    </body>
</html>

And my action.php file :

<!doctype html>
<?php
    session_start();
?>
<html>
    <body>
        <?php
        var_dump($_POST);
        if (isset($_POST["submit"])===true) {
            #if ($_SESSION["st"] = $_POST["selected"]) {
                echo "successful";
            } 
        else {
            ;
        }
        ?>
    </body>
</html>

The output viewed in Xampp Server : Xampp Server

I have many elements in my HTML document like textarea, select, etc. But $_POST shows only submit in it. I have searched a lot in web about $_POST and scrolled many feed in SO related to my query but I ended in vain.

Please help me !

CodePudding user response:

$_POST (and all REST calls with HTML) require a name attribute:

<form action="../php/action.php" method="post">
  <label for="no_seats" name="no_seats">No. of seats </label>
    <select id="no_seats" name="no_steats">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
    </select>
  <label for="selected">Selected seats</label>
  <textarea id="selected" name="selected" disabled></textarea>
  <button type="submit">Book</button>
</form>

Notice how the select gets the name attribute instead of the option and how the a has been removed as it belongs outside of the form

Also you don't need a select at all:

<input type="number" min="0" max="10" value="0">

CodePudding user response:

Any piece of information which you want to handle needs to be referenced in both the HTML and PHP files.

In HTML, you use the name="" property. This should be in the <input> or <select> tag(s) and not on the <label>.

A quick sidenote: For number values in your form, you could use the <input type="number" min="1" max="5">.

    <form action="action-file-name.php" method="post">
      <label for="no_seats">
      <!-- <select name="no_seats" id="no_seats">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select> -->

        <input type="number" min="1" max="10" name="no_seats">
    </label>
    <label for="otherField">
      <input type="text" id="otherField" name="reference_in_php" placeholder="Enter details here..." value="">
    </label>
  </form>

For PHP, you need to reference the name="" property you specified in HTML.

<?php
    /**
     * If $_POST["submit"] is not set...
     */
    if (!isset($_POST["submit"])) {

      /**
       * Use a 'gated' approach (this is a good security approach).
       * Essentially, it means that the details can be assumed to be incorrect 
       * so as to stop unauthorised access from the very beginning.
       * 
       * If the submit value is NOT set:
       * 
       * 1. Force the session to end 
       * -
       * OR
       * -
       * 2. Send the user somewhere else, perhaps a custom 403 page
       * 
       * ----------------------
       * It's also worth setting a HTTP response so that your server
       * and any future front-end scripts e.g. JS will know this is
       * not available or accessible. 
       * 
       * E.g. http_response_code(403);
       */ 

      http_response_code(403);

      /* 1 */
      die("Message to show to user");

      /* 2 */
      header("location: 403.php")

    } else {

      /**
       * We now know that the details were 'submitted', so let's
       * do what we need to do.
       * 
       * 1. Let's get the $_POST values.
       * 
       * Never trust a value sent by $_POST. Sanitise it first to 
       * make sure the value doesn't contain unwanted values. 
       */

      $number_of_seats = htmlentities($_POST['no_seats']);

      echo $number_of_seats;

      /* The rest of the code here... */

  ?>
  • Related