简体   繁体   English

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

[英]Saving data when sending it via post method

I have a form, example: 我有一个表格,例如:

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

I need to save this data into mySQL before sending it into "some external url" - let's say that my "saving to database" script is on "script/save" url. 我需要先将该数据保存到mySQL中,然后再将其发送到“某个外部URL”中-假设我的“保存到数据库”脚本位于“脚本/保存” URL上。 How can I transfer my data there before sending it throught this form? 在通过此表格发送数据之前,我该如何在那传输数据?

EDIT 编辑

It would be nice, when my form will only be sent to "some external url" when the "script/save" gives POSITIVE RESPONSE. 当我的表单仅在“脚本/保存”给出积极响应的情况下发送到“某个外部URL”时,会很好。 Could you help me with that too? 您能帮我吗?

Database access is handled at the server, which is what your form is transferring to. 数据库访问是在服务器上处理的,这是您的表单要转移到的服务器。 Therefore, in order to do this, re-route your form to a different script on the server; 因此,为了做到这一点,请将您的表单重新路由到服务器上的其他脚本。 this script will write to the database and then call "some external URL". 该脚本将写入数据库,然后调用“某些外部URL”。

You would need to use ajax to send the data to your url during the onsubmit event of your form. 您需要在表单的onsubmit事件期间使用ajax将数据发送到您的url。 The following code might work for you. 以下代码可能对您有用。

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

Instead of posting your form to the external URL, post it to an internal script: 不要将您的表单发布到外部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);
?>

Hey just use an ajax call onsubmit then submit the form when the ajax call is done 嘿,只需使用ajax调用onsubmit,然后在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM