在表单提交后将变量附加到url

时间:2012-06-13 22:23:17

标签: php javascript forms

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

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?我整天都在绞尽脑汁想不起来,我觉得我很近。

感谢您的帮助!

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:1)

问题是输入字段get变量导致你的url get变量被截断,把你所有的url参数都作为隐藏值。

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

答案 2 :(得分:0)

我假设你喜欢它生成的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');
    ...
  });
});
相关问题