Jump to content

Forms within functions - help me!


ihw13

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/53504-forms-within-functions-help-me/
Share on other sites

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.