Jump to content

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.