执行PHP而不重定向到动作链接文件?

If I have a submit button that will execute a PHP script, can I execute it without actually going to that page, i.e.: process.php? I'm used to JavaScript being able to do this.

(For the record, I'm talking about without AJAX, without redirecting once on that other page, and no, I'm not very hopeful.)

The reason you are able to do this with JavaScript is because JavaScript can execute within the browser on the client side. PHP on the other hand ALWAYS executes on the server and returns HTML to the client.In short, no you can't.

One way to do it without AJAX is to submit the form to an invisible iframe, like so:

<form action="blah.php" target="theReallyCoolIframe">...</form>
<iframe name="theReallyCoolIframe></iframe>

Note that this isn't really a very graceful solution; AJAX is a much better solution overall, and isn't that difficult - you should look in to jQuery and its AJAX APIs.

Try this, if you want to use a PHP only solution to your problem.

<?php


function submit_form(){
    $host = "localhost";
    $user = "user";
    $password = "password";
    $database = "database";   

    $firstname  = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING); 
    $lastname   = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING); 
    $email  = filter_var(filter_var($_POST['email'],FILTER_SANITIZE_EMAIL),FILTER_VALIDATE_EMAIL);


    // open connection to database
    $link = mysqli_connect($host, $user, $password, $database);
        IF (!$link){
            echo ("Unable to connect to database!");
        }
        ELSE {
           //INSERT VALUES INTO DATABASE
           $query = "INSERT INTO users (firstname,lastname,email) 
VALUES('".$firstname."', '".$lastname."', '".$email."'";
           return mysqli_query($link,$query);

        }
//close connection to database
        mysqli_close($link);

    }


$form = <<<EODuserform
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Form</title>
    </head>
        <form action="{$_SERVER['PHP_SELF']}" method="POST" name="userform">
            <label for='first'>First Name:</label></br>
            <input type="text" name="firstname" id="first" maxlength="25" tabindex='1' VALUE="firstname" /></br>
            <label for='first'>Last Name:</label></br>
            <input type="text" name="lastname" id='lastname' maxlength="25" tabindex='2' VALUE="lastname" /></br>
            <label for='email'>E-mail:</label></br>
            <input type="text" name="email" id='email' maxlength="100" tabindex='3' VALUE="email" /></br>
            <input type="submit" class='button' name="submit" value="submit" tabindex='4' />
            </form>
    </body>
</html>
EODuserform;


IF(!IsSet($_POST['submit'])){ // Check if form is not send, if not display empty form.

    echo $form;
}

ELSE{
// in the case you want to send something to the database use 
submit_form();
echo ('Thanks for submitting your form');
}

?>

It is a very basic script you can find a similar script on this page: Form not saving to database