Gavski Posted January 16, 2014 Share Posted January 16, 2014 I have a form that contains a function. i want this function to be activated only when the user reposts the page to itself. (php_self). ie. the user access the page and the function is hidden, the user then inputs data, repost the page and the function is activated. I've tried flagging the function with a variable - $var = 0, then $var = 1 at the end of the page but $var just resets itself to 0 when the page is posted. Sure this is simple but I just cant get it? thanks Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/ Share on other sites More sharing options...
ginerjm Posted January 17, 2014 Share Posted January 17, 2014 You have a form with a function? That's about the only thing I understood from your post. And I don't believe it. I'd love to see the code that implements this 'thing'. Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465505 Share on other sites More sharing options...
KaiSheng Posted January 17, 2014 Share Posted January 17, 2014 it is php. pre hypertext process. It won't work. Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465515 Share on other sites More sharing options...
Shayna23 Posted January 17, 2014 Share Posted January 17, 2014 (edited) Good question, I'd like to see the answer, too. Edited January 17, 2014 by Shayna23 Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465524 Share on other sites More sharing options...
Gavski Posted January 17, 2014 Author Share Posted January 17, 2014 Here's my form, it contains a function (really!). I want this function to work only after the form has been posted (not on initial loading). Here PHP reads the value of $token inside the function on loading (setsit to 1) and so executes the function when the page is loaded, there must be a way round this? <?phpsession_start();$_SESSION['regName'] = $regValue; $token = 0; //here's the functionfunction doThis(){ echo "Do this Function";}if ($token = 1){doThis();$token = 1;}echo $token;?><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" ><input type="text" name="regName" value=""><input type="submit"></form> Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465548 Share on other sites More sharing options...
Gavski Posted January 17, 2014 Author Share Posted January 17, 2014 Sorry here it is again without the glaring error - <?phpsession_start();$_SESSION['regName'] = $regValue;$token = 0;//here's the functionfunction doThis(){ echo "Do this Function";}if ($token = 1){doThis();}$token = 1;echo $token;?><form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" ><input type="text" name="regName" value=""><input type="submit"></form> Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465550 Share on other sites More sharing options...
cyberRobot Posted January 17, 2014 Share Posted January 17, 2014 You could use a hidden field to run the function. For example: <?php function doThis() { echo "Do this Function"; } if(isset($_POST['runFunction'])) { doThis(); } ?> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" > <input type="text" name="regName" value=""> <input type="hidden" name="runFunction" value="1"> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465556 Share on other sites More sharing options...
ginerjm Posted January 17, 2014 Share Posted January 17, 2014 For future reference you have a "script with a function and a form". Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465584 Share on other sites More sharing options...
Solution Gavski Posted January 17, 2014 Author Solution Share Posted January 17, 2014 Thanks, that's great, Ive found a similar but simpler solution - if ($_SERVER["REQUEST_METHOD"] == "POST") { myfunction(); } test whether a post has been made (in pre hypertext process.) thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/285426-php-posting-problem/#findComment-1465603 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.