Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call form submit action from php function on same page

I'm working on a simple web application. In order to reduce the number of files, I want to put (php) code for a form submit function into the same page as the form. Something like this:

<body>
   <form id = "rsvp-status-form" action = "rsvpsubmit" method = "post">
     <input type="radio" name="rsvp-radio" value="yes"/> Yes<br/>
     <input type="radio" name="ravp-radio" value="no" checked/> No<br/>
     <input type="radio" name="rsvp-radio" value="notsure"/> Not Sure<br/>
     <input type="submit" value="submit"/>
   </form>
 </body>

<?php 
  function rsvpsubmit() {
    // do stuff here
  }

What is the proper way to call the submit function?

like image 753
starsinmypockets Avatar asked May 19 '11 14:05

starsinmypockets


People also ask

How do you display submitted data on the same page as the form in PHP?

In order to stay on the same page on submit you can leave action empty ( action="" ) into the form tag, or leave it out altogether. For the message, create a variable ( $message = "Success! You entered: ". $input;" ) and then echo the variable at the place in the page where you want the message to appear with <?

Can form have two actions PHP?

No, a form has only one action.

How do I send a form to another page?

If you want to redirect to another page after form submit html, Then you have to provide/Sign the Other pages path inside HTML Form tag's ACTION Attribute. Which will POST/Send your Form data to that Location and Open/Redirect your Users to That Given Web Page.


2 Answers

After you fix your radio group so they all have the same name:

if (isset($_POST['rsvp-radio'])) {
    rsvpsubmit();
}
like image 193
Quentin Avatar answered Sep 20 '22 22:09

Quentin


<?php
   if (isset($_POST['rsvpsubmit'])) {
     //do something
     rsvpsubmit();
   }
   else {
    //show form
?>
<body>
   <form id="rsvp-status-form" action="?rsvpsubmit" method="post">
      <input type="radio" name="rsvp-radio" value="yes"/> Yes<br/>
      <input type="radio" name="rsvp-radio" value="no" checked/> No<br/>
      <input type="radio" name="rsvp-radio" value="notsure"/> Not Sure<br/>
      <input type="submit" value="submit"/>
   </form>
 </body>
<?php 
  }

  function rsvpsubmit() {
    // do stuff here
  }
?>
like image 39
line-o Avatar answered Sep 22 '22 22:09

line-o