通过 POST 将值提交到多个 PHP 文件

Submit Values via POST to multiple PHP files

本文关键字:PHP 文件 提交 POST 通过      更新时间:2023-09-26

我有一个表格:

<form action="moods.php" method="post" id="geog">
Longitude: <input size="15" id="lonbox" name="lon" type="text" />
Latitude: <input size="15" id="latbox" name="lat" type="text" />
<input type="submit"  />
</form>

我希望使用上面的单个表格同时将纬度和经度的值提交到除情绪之外的多个.php文件中.php。

我该怎么做?? 请提出一些方法..

你可以让一个脚本包含()其他脚本时,为什么要将表单提交到多个页面?

require('script1.php');
require('script2.php');
require('script3.php');

您可以将其提交到包含 cURL 脚本的文件,该脚本将处理多个提交

<form action="multi_submit.php" method="post" id="geog">

multi_submit.php使用 cURL 处理表单提交

如果你真的需要提交多个.php文件的值,而 dqhendricks 给出的 require 选项不能解决它,为什么不使用几个 Ajax 调用呢?每个文件一个。

你可以有这样的东西:

<form onsubmit='sendSeveralPost()'>
... form fields
</form>

和JavaScript函数

function sendSeveralPost() {
    var f1 = document.getElementById('field1');
    var f2 = document.getElementById('field2');
    var x = getXmlHttp();
    var params = 'field1='+f1+'&field2='+f2;
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    x.setRequestHeader("Content-length", params.length);
    x.setRequestHeader("Connection", "close");
    var files = new Array();
    files[0] = 'script1.php';
    files[1] = 'script2.php';
    files[2] = 'script3.php';
    for (i=0;i<files.lenght;i++) {
        var url = files[i];
        x.open("POST", url, true);
        x.onreadystatechange = function() {//Call a function when the state changes.
             if(x.readyState == 4 && x.status == 200) {
                alert(x.responseText);
            }
        }
        x.send(params);
    }
}
function getXmlHttp() {
    var xmlHttp;
    try {    // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    } catch (e) {
        try {     // Internet Explorer 6.0+
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e){
            try {   // Internet Explorer 5.5
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}

有关命令的进一步说明可以在文章 http://www.openjs.com/articles/ajax_xmlhttp_using_post.php 中找到,我从中获得了此示例的灵感。

希望这有帮助。

纳马斯特!