0

I am trying to understand why this example form is acting though the submit button is set to true when the page initially loads. This is an example, but the underlying problem is that I am trying to do something when the submit button is click, but when the page loads, the code runs as though the button has already been clicked.

<?php
   $GLOBALS["zipcode"] = null;
?>

<!--========================Dialogue Box========================-->

<h2 class="ind3">Service Area</h2>

<form name="zipcodeform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <p>Enter your zip code:</p>
   Zip:
   <label><input type="text" name="zipcode" value=""></label>
   <p /><p />
   <input type="submit" name="submit" value="Submit">

</form>

<?php
   /*Test to see if form submit button has been pressed. */
   If (isset($_POST['submit'])) {echo "the button has been pressed!";}
?>

<!--========================Dialogue Box========================-->

I know this is something simple, but I've spent hours googling and trying to figure out what I'm doing wrong. I want the PHP code at the end of the dialogue box to only display if the button is pressed, not automatically when the page loads. Thanks for your help!

Tom Anson
  • 3
  • 1
  • Are you trying to do something on the client side or on the PHP side after it submitted? if you start with a fresh load it should not be true, if after you submit it will be true. – Eric G Apr 17 '15 at 21:39
  • Ultimately, I want to create a form that will look for a string in an external text file (specifically a zip code) to see if the user's zip code is on the list of serviceable zip codes. However, I'm just first trying to get my head wrapped around how PHP handles variables and forms first. I'm coming from an asp.net background, so this is a bit of a learning process for me as well. – Tom Anson Apr 18 '15 at 01:55

1 Answers1

0

It looks like you are trying to run server side code for a client-side action.

PHP will always execute before the page loads. If you want to capture a user's action on the button, you will need to use JavaScript to handle the click event or the submit event.

See the difference between server side and client side coding.

Community
  • 1
  • 1
Eric G
  • 907
  • 1
  • 9
  • 30
  • OK, I had read that too, but I also saw answers with purely php methods of doing this--obviously I either misunderstood, or they were just wrong. I wil look into JavaScript submit as a solution to this problem. Thanks for your quick response and the links you provided. – Tom Anson Apr 18 '15 at 02:00