Jump to content

Processing form data within functions


ihw13

Recommended Posts

I asked this question way earlier and didn't get the right answer because I think I asked it wrong.

 

Basically I have a game type that needs user input at certain points and then needs to act on that input.

 

Here's the code I currently have that doesn't work well:

 

<?php
class form{

function show_form()
{
	echo'<form method="POST" action="';
	echo "#$form->process_form()";
	echo '">';
	echo '<input type="radio" name="response" value="f">
	Follow Move	<input type="radio" name="response" value="s">Sit In<br>
	<input type="submit" name="submit" value="Submit">';
	echo '<input type="hidden" name="submit_check" value="1"/>
	</form>';
}
function process_form()
{
	return $_POST['response'];
}

}

?>

 

Yes I realize the form is written poorly, but that's not the issue. Basically in another function I call $form->show_form(); then I call $response=$form->process_form(); somehow I need to display the form, process it, and return the value of 'response' to the original function where I called $form->show_form();.

 

Ideally I could call this function once something like $form=$form->xform(); where the xform() function would display and process the form and return the value of 'response' into the $form variable which I can then work with.

 

Hopefully that makes sense, thanks in advance.

Link to comment
Share on other sites

The action="..." parameter of a form must be a URL. When a browser submits form data, it makes a http request for the URL in the action="..." parmater and then sends the POST (or GET) data to the server.

 

You cannot put a php class function in the action="..." parameter for a couple of reasons, 1) the browser has absolutely no knowladge of what code exists in a php file on the server and 2) your must supply a URL, as that is the only way the http/https protocol works.

 

Even if you were using AJAX, you are still making a http request for a URL.

Link to comment
Share on other sites

Your code is way to complicated for the job at hand. It doesn't really require a class anyway tbh unless you're going to add a lot to it.

<?php
if (!$_POST)
  {

echo'
<form method="POST" action="'.$_SERVER['PHP_SELF'].'">
<input type="radio" name="response" value="f">Follow Move
<input type="radio" name="response" value="s">Sit In<br>
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="submit_check" value="1"/>
</form>
';
  }
else
{
	echo $_POST['response'];
}

}

?>

Do that.

Link to comment
Share on other sites

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.