简体   繁体   中英

How can I put an Html <form> tag inside a PHP function, which would redirect me to another .php file?

I have been searching for hours on the web and I just can't seem to find an answer.

What I want to do is put a <form> tag inside a php function which would redirect me to another x.php file if a certain criteria is met.

Sounds simple enough:

function myFunction() {
    if ( Some Condition Here ) {
        ?>
             <form action="x.php">
                 <button>submit</button>
             </form>
        <?php
    }
}

Obviously the "redirect" won't happen until the form is submitted. Redirecting is done in response to an HTTP request not to "having a form".

You don't use form to redirect , you use a header

function redirect(url){
  header("Location: ".url);
  die();
}

so now

if(some_condition){
  redirect("anotherFile.php");
}

For redirect use following code:

header('Location: page.php');
exit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM