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
https://forums.phpfreaks.com/topic/81591-processing-form-data-within-functions/
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.

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.

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.