TAGS :Viewed: 1 - Published at: a few seconds ago

[ Form if then Else/ifElse HTML on submit ]

I have a form that I need to act in specific ways. Example: User brings equipment back: I don't want that to show up in my form. User takes equipment out: I want it to generate to my form, they enters some information, form sends information to database. User takes equipment out: it too generates said form, he doesn't enter any information, form submits information after 20 sec, information is in database.

I was doing a simple page refresh to get the information into my form but that was pulling all the equipment that had been brought into the warehouse that day. So I commented that out and then I would get stuck on my ajax page.

I then tried creating a new php page that had a 301 page refresh and that worked to get the page back to my index page, however, my form wasn't working properly and so I commented out the auto submit and now my form works great... except I can't fulfill this last requirement where the page will submit the data if the user forgets to put his equipment information in.

I'm looking to do an if/then/else type of page submission. However, I can't figure out how to do one purely in either php or html. This is what I have figured out so far but it doesn't work and I know I'm way off somewhere.

<!DOCTYPE html>
<html>
<body>

<!--This is the one that is currently commented out in my code
<script type="text/javascript">
 window.setTimeout(function() {
 window.location = 'index.php'},1000*2);
</script>
</body>
</html> -->

  //This is my pipe dream 
   <?php
   if (isset($_POST['action'])) {
        switch ($_POST['action']) {
                    case ‘DONE’:
                        document.getElement('formSubmit').submit();
                        break;
            }
              } else {
                 <script type="text/javascript">
                     window.setTimeout(function(){
                     document.getElement('formSubmit').submit();
                },1000*30);
                 </script>
           }

          ?>

I don't know that much about jQuery other than going through a codecademy, and I know even less javascript. I'm a database person that got "conned" into building an advanced form/webpage. So I'm learning PHP, CSS, jQuery and HTML as I code.

Index.php:

<!DOCTYPE html>
<html>
<head>
<meta name=“Warehouse 3962” content="width=device-width, initial-scale=1.0">
<link href="style.css" type="text/css" rel="stylesheet">
<script type="text/javascript">
<!—window.setTimeout(function(){
              document.getElement(‘formSubmit').submit();
              },1000*30); —>
</script>
</head>
<body>
<section class="w">
    <div class="row">
        <div class="small columns">
            <img src="logo.png" />
            <h3 class="green">Users</h3>
        </div>

        <h6>Warehouse 3962</h6>
    </div>

    <div class="row">
        <div class="small columns">
            <form action="ajax.php" method="post" id="formSubmit">
                <table id="equipment-table">
                    <thead>
                        <tr>
                            <th>Equipment</th>
                            <th>Amount</th>
                            <th>Val1</th>
                            <th>Val2</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php foreach 
                        ($results['tags'] as $equipment) {
                            if ($equipment['category'] == "Part") { ?>

                                <tr>
                            <td><?= $equipment['Amount']; ?></td>
                <td class="text-center"><?= $equipment[‘quantity']; ?></td>
                                    <td><input type="text" name="val1" /></td>
                                    <td><input type="text" name="val2" /></td>
                                </tr>

                            <?php } // end if
                        } // end foreach ?>

                        <tr>
        <td colspan="4" style="text-align: right;"><input      type="submit" class="button" name="DONE" value="DONE" /></td>
                        </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
</section>
</body>
</html>

Answer 1


Please try this new solution. This will send a post every 30 seconds containing only the input boxes containing text on them.

<!DOCTYPE html>
<html>
<head>
<meta name="Warehouse 3962" content="width=device-width, initial-scale=1.0">
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
  <section class="w">
    <div class="row">
      <div class="small columns">
        <img src="logo.png" />
        <h3 class="green">Users</h3>
      </div>

      <h6>Warehouse 3962</h6>
    </div>

    <div class="row">
      <div class="small columns">
        <form action="ajax.php" method="post" id="formSubmit">
          <table id="equipment-table">
            <thead>
              <tr>
                <th>Equipment</th>
                <th>Amount</th>
                <th>Val1</th>
                <th>Val2</th>
              </tr>
            </thead>
            <tbody>
              <?php
              foreach ($results['tags'] as $equipment) :
              if ($equipment['category'] === "Part") :
              ?>
              <tr>
                <td>
                  <?php echo $equipment['Amount']; ?>
                </td>
                <td class="text-center">
                  <?php echo $equipment['quantity']; ?>
                </td>
                <td>
                  <input id="<?php echo $equipment['number'];?>-val1" type="text" name="<?php echo $equipment['number'];?>-val1"/>
                </td>
                <td>
                  <input id="<?php echo $equipment['number'];?>-val2" type="text" name="<?php echo $equipment['number'];?>-val2"/>
                </td>
              </tr>
              <?php
                endif;
                endforeach;
               ?>
              <tr>
                <td colspan="4" style="text-align: right;">
                  <input type="submit" class="button" name="DONE" value="DONE" />
                </td>
              </tr>
            </tbody>
          </table>
        </form>
      </div>
    </div>
  </section>
  <script type="text/javascript">
    function sendData() {
      var inputs = document.getElementById('equipment-table').getElementsByTagName('input'),
          data = [],
          name, val1, val2;

      for (var i = 0; i < inputs.length; i++) {
        if ( inputs[i].type === 'submit') {
          continue;
        }

        if ( inputs[i].value ) {
          name = inputs[i].name.split('-val');
          val1 = inputs[i].name.split('val1');

          if (val1.length > 1) {
            data.push({name: name[0], val1: inputs[i].value});
          }
          else {
            data.push({name: name[0], val2: inputs[i].value});
          }
        }
      }

      window.setTimeout(function() {
        sendData();
      },30000);
    }

    sendData();
  </script>
</body>
</html>

Answer 2


You cannot mix PHP with JavaScript and why you set a timeout to 1000*30, that is equal to set a timeout to 30000 which means 30 seconds. Then, you can use the 'require' attribute in the input filed to force the user to fill the required information.

<!DOCTYPE html>
<html>
<head>
<meta name="Warehouse 3962” content="width=device-width, initial-scale=1.0">
<link href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
  <section class="w">
    <div class="row">
      <div class="small columns">
        <img src="logo.png" />
        <h3 class="green">Users</h3>
      </div>

      <h6>Warehouse 3962</h6>
    </div>

    <div class="row">
      <div class="small columns">
        <form action="ajax.php" method="post" id="formSubmit">
          <table id="equipment-table">
            <thead>
              <tr>
                <th>Equipment</th>
                <th>Amount</th>
                <th>Val1</th>
                <th>Val2</th>
              </tr>
            </thead>

            <tbody>
              <?php
                foreach ($results['tags'] as $equipment) :
                  if ($equipment['category'] == "Part") : ?>

              <tr>
                <td><?= $equipment['Amount']; ?></td>
                <td class="text-center">
                  <?php echo $equipment[‘quantity']; ?>
                </td>
                <td>
                  <input id="val1" type="text" name="val1”/>
                </td>
                <td>
                  <input id="val2" type="text" name="val2"/>
                </td>
              </tr>
              <?php
                endif;
                endforeach;
               ?>
              <tr>
                <td colspan="4" style="text-align: right;">
                  <input type="submit" class="button" name="DONE" value="DONE" />
                </td>
              </tr>
            </tbody>
          </table>
        </form>
      </div>
    </div>
  </section>
  <script type="text/javascript">
    window.setTimeout(function(){
      var val1 = document.getElementById("val1").value,
          val2 = document.getElementById("val1").value;

      if ( ! val1 && ! val2 ) document.getElement('formSubmit').submit();
    },30000);
  </script>
</body>
</html>