【问题标题】:Append variables to url after form submit表单提交后将变量附加到 url
【发布时间】:2012-06-13 22:23:17
【问题描述】:

我有这个登陆页面,我需要在提交后预先填充表单值。基本上最后我必须让网址看起来像这样......

http://example.com/r.php?sid=xx&pub=xxxxx&c1=&c2=&c3=&append=1&firstname=Test&lastname=Smith&address=2211+Commerce+St.&city=Dallas&state=TX&zipcode=75080&email=test@test.com

我目前拥有的表单是......

<form name="regForm" method="get" action="http://example.com/r.php?sid=xx&pub=xxxx&c1=&c2=&c3=&append=1">
    <input id="firstname" class="text" type="text" name="firstname"/><br>
    <input id="lastname" class="text" type="text" name="lastname" /><br>
    <input id="email" class="text" type="text"  name="email" /><br>
    <input id="address" class="text" type="text" /><br>
    <input id="city" class="text" type="text"/><br>
    <input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
    <input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>

提交表单后如何创建上面的 URL?我整天都在为此绞尽脑汁,无法弄清楚,我觉得我很接近。

感谢您的帮助!

【问题讨论】:

  • x*x.com?更好地使用 example.com
  • 我现在正在关注。你已经设置了method="get",所以它应该按原样工作。提交时会得到什么网址
  • 或者你的意思是你想像这样使用value 属性填充值? &lt;input id="firstname" class="text" type="text" name="firstname" value="Test"/&gt;
  • @BenLee 我得到了这个“example.com/r.php?firstname=D&lastname=M&email=dave%40eternalnyc.com&state=&zipcode=89078&look=Single+Family+Home”

标签: php javascript forms


【解决方案1】:

将额外的参数包含为隐藏的表单字段,而不是内联查询参数:

<form name="regForm" method="get" action="http://example.com/r.php">
    <input type="hidden" name="sid" value="xx" />
    <input type="hidden" name="pub" value="xxxx" />
    <input type="hidden" name="c1" value="" />
    <input type="hidden" name="c2" value="" />
    <input type="hidden" name="c3" value="" />
    <input type="hidden" name="append" value="1" />

    <input id="firstname" class="text" type="text" name="firstname"/><br>
    <input id="lastname" class="text" type="text" name="lastname" /><br>
    <input id="email" class="text" type="text"  name="email" /><br>
    <input id="address" class="text" type="text" /><br>
    <input id="city" class="text" type="text"/><br>
    <input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
    <input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>

【讨论】:

  • 你是一个救生员......非常感谢
【解决方案2】:

问题是输入字段获取变量导致您的 url 获取变量被截断,将所有 url 参数作为隐藏值。

<input id="pub" type="hidden" name="pub" value=""/>
<input id="sid" type="hidden" name="sid" value=""/>

【讨论】:

  • 这个问题是需要定义 sid 和 pub 变量(它是附属链接)
【解决方案3】:

我假设您喜欢它生成的 URL,只是您缺少 sid、pub、c1、c2、c3 和 append 变量。如果是这样,只需像这样进行隐藏输入:

<form id="regForm" ...>
  <input id="sid" name="sid" value="" type="hidden" />
  <input id="pub" name="pub" value="" type="hidden" />
  ...
</form>

如果您在创建表单时知道这些值,那么您可以在服务器端进行。如果你不这样做,那么假设你使用的是 jQuery,你将不得不这样做:

$(function() {
  $('#regForm').submit(function () {
    $('#sid').val('myNewSidValue');
    $('#pub').val('myNewPubValue');
    ...
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2021-10-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多