0

I'm trying top pass a value to php and run a function. But I can not get it to work?

 <form action="" method="post">
    Name: 
    <input type="text" name="val1" id="val1"></input>
    <input type="submit" value="Submit"></input>
</form>

<?php
if( isset($_POST['submit']) ){
    $folder = htmlentities($_POST['val1']); 
}
if( isset($folder) ) {
//do something with $folder
}
?>
4
  • 1
    what is the error you got? Commented May 8, 2014 at 19:54
  • @AwladLiton there will be no error - just isset($_POST['submit']) always returns False Commented May 8, 2014 at 19:55
  • yah that is true but OP should clearify in the question that there is no error :) Commented May 8, 2014 at 19:56
  • @AwladLiton: I'm getting no error. nothing is happening. The input text goes blank after I hit the submit button. Commented May 8, 2014 at 20:07

2 Answers 2

4

You need to add name to your submit if you wan't to check for it's existence in $_POST:

<input type="submit" value="Submit" name="submit"></input>
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Worth noting that not all browser versions will post a submit button value; so it's sometimes best to post a hidden value and check for that. Best to actually use a token generated by the server and store in user session then verify that the token matches the contents of the session to protect against CSRF.
@ElmoVanKielmo: thanks. that did not work. I'm getting no response when I run the code.
1

The problem is this:

if( isset( $_POST['submit'] ) ){

Due to the fact that your submit element does not have a name (as pointed out by @ElmoVanKielmo )

The original way I proposed is to try validating your val1 field:

if( isset( $_POST['val1'] ) ){

Or you can add the name attribute to your submit.

3 Comments

This is walking around the problem and not solving it.
Why would I get a downvote? The fact that I didn't mention the submit not having a name doesn't make my answer wrong.
Added to the answer so it shows what the real error is as mentioned by @ElmoVanKielmo

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.