简体   繁体   中英

PHP send form data more than one form/pages

I am a PHP beginner.

I want to send a form data to more than one form/pages. Is it possible?

It sends data to use.php . But I want that it also sends data to two more PHP files: lock.php and unlock.php .

How is it possible?

Make your formdata go to one script, and simply include to the other scripts and they'll have access to the $_POST variable as well.

I use this a lot myself. I have a script where everything runs through the index.php file, but functions are stored in different php files depending on what they're doing. My index.php includes all the php files I need, and inside these php files I have scripting like this:

index.php:

<?php
    include('pagename.php');
    include('otherpage.php');
    echo $return;  //output from previous pages
?>

and pagename.php:

<?php
    if( $_GET['page'] != 'pagename' )
    {
        return ('');
    }

    if( isset($_POST['var']) )
    {
        // some code
    }

You can use Ajax on a client side. I recommend Jquery because it is very easy to start with, or you can use CURL on server side, but it is much more complicated, you can find a bunch of tutorials, just google: sending post data with curl.

Now Jquery Ajax approach:

Lets say your form has an ID of myForm:

make a selector:

    $(document).ready(function () {
        $("myForm").submit(function (e) {
            e.preventDefault(); //prevent default form submit
    var url1 = 'your path to url1';
    var url2 = 'your path to url2';
    var url3 = 'your path to url3';

   sendAjax(data,url1);
   sendAjax(data,url2);
   sendAjax(data,url3);
//do the regular submit
$(this).submit();
});

    function sendAjax(data,url){

     $.ajax({
                    url: url,
                    type:'POST',
                    data: data,
                    success: function (data) {
                      //here you do all the return functionality
                    },
                    cache: false
                });
            });

    }

What have we done here:

prevented default sending of form, made X ajax requests, and send the form normally.

We have made a function for simple ajax handeling just to make our code cleaner.

The problem with this method is that you must make form checking in javascript before you start sending.

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