我正在處理一個django網絡應用程序,並且有一個html表單,當表單提交時我需要做兩件事情:在應用程序數據庫中創建一個記錄併發布一些收集到另一個網站(例如支付網站)的價值。製作django html表單在提交時執行2個操作
我遇到的問題是讓窗體同時做兩件事。我知道一個HTML表單只能有一個動作,並且我已經在StackOverflow上閱讀了一些關於使用JavaScript獲取表單以執行2個或更多操作的帖子,但是迄今爲止我嘗試過的所有內容都不適用於這種情況。他們似乎只有一個行動才能工作。
這是我的Django的模板看起來像現在:
{% extends "some other template" %}
{% block content %}
<div>
...
<form id=form1" name="trans_form" method="POST" >
...
<!--DATA TO POST TO PAYMENT SITE-->
<input type="hidden" name="transaction_id" value="some value" />
<input type="hidden" name="transaction_amount" value="some value"/>
<input type="hidden" name="customer_id" value="some value" />
<input type="hidden" name="customer_name" value="some value" />
<!--DATA TO POST TO PAYMENT SITE-->
...
<!--DATA TO POST TO APP DATABASE-->
<input type="hidden" name="user" value="{{ user.id }}">
<input type="hidden" name="type" value="CC">
<input type="hidden" name="ref_no" value="{{ ref_no }}">
Amount: <input type="text" name="amount" id="id_amount" required />
Ref ##: <span>{{ ref_no }}</span>
Date: <span>{{ cur_date|date:'d/m/Y' }}</span>
<a href="#" id="pay-btn" class="button" onclick="submitForm();">Submit</a>
<!--DATA TO POST TO APP DATABASE-->
...
</form>
...
</div>
{% endblock %}
{% block script %}
<script>
function submitForm()
{
createRecord(document.forms["trans_form"]);
sendToPay(document.forms["trans_form"]);
}
function sendToPay(f)
{
f.action= "www.paymentsite.com";
f.target = null;
f.onsubmit = null;
f.submit();
}
function createRecord(f)
{
f.action = "url to view that creates the record in database";
f.target = "_blank";
f.onsubmit = null;
f.submit();
}
</script>
{% endblock %}
你覺得呢?我是否想要實現不可能的目標?如果不是,請指出我的方向。謝謝。
'f.action ='也許應該包括'的https:/ /'在你的'sendToPay'函數的'www'之前。 –