ihw13 Posted May 30, 2007 Share Posted May 30, 2007 Basically I want to know if there's a way to execute a form within a function and alter the functions response based on the persons input through the form. Hopefully that makes sense, here some code and I'll try explaining it further: function attack_response($racer) { echo "How would you like $racer to respond?<br>\n"; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="radio" name="response" value="follow">Follow Move <input type="radio" name="response" value="sit">Sit In<br> <input type="submit" name="submit" value="Submit"> </form> <?php } The function is within a class. In my main page I call one function, which calls another classes function which then calls this classes function. When I submit this form, and use the following: if (isset($_POST['submit'])) { echo "submitted"; } It will not work inside the function or class and when I call it it uses the main page and echos the result on that main page. I need it to echo the result (and possibly return a variable) within the function. There must be a way to do this, but I am very new to O.O.P. and was looking for some help. Thanks in advance. P.S. I'm using PHP5 and please tell me if I'm nuts! Quote Link to comment https://forums.phpfreaks.com/topic/53504-forms-within-functions-help-me/ Share on other sites More sharing options...
jnewing Posted June 1, 2007 Share Posted June 1, 2007 You sure can but i would strongly suggest you look into a template system like Smarty ( http://smarty.php.net ) it will keep things a lot cleaner. I find its all ways good practice to keep help separate from you php. With Smarty you could do something like <?php function chkResponce($resp) { switch ($resp) { case 'something': $smarty->display('form1.tpl'); break; case 'somethingelse': $smarty->display('form2.tpl'); break; // and so and so on } } ?> Forgot to add: form1.tpl could look like <form action="" method="post"> <input type="radio" name="response" value="follow">Follow Move <input type="radio" name="response" value="sit">Sit In<br> <input type="submit" name="submit" value="Submit"> </form> where as form2.tpl could look totally different. Quote Link to comment https://forums.phpfreaks.com/topic/53504-forms-within-functions-help-me/#findComment-266304 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.