Hello. Such question: There is a form to send a message:

<form name="message" method="" action=""> <p><textarea name="message" rows="5" cols="45"> </textarea></p> <p><input type="submit" value="Отправить" /></p> </form> 

but I don't want to create a separate php file to process it. I want to have one functions.php file, where I describe all the functions. and when sending, the desired function send_message () was called. how to do it, tell me, please.

  • or describe this function in the same file and call $ _SERVER ['PHP_SELF']? But still, I would like to keep all functions in one place - tfiwsrets

3 answers 3

 <form name="message" method="post" action="functions.php"> ... <input type="submit" value="Отправить" name="submit_form1" /> </form> functions.php: <? if(isset($_POST['submit_form1'])) { ... } ?> 

Do you mean it?

  • and if in functions.php will be send_message (), it will look like this: send_message () {if (isset ($ _ POST ['submit_form1'])) {...}}? and the function will be called, yes? - tfiwsrets
  • No, it would rather look like function send_message () {...} if (isset ($ _ POST ['submit_form1'])) {send_message ();} And the function itself will not be called. - ling
  • fine! Thank you very much! - tfiwsrets

It is better to send the form to the page you need, and when you receive it, send it to the function check. For example, the code would look something like this.
File.php

 <?php if(isset($_POST['button'])) { send_message($_POST); } ?> <form name="message" method="" action=""> <p><textarea name="message" rows="5" cols="45"> </textarea></p> <p><input name="button" type="submit" value="Отправить" /></p> </form> 

If this option does not fit then JS in your hands.

 <form name="message" method="" action=""> <p><textarea name="message" rows="5" cols="45"> </textarea></p> <p><input type="submit" onclick="send_message()" value="Отправить" /></p> </form> 

On submit, we hang up the JS call onclick = "send_message ()" and send it to JS in the functions.php file and return the response

    usually done like this:

     <form name="message" method="" action=""> *<input type="hidden" name="action" value="send_message"/>* <p><textarea name="message" rows="5" cols="45"> </textarea></p> <p><input type="submit" value="Отправить" /></p> </form> 

    Php

     switch( @$_REQUEST['action'] ){ case 'send_message': ..... break; case 'send_other_message': .... break; ..... }