我有一个包含两个字段(姓名和初始名称)的php表单,提交后需要在外部网站上打开一个链接,表单数据构成查询字符串的一部分。
如果用户输入John Doe,则当前表单会删除查询字符串的第一个和最后一个部分并输入输入名称。这会产生http://example.com/?initial=John&surname=Doe
而不是所需的http://example.com/?method=boolean&query=john+doe&othermeta&etc
目前的表格:
<form class="form" action="http://example.com?method=boolean&query=<?php $_POST['initial']; ?>+<?php $_POST['surname']; ?>&othermeta&etc" method="get">
<input type="text" name="initial" value="" required />
<input type="text" name="surname" value="" required />
<input type="submit" value="submit" />
</form>
答案 0 :(得分:0)
我不知道你为什么要这样做,但这个答案可能很愚蠢,但这会起作用
<form class="form" id="form">
<input type="text" name="initial" id="initial" value="" required />
<input type="text" name="surname" id="surname" value="" required />
<input type="button" value="submit" onclick="onSubmitForm()">
</form>
<script>
function onSubmitForm() {
var mainText = "http://example.com?method=boolean&othermeta&etc";
var initial = document.getElementById('initial').value;
var surname = document.getElementById('surname').value;
window.location.href = mainText+'&query='+initial+"+"+surname;
}
</script>
答案 1 :(得分:0)
这是我的建议 - 无需javascript或jQuery。
但是如果你想在提交时将initial
和surname
值连接成query
变量,那么我推荐@Gustiawan Ouwawi提供的方法。
目前还不清楚,您希望如何使用$_POST
变量,因此我将它们作为默认输入值实现。
<form class="form" action="http://example.com/" method="get">
<input type="hidden" name="method" value="boolean" />
<input type="text" name="initial" value="<?php echo isset($_POST['initial']) ? $_POST['initial'] : ''; ?>" required />
<input type="text" name="surname" value="<?php echo isset($_POST['surname']) ? $_POST['surname'] : ''; ?>" required />
<input type="hidden" name="othermeta" value="" />
<input type="hidden" name="etc" value="" />
<input type="submit" value="submit" />
</form>