0
 <form method="get" action=..... >

                      Food <input name="food" type="text"  size="1" />
                  Pizza <input name="pizza" type="text"  size="2" />
                  Drink <input name="drink" type="text" size="2"/>           

Now I want to pass a third variable which is a concatenation of food+pizza+drink in the URL I don't want to create a new <input name="total" type="text" size="2"/>

Is this possible?

EDIT:

I want to do it before submitting the values.

I will have a url like
 http://www.abc.com/&food=tasty&pizza=cheeze&drink=pepsi
I want the url to be
 http://www.abc.com/&food=tasty&pizza=cheeze&drink=pepsi&total=tastypizzacheezepepsi
3
  • How would you differentiate between someone ordering 11 food and 2 pizzas and 1 drink v.s. someone ordering 1 food and 1 pizza and 21 drinks? Both would be 1121. Commented May 20, 2011 at 16:15
  • @MarcB i assume the OP meant adding, not concatenate Commented May 20, 2011 at 16:18
  • re: your update: why do you want to add a get variable? Commented May 20, 2011 at 16:19

4 Answers 4

4

Why don't just do it on the server side?

$allTogether = $_GET["food"] . $_GET["pizza"] . $_GET["drink"];
Sign up to request clarification or add additional context in comments.

Comments

3

Well once it is submitted just concatenate (after validation)

$total = $_GET['food'] + $_GET['pizza'] + $_GET['drink'];
         //assuming the _GETs were numbers, otherwise use `.` to concatenate

Or if you don't want to do that, you would have to do some JavaScript magic on the client side (but that is not always trustworthy)

6 Comments

You meant to use . instead of + to concatenate the string values, perhaps?
@Phoenix, depends on what the information was. the OP used +
Ah I see what you mean. It did not occur to me that the values of those inputs could be numeric.
The example has text for those values.
@Tomalak, that was after the OP updated the Q, and i added a comment for that case
|
2

Assuming that you don't like the answers above using PHP to concatenate, you can use Javascript to do that.

<script type="text/javascript">
function addTotal(food, pizza, drink) {
   document.getElementById("total").value = food + drink + pizza;
}
</script>

<form method="get" name="fname" action="" onSubmit="addTotal(document.fname[0].value,document.fname[1].value,document.fname[2].value)" >
Food <input name="food" type="text"  size="1" />
Pizza <input name="pizza" type="text"  size="2" />
Drink <input name="drink" type="text" size="2"/> 
<input type="hidden" name="total" id="total" value="" />
</form>

And in your PHP file, you now should get

$_GET['food'];
$_GET['pizza'];
$_GET['drink'];
$_GET['total'];

Comments

1

This should do the trick

$sep = ""; //if you want a space in between each just change this to a space.
$concat = implode($sep, array($_GET['food'], $_GET['pizza'], $_GET['drink']));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.