Hi There,
I hope this quesiton isn't too dumb!
I have a page with a text input form, and the data in the form gets entered into a variable ($OrderID), which gets written to a text file on the web server.

After data is entered, the page reloads and the user can enter more data. I have included the relevant snippits of code below.

The problem is, when someone manually refreshes the page, or the first time they visit the page, the site writes the text file to the web server with an empty space where the variable goes. Just wondering if there's a way around this without using javascript to validate the form input?

Hope that makes sense!

Ant.

code:

$OrderID = $_POST["OrderID"];

<SCRIPT>
function setFocus() { document.form1.OrderID.focus() }
</SCRIPT>
<BODY onLoad=setFocus()></body>
OrderID:<FORM name=form1 method="post" action="<?php echo $PHP_SELF;?>">
<input name="OrderID" size="12" maxlength="48" type="text"></input>
</FORM>

    First: NEVER EVER rely on Javascript to validate the input. It is possible to send POST and GET values anyway, and some persons have Javascript disabled. It is good to use Javascript to validate, but you should validate in PHP as well.

    Second: To avoid the problem check the value in the server first. Let's as an example say that the page sends a id through POST method when data should be saved:

    if (!empty($_POST['id']) && $_POST['id'] != "")
    {
        $id = $_POST['id'];
        // Code to process and save data
    }
    else
    {
        // Do not process the code
    }
    // Code to show a new page

      Hi Piranha,
      Thanks for the quick reply!
      I have updated the script with the code you suggested, but I still can't get the form to not submit if there is nothing in it. I am guessing I need some code after the "else" section.
      I've googled around but all I can find is how to do it with a form and a submit button

      for example:
      <?php
      if (empty($stage)) { display_form(); }
      else { process_form(); }

      ?>

      Given I need the page to be the same the whole time, I'm a bit lost as to how to stop it submitting...

      At the moment, I have:
      if (!empty($POST['reference']) && $POST['reference'] != "")

      {
      $reference = $_POST['reference'];


      }
      else
      {
      $reference = noref;
      }


      I figure I need something to replace "$reference = noref" that tells it not to submit the form?

      I hope that makes sense!!

      Thanks again for the help.
      Ant.

        You can put the piece of code that submits the data to the textfile in the IF { } section

        then it wont be submitted when $_POST['reference'] is empty

          Thanks sangre, I've made the change and it's working like a dream!

          Thanks again!
          Ant

            Write a Reply...