0

I'm new to development. Try to Learning php but I'm Having issue with php form. Here Is my Problem. I have Two Form In a page. So, When I submit data in first form then second form show > > Undefined index. Other hand, When I submit data in second form then first form show > > Undefined index. I'm try to receive data using >>> $_SERVER["PHP_SELF"]

Here Is my Enter Code

<!DOCTYPE html>
<html>
    <head>
        <title> Hello world</title>
        <link rel= "stylesheet" href="style.css" />
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    </head>
    <body class="container mt-5">
        <?php
            $name=$email="";
            if($_SERVER["REQUEST_METHOD"]=="POST"){
                $name = test_input($_POST["name"]);
                $email = test_input($_POST["email"]);
            }
            function test_input($data){
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }
        ?>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class="row">
            <div class="col-auto">
                <label class="visually-hidden" for="autoSizingInput"> Name</label>
                <input type="text" class="form-control" id="autoZizingInput" name="name" placeholder="User name"></input>
            </div>
            <div class="col-auto">
            <input type="email" class="form-control" id="autoZizingInput" name="email" placeholder="Enter Email"></input> 
           
            </div>
            <div class="col-auto">
            <button type="submit" class="btn btn-primary">Submit</button>
            </div>
            
        </form>
        <div class="container">
            <div class="row mt-5">
               <?php 
                if(!empty($name)&& !empty($email)):?>
                    <h1>Your Name Is <?php echo $name;?> & Email Address <?php echo $email;?></h1>
                    <?php
                endif;
               ?>
            </div>
        </div>

            <?php 
                $color="";
                if($_SERVER["REQUEST_METHOD"]=="POST"):
                  $color = $_POST["color"];
                endif;
            ?>
            <form class="row mt-5" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
                <input type="text" class="form-control" placeholder="Enter colot name" name="color"></input>
                <button type="submit" class="btn btn-danger mt-5"> Submit</button>
            </form>
            <div class="row">
                <?php 
                    if(!empty($color)):?>
                    <h1>Color Is <?php echo $color;?><h1>
                  <?php      
                    endif;
                ?>
            </div>

    </body>
</html>
  • So you need to check which button was pressed and only attempt to access the relevant form fields that exist in the submitted form – RiggsFolly Feb 11 '22 at 12:52
  • 2
    So you will need to give the buttons each a different `name="..."` attribute so you can indentify the bittons in code – RiggsFolly Feb 11 '22 at 12:52
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – CBroe Feb 11 '22 at 13:26

1 Answers1

1
    <!DOCTYPE html>
<html>

<head>
  <title> Hello world</title>
  <link rel="stylesheet" href="style.css" />
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>

<body class="container mt-5">
  <?php
  $name = "";
  $email = "";
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = test_input(isset($_POST["name"]) ? $_POST["name"] : '');
    $email = test_input(isset($_POST["email"]) ? $_POST["email"] : '');
  }
  function test_input($data)
  {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
  ?>
  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" class="row">
    <div class="col-auto">
      <label class="visually-hidden" for="autoSizingInput"> Name</label>
      <input type="text" class="form-control" id="autoZizingInput" name="name" placeholder="User name"></input>
    </div>
    <div class="col-auto">
      <input type="email" class="form-control" id="autoZizingInput" name="email" placeholder="Enter Email"></input>

    </div>
    <div class="col-auto">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>

  </form>
  <div class="container">
    <div class="row mt-5">
      <?php
      if (!empty($name) && !empty($email)) : ?>
        <h1>Your Name Is <?php echo isset($name) ? $name : ''; ?> & Email Address <?php echo isset($email) ? $email : ''; ?></h1>
      <?php
      endif;
      ?>
    </div>
  </div>

  <?php
  $color = "";
  if ($_SERVER["REQUEST_METHOD"] == "POST") :
    $color =  isset($_POST["color"]) ? $_POST["color"] : '';
  endif;
  ?>
  <form class="row mt-5" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
    <input type="text" class="form-control" placeholder="Enter colot name" name="color"></input>
    <button type="submit" class="btn btn-danger mt-5"> Submit</button>
  </form>
  <div class="row">
    <?php
    if (!empty($color)) : ?>
      <h1>Color Is <?php echo $color; ?><h1>
        <?php
      endif;
        ?>
  </div>

</body>

</html>

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL.

This function returns true if the variable exists and is not NULL, otherwise it returns false.

Sajeed Kannoje
  • 931
  • 1
  • 5
  • 13