【问题标题】:Combine form get variable into single variable将表单获取变量合并为单个变量
【发布时间】:2017-05-13 12:20:49
【问题描述】:

当用户在表单中提交数据时,我想将多个get变量合并为一个变量。

例如,如果用户输入“名字:Dave,姓氏:Smith,姓氏:Dave+Villa”之类的输入值 然后表单将 get 变量设为“?fName=Dave&lName=Smith&famName=Dave+Villa

但我需要在单个变量中输出为“?family=Dave-Smith-Dave+Villa”。

这可能吗?

您也可以使用javascript。

这是我的代码,

<html>
     <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
       <h4>Family Category</h4>
      <form name="userForm" action="" method="get">
      <table>
      <tr><td>First Name :</td><td><input type="text" name="fName"/></td><tr>
      <tr><td>Last Name :</td><td><input type="text" name="lName"/></td><tr>
      <tr><td>Family Name:</td><td><input type="text" name="famName"/></td><tr>
      <tr><td></td><td><input type="submit" value="Add Category"/></td><tr>
      </table>
      </form>
    </body>
  </html>

【问题讨论】:

  • 在你的 php 中使用 implode
  • 你使用javascript吗?
  • 是的,你可以使用 javascript。但我不知道如何使用它..
  • 您需要-+ 介于两者之间还是DaveSmithDave%2BVilla 可以?
  • 是的,我需要一个 - 和 + 在字符串之间

标签: javascript php html forms variables


【解决方案1】:

使用隐藏字段输入并将其命名为name="family"并从所有其他输入中删除name。然后我们可以在使用Javascript提交表单之前设置family的值。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h4>Family Category</h4>
<form name="userForm" action="" method="get" onsubmit="setGetUrl()">
    <table>
        <tr><td>First Name :</td><td><input id="name" type="text" />
            <input type="text" name="family" id="family" hidden></td><tr>
        <tr><td>Last Name :</td><td><input id="lname" type="text" ></td><tr>
        <tr><td>Family Name:</td><td><input id="famName" type="text"/></td><tr>
        <tr><td></td><td><input type="submit" value="Add Category"/></td><tr>
    </table>
</form>
</body>
<script>
    function getValue(id) {
        return document.getElementById(id).value;
    }

    function setGetUrl() {
        var name = getValue('name');
        var lname = getValue('lname');
        var famName = getValue('famName');
        document.getElementById('family').value = name + '-' + lname + '-' + famName;
    }

</script>
</body>
</html>

【讨论】:

  • 哈哈哈!同一时间给出同样的答案。 :D
  • @DineshDiNu 很高兴我能帮上忙。
【解决方案2】:

我们可以使用 JavaScript 来更改 name=family 表单中的 hidden 变量,并连接 FirstNameLastNameFamily

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<script>
    function X()
    {
        x = document.getElementById("a").value + "-" + document.getElementById("b").value + "-" + document.getElementById("c").value;
        document.getElementById("d").value = x;
    }
</script>

<h4>Family Category</h4>
<form name="userForm" action="dd.php" method="get" onsubmit="return X()">
<table>
<tr><td>First Name :</td><td><input type="text" id="a"/></td><tr>
<tr><td>Last Name :</td><td><input type="text" id="b"/></td><tr>
<tr><td>Family Name:</td><td><input type="text" id="c"/></td><tr>
<tr><td></td><td><input type="hidden" id="d" name="family"/></td><tr>
<tr><td></td><td><input type="submit" value="Add Category"/></td><tr>
</table>
</form>
</body>
</html>

那我们就可以提交了。

注意:在这里,您会发现 URL 为 family=Dave-Smith-Dave%2BVilla 不用担心!使用

<?php
    echo $_GET["family"];
?>

你会在那里看到很好的Dave-Smith-Dave+Villa

这个 %2B 的原因是Character + is converted to %2B in HTTP Post

您还可以在https://www.w3schools.com/tags/ref_urlencode.asp 中找到 URL 编码列表

【讨论】:

  • @DineshDiNu,julekgwa 和我们同时做同样的事情,这是我 SO 日子里的巧合! :D
猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多