Php 通过post方法发送数据时保存数据

Php 通过post方法发送数据时保存数据,php,ajax,Php,Ajax,我有一个表格,例如: <form method="post" action="some external URL"> <input type="hidden" id="id" name="session_id" /> <input type="hidden" id="val1" name="val1" /> <input type="hidden" id="val2" name="val2" /> <inpu

我有一个表格,例如:

<form method="post" action="some external URL">
    <input type="hidden" id="id" name="session_id" />
    <input type="hidden" id="val1" name="val1"  />
    <input type="hidden" id="val2" name="val2" />
    <input type="submit" value="Send" />
</form>

在将数据发送到“某个外部url”之前,我需要将数据保存到mySQL中——假设我的“保存到数据库”脚本位于“脚本/保存”url上。在通过此表单发送数据之前,我如何在那里传输数据

编辑

如果我的表单只有在“脚本/保存”给出肯定响应时才会发送到“某个外部url”,那就太好了。
你也能帮我吗?

数据库访问是在服务器上处理的,这就是你的表单传输到的服务器。因此,要做到这一点,请将表单重新路由到服务器上的不同脚本;该脚本将写入数据库,然后调用“某个外部URL”

在表单的onsubmit事件期间,需要使用ajax将数据发送到url。以下代码可能适合您

$("form").submit(function(){        
    $(this).ajaxSubmit({url: 'script/save', type: 'post'});
});

不要将表单发布到外部URL,而是将其发布到内部脚本:

<?php
// Process your $_POST here and get all the stuff you need to process your SQL statements.

// Then, POST to the external URL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "your external URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
?>

他们只需在submit上使用ajax调用,然后在ajax调用完成后提交表单

<html>
<head>
<script type="text/javascript">
    function doSomething()
    {
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open('GET', 'yourscript.php, true);
        xmlHttp.onreadystatechange = function(){
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
                var response = xmlHttp.responseText;
                // response is what you return from the script
                if(response == 1){
                    alert('works');
                    form[0].submit;
                }else{
                    alert('fails');
                }
            }else{
                alert('fails');
            }
        }
        xmlHttp.send(null);
    }
</script>
</head>
<body>
    <form action="external-url" method="post" onsubmit="doSomething();">
        <input type="text" name="blah" id="blah">
        <input type="submit" value="save">
    </form>
</body>
</html>

函数doSomething()
{
xmlHttp=新的XMLHttpRequest();
open('GET','yourscript.php,true);
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
var response=xmlHttp.responseText;
//响应是您从脚本返回的内容
如果(响应==1){
警报(“工作”);
表单[0]。提交;
}否则{
警报(“失败”);
}
}否则{
警报(“失败”);
}
}
xmlHttp.send(空);
}

您可能希望编写一个内部脚本,接受post数据,为您的DB处理它,然后将post卷曲到外部URL。是的-但是一些ajax解决方案不是更好吗?ajax可能更好,但您可能会遇到使用相同来源策略的问题。