1

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.)

Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • Have you tried javascript:void(0). http://stackoverflow.com/questions/1291942/what-does-javascriptvoid0-mean. BTW why would you want to stay on the same site. Anyway you could also try to use post and then use the same page in action or use $_SERVER['PHP_SELF'] to stay on the php page with a script. If not submitted show form and when submitted show what every you like on the same page. – Mr. Radical Feb 17 '13 at 00:51
  • Doug Smith is you question solved? – Mr. Radical Feb 20 '13 at 16:38

3 Answers3

3

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.

DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
1

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.

Gawdl3y
  • 230
  • 1
  • 3
  • 15
0

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

Community
  • 1
  • 1
Mr. Radical
  • 1,847
  • 1
  • 19
  • 29
  • So you are telling me that this works without any navigation at all? I wasn't aware that PHP executed or is even transmitted to the clients system? It was my understanding that PHP is executed on the server and then sends HTML to the client. Reference http://w3schools.com/php/default.asp – DotNetRussell Feb 17 '13 at 01:19
  • "What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is free to download and use" http://w3schools.com/php/php_intro.asp – DotNetRussell Feb 17 '13 at 01:20
  • @AMR thank you for your comments. The form executes on the server after the submit button is pressed. Then the next loop is executed. However for the visitor to the page the location in the browser remains the same. – Mr. Radical Feb 17 '13 at 01:30
  • I know. The reason I mentioned this is because the OP said he wanted it done without navigation. – DotNetRussell Feb 17 '13 at 01:31
  • @AMR this is how I understood the question. I think that OP doesn't want to be redirect to the process.php page. So, on pressing submit he doesn't want to leave the page. I suspect that the form action current has process.php, the result will be that when he presses submit the browser will change location to process.php. – Mr. Radical Feb 17 '13 at 01:33
  • 1
    The change in browser location might be considered as navigation to another page. – Mr. Radical Feb 17 '13 at 01:39