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?
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 <?
No, a form has only one action.
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.
After you fix your radio group so they all have the same name:
if (isset($_POST['rsvp-radio'])) {
rsvpsubmit();
}
<?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
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With