0

On basis of the information that the user fills in my form, I want to execute some PHP code. So after the form is submitted, it should have control on the same page and thereafter it should execute the PHP code (and not before pressing submit button). I have used <input type="hidden" value=1 name="hid"/>. When the user clicks the submit button, the value is changed to 0. But its not working. so solution please..

8
  • I tried to rephrase your question a bit, but it is still difficult to understand what you want. Please clarify your question. (e.g. what does it should have control on the same page mean?). Commented Feb 6, 2011 at 18:39
  • Are you posting the form data, as in "refreshing" the page? (refreshing isn't the right word, can't think of it atm) A bit of code would be useful. Commented Feb 6, 2011 at 18:40
  • @Felix same page in the sense same file in which the form is. Commented Feb 6, 2011 at 18:44
  • It sounds like you just want to submit the data, run some code and show the form again. Is this correct? Or do you want to show the form twice before some code is run? What's the deal with the hidden form field? Commented Feb 6, 2011 at 18:47
  • @Felix: Actually when the user clicks on submit button, validation is done and if its valid then i do change the value to 0 and thereafter the php code which is in same file is executed. but don't know how to do this. Commented Feb 6, 2011 at 18:51

1 Answer 1

2

Is this similar to what you are looking for ?

<?php

if (!isset($_POST["submit"]) ) { 
  if ($_POST["hid"] == 0 ) {  
      echo "hid is not 0. display form."; 
  }      
?>
<html>
  <head>
    <script type="text/javascript">
    function check_valid() {
      document.getElementById("hid").value = 0;     
    }
    </script>
  </head>
  <body>
    <form method="POST" action="<?php echo $PHP_SELF;?>" onsubmit="return check_valid();" >
      <input type="hidden" id="hid" name="hid" value="1" />
      <input type="submit" value="submit" name="submit"/>
      <!-- form elements go here -->
    </form>
  </body>
</html>
<?php
} else {
  echo "hid is now 0, execute the php code";    
}
?> 

EDIT: added <input type="hidden" name="hid" value="1" /> for clarity. Thanks to andre_roesti for the suggestion

Sign up to request clarification or add additional context in comments.

2 Comments

+1, but you should maybe add a <input type="hidden" name="hid" value="1" /> to the <form>, just for clarity.
hey error will occur --> undefined index:hid. I used the same code and hence again wiith the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.