简体   繁体   中英

Combine form get variable into single variable

I would like to combine multiple get variable into single variable when the user submits data in form.

For an example, if the user gives input values like "First Name : Dave, Last Name : Smith, Family Name : Dave+Villa" then the form gives get variable as " ?fName=Dave&lName=Smith&famName=Dave+Villa "

But i need output as " ?family=Dave-Smith-Dave+Villa " in a single variable.

is that possible?

Also You can use javascript.

Here it is my code,

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

We can use JavaScript to change a hidden variable in the form with name=family with the concatenated FirstName , LastName and Family .

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

Then we can submit it.

Note: Here you'll find the URL as family=Dave-Smith-Dave%2BVilla Don't worry! Use

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

and you'll see nice Dave-Smith-Dave+Villa there.

The reason of this %2B is Character + is converted to %2B in HTTP Post

Also you can find a list of URL encodings in https://www.w3schools.com/tags/ref_urlencode.asp

Use a hidden field input and name it name="family" and remove name from all other inputs., then we can set the value of family before we submit the form using 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" 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM