1

I’m hoping this is a simple thing I’m missing or not doing properly.

It relates to posting two results from 1 submit button on a form.

Without sending the mass of code, here’s the crux: (It’s a series of submit buttons in a table)

<td><input type=\"hidden\" name=\"query\" value=\"$query1\"><input type=\"submit\"       name=\"Submitano\" value='$aa'></td>
<td><input type=\"hidden\" name=\"query\" value=\"$query2\"><input type=\"submit\"  name=\"Submitano\" value='$ab'></td>
<td><input type=\"hidden\" name=\"query\" value=\"$query3\"><input type=\"submit\" name=\"Submitano\" value='$ac'></td>

Then on the other results page I use:

$s1=$_POST['Submitano'].$_POST['query'];

The problem is when I click on the relevant submit button it pulls through the correct POST [‘Submitano’] that was chosen (i.e. $aa, or $ac) but it ALWAYS pulls through the last query - $query3 instead of the query that is attached to the submit. (i.e. $aa should pull through $query1, but it pulls through $query3)

I suspect it has something to do with when I click submit, it is submitting the ['Submitano'] and not the ['query'], so the next page is simply picking up the last query that was typed. If so, can anyone suggest how I can post two bits of data (one hidden, one seen) via 1 submit?

Hope this makes sense.

Thanks

Paul


Thanks for all the comments. This is what I’m trying to do, but maybe I should do it in an entirely different way?

Screen 1 – 3 – User chooses a variable on each page that is passed to Screen 4.

Screen 4 – The aforementioned 3 variables are read and fed into 25 pre-set Mysql queries and run (i.e. Query 1, Query 2, etc), but each query has a slightly different element, thus resulting in 25 different answers.

The results of these 25 queries are stored in 25 variables $aa, $ab, etc.

A form is set up that displays the results of these 25 queries, with each answer being displayed in a submit button. (i.e. where I have coded ‘Value=$aa’, it’s so the user can see the answer to that query, i.e. ‘73’ and then click on it).

(The user basically needs to see 25 different answers based on their choices in the first 3 pages, and then choose the answer they want to know more about.)

Page 5 – This page needs to know what button the user clicked, but more importantly which query they chose, but my problem is that it only feeds through the value, and not the preceding query that generated that value.

Regarding naming each one differently, I tried that but it’s how to pick it up on page 5 that’s the problem. I take it I would need 25 preset $_POST’s? The problem with that is it doesn’t like the other 24 unused $_POST’s. I even tried putting them in an elseif loop, but no joy.

However, does and answer lie in this solution somehow?

Also, a couple of people suggested an array, but as I’m not too hot on arrays I’m not sure how they would solve the problem?

If arrays are the answer, any more info on the set up or more specifically how I pull out the correct one on page 5 would be great?

Thanks again.

Paul

1
  • Don't use such abstract names and values for your forms. It's quite hard to understand what it's about this way. Commented Aug 13, 2012 at 23:17

4 Answers 4

3

They shouldn't have the same name. As they are in the same form, the last value overwrites the others.

If you only have these six items, you might also wrapp them in three different forms for this to work.

Sign up to request clarification or add additional context in comments.

3 Comments

@asprin: Why did you edit my answer? Was the thing about the forms wrong?
By accepting it, it was deleted. Thank you for the grammar-corrections though. My english isn't the very best unfortunately.
Thanks guys. Based on other comments I have added more details. (Added as an answer as the comment box too small)
0

Names are supposed to be unique. Since all of your hidden inputs are named 'query', only the last is submitted.

1 Comment

Thanks. Based on other comments I have added more details. (Added as an answer as the comment box too small)
0

Your form items have the same name. That will mean that their values effectively "overwrite" each other. Depending on the browser either the first or last value will be POSTed to your page.

To fix it you can either:

1) Give them different names.

<input type=\"submit\" name=\"Submitano1\" value='$aa'>
<input type=\"submit\" name=\"Submitano2\" value='$aa'>
<input type=\"submit\" name=\"Submitano3\" value='$aa'>

2) Give them names as an array.

<input type=\"submit\" name=\"Submitano[]\" value='$aa'>
<input type=\"submit\" name=\"Submitano[]\" value='$aa'>
<input type=\"submit\" name=\"Submitano[]\" value='$aa'>

The second option will allow you to access $_POST['Submitano'][0] (as well as 1 and 2) which will put all of the Submitano items into an array. This second method will be more helpful if you are using a large amount of these items.

More information as to exactly what you're doing would help give more specific advice if needed.

1 Comment

Thanks. Based on other comments I have added more details. (Added as an answer as the comment box too small)
0

You should name your elements in the form of an array:

<input type="text" name="query[]" value="whatever" />

Same is the case for the submit button than on the next page use the same index.

Edit: If your requirement is to collect data from multiple screens, I suggest you use $_SESSION variables. http://php.net/manual/en/book.session.php

2 Comments

Thanks. Based on other comments I have added more details. (Added as an answer as the comment box too small)
Thanks Muhammad Between that page and some stuff a guy suggested on another forum about arrays I've managed to get it to work. It's much appreciated! Paul

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.