1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need help w/ form

Discussion in 'PHP' started by pshaw, Jun 2, 2021.

  1. #1
    Hello nice peoples. I have this form that has me puzzled(what me no way).
    It works(with needless warnings) if I submit it twice. I know it's a lot
    but can you see my error?
    -------------------------------------------------
    this is code:

    <!DOCTYPE HTML><html>
    <head>
    
    <style>
    .error {color: #FF0000;}
    </style>
    
    </head>
    <body bgcolor="ccffff"><center><b> 
    
    <?php
    echo "<center>";echo date('m/d/y');echo "</center>";
    
    // define variables and set to empty values
    $tenantErr = $unitErr = $rentpaidErr = $datepaidErr = $commentErr = "";
    $tenant = $unit = $rentpaid = $hudpay = $datepaid = $comment = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    
      if (empty($_POST["tenant"])) { $tenantErr = "tenant is required"; }
    else {
      $tenant = test_input($_POST["tenant"]);
      // check if tenant only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z-' ]*$/",$tenant)) {
      $tenantErr = "Only letters and white space allowed"; }
      }
    }
    
      if (empty($_POST["unit"])) {
      $unitErr = "unit is required";
      }
    else {
      $unit = test_input($_POST["unit"]);
      // check if unit only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z-' ]*$/",$unit)) {
      $unitErr = "Only letters and white space allowed";
      }
      }
      
      if (empty($_POST["rentpaid"])) {
      $rentpaidErr = "rentpaid is required";
      }
    else {
      $rentpaid = test_input($_POST["rentpaid"]);
      // check if rentpaid only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z-' ]*$/",$rentpaid)) {
      $rentpaidErr = "Only letters and white space allowed";
      }
      }
    
    if (empty($_POST["hudpay"])) {
      $hudpayErr = "hudpay is required";
      }
    else {
      $hudpay = test_input($_POST["hudpay"]);
      // check if hudpay only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z-' ]*$/",$hudpay)) {
      $hudpayErr = "Only letters and white space allowed"; 
      }
      }
    
    if (empty($_POST["datepaid"])) {
      $datepaidErr = "datepaid is required";
      }
    else {
      $datepaid = test_input($_POST["datepaid"]);
      // check if datepaid only contains letters and whitespace
      if (!preg_match("/^[a-zA-Z-' ]*$/",$datepaid)) {
      $datepaidErr = "Only letters and white space allowed"; 
      }
      }
    
    
    if (empty($_POST["comment"])) {
      $comment = "";
      }
    else {
      $comment = test_input($_POST["comment"]);
      }
    
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    ?>
    
    <h3>Fill in form for rent payment</h3>
    <p><span class="error">* required field</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      Tenant: <input type="text" name="tenant" value="<?php echo $tenant;?>">
      <span class="error">* <?php echo $tenantErr;?></span>
      <br><br>
      Unit: <input type="text" name="unit" value="<?php echo $unit;?>">
      <span class="error">* <?php echo $unitErr;?></span>
      <br><br>
      Rentpaid: <input type="text" name="rentpaid" value="<?php echo $rentpaid;?>">
      <span class="error">* <?php echo $rentpaidErr;?></span>
      <br><br>
    Hudpay: <input type="text" name="hudpay" value="<?php echo $hudpay;?>">
      <span class="error">* <?php echo $hudpayErr;?></span>
      <br><br>
      Datepaid: <input type="text" name="datepaid" value="<?php echo $datepaid;?>">
      <span class="error">* <?php echo $datepaidErr;?></span>
      <br><br>
    Comment: <textarea name="comment" rows="2" cols="100"><?php echo $comment;?></textarea>
      <br><br>
      <input type="submit" name="submit" value="Submit"> 
    </form>
    
    <?php
    //echo "<h2>Data for Payment:</h2>";
    echo $tenant;echo "<br>";
    echo $unit;echo "<br>";
    echo $rentpaid;echo "<br>";
    echo $datepaid;echo "<br>";
    echo $comment;echo "<br>";
    ?>
    </b></center></body></html>
    -----------------------------------------
    this is appearamce of form:
    SEMrush
    06/02/21
    Fill in form for rent payment

    * required field
    Tenant: *

    Unit: * unit is required

    Rentpaid: * rentpaid is required

    Hudpay: * hudpay is required

    Datepaid: * datepaid is required

    Comment:
    -----------------------------------------
    when I fill in the form and submit this is displayed:
    06/02/21
    Fill in form for rent payment

    * required field
    Tenant: * Only letters and white space allowed

    Unit: * Only letters and white space allowed

    Rentpaid: * Only letters and white space allowed

    Hudpay: * Only letters and white space allowed

    Datepaid: * Only letters and white space allowed

    Comment:

    tenant5
    apt1
    530.00
    3/8/2021

    ----------------------------------
     
    Last edited by a moderator: Jun 2, 2021
    pshaw, Jun 2, 2021 IP
    SEMrush
  2. Julia Shamray

    Julia Shamray Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Hey, try this code. I added the definition of this ($hudpayErr) variable at the beginning. If there are any more problems with the code, I am happy to help.

    <!DOCTYPE HTML><html>
    <head>
    
    <style>
    .error {color: #FF0000;}
    </style>
    
    </head>
    <body bgcolor="ccffff"><center><b>
    
    <?php
    echo "<center>" . date('m/d/y') . "</center>";
    
    // define variables and set to empty values
    $tenantErr = $unitErr = $rentpaidErr = $datepaidErr = $commentErr = $hudpayErr = "";
    $tenant = $unit = $rentpaid = $hudpay = $datepaid = $comment = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    
        if (empty($_POST["tenant"]))
        {
            $tenantErr = "tenant is required";
        }
        else
        {
            $tenant = test_input($_POST["tenant"]);
            // check if tenant only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z-' ]*$/", $tenant))
            {
                $tenantErr = "Only letters and white space allowed";
            }
        }
    }
    
    if (empty($_POST["unit"]))
    {
        $unitErr = "unit is required";
    }
    else
    {
        $unit = test_input($_POST["unit"]);
        // check if unit only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z-' ]*$/", $unit))
        {
            $unitErr = "Only letters and white space allowed";
        }
    }
    
    if (empty($_POST["rentpaid"]))
    {
        $rentpaidErr = "rentpaid is required";
    }
    else
    {
        $rentpaid = test_input($_POST["rentpaid"]);
        // check if rentpaid only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z-' ]*$/", $rentpaid))
        {
            $rentpaidErr = "Only letters and white space allowed";
        }
    }
    
    if (empty($_POST["hudpay"]))
    {
        $hudpayErr = "hudpay is required";
    }
    else
    {
        $hudpay = test_input($_POST["hudpay"]);
        // check if hudpay only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z-' ]*$/", $hudpay))
        {
            $hudpayErr = "Only letters and white space allowed";
        }
    }
    
    if (empty($_POST["datepaid"]))
    {
        $datepaidErr = "datepaid is required";
    }
    else
    {
        $datepaid = test_input($_POST["datepaid"]);
        // check if datepaid only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z-' ]*$/", $datepaid))
        {
            $datepaidErr = "Only letters and white space allowed";
        }
    }
    
    if (empty($_POST["comment"]))
    {
        $comment = "";
    }
    else
    {
        $comment = test_input($_POST["comment"]);
    }
    
    function test_input($data)
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }
    ?>
    
    <h3>Fill in form for rent payment</h3>
    <p><span class="error">* required field</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
      Tenant: <input type="text" name="tenant" value="<?php echo $tenant; ?>">
      <span class="error">* <?php echo $tenantErr; ?></span>
      <br><br>
      Unit: <input type="text" name="unit" value="<?php echo $unit; ?>">
      <span class="error">* <?php echo $unitErr; ?></span>
      <br><br>
      Rentpaid: <input type="text" name="rentpaid" value="<?php echo $rentpaid; ?>">
      <span class="error">* <?php echo $rentpaidErr; ?></span>
      <br><br>
    Hudpay: <input type="text" name="hudpay" value="<?php echo $hudpay; ?>">
      <span class="error">* <?php echo $hudpayErr; ?></span>
      <br><br>
      Datepaid: <input type="text" name="datepaid" value="<?php echo $datepaid; ?>">
      <span class="error">* <?php echo $datepaidErr; ?></span>
      <br><br>
    Comment: <textarea name="comment" rows="2" cols="100"><?php echo $comment; ?></textarea>
      <br><br>
      <input type="submit" name="submit" value="Submit">
    </form>
    
    <?php
    //echo "<h2>Data for Payment:</h2>";
    echo $tenant;
    echo "<br>";
    echo $unit;
    echo "<br>";
    echo $rentpaid;
    echo "<br>";
    echo $datepaid;
    echo "<br>";
    echo $comment;
    echo "<br>";
    ?>
    </b></center></body></html>
    
     
    Julia Shamray, Jun 25, 2021 IP