473,738 Members | 3,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Retrieving the multiple values set by a form

Hi Everyone,

I am passing a form to a php script for further processing.
I am able to retrieve the last value set for that given form variable
using
$variable=$_REQ UEST['form_variable'];
My question is, what is the Php way of retrieving all the values passed
for the same form variable?

For example, if the php script is called with a syntax like

http://xxxx/get_variables.php?form_v...iable=variable
, how do I iterate through all the values that form_variable has been
set to?

Thanks and regards,
Girish

Sep 27 '06 #1
10 3695
Girish wrote:
>
http://xxxx/get_variables.php?form_v...iable=variable
Above request will be read by php like this:
$_REQUEST["form_varia ble"] = "value1";
$_REQUEST["form_varia ble"] = "value2";
$_REQUEST["form_varia ble"] = "value3";

so only the last value will be the result. Because the first will be
replaced with second value and second value will be replace with the
last value. I think its better you use different variable name.

http://xxxx/get_variables.php?form_v...able3=variable
You can get all the values of above variables.

---
http://www.theukmap.com
http://www.theaussiemap.com

Sep 27 '06 #2

"Girish" <gi************ @gmail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hi Everyone,

I am passing a form to a php script for further processing.
I am able to retrieve the last value set for that given form variable
using
$variable=$_REQ UEST['form_variable'];
My question is, what is the Php way of retrieving all the values passed
for the same form variable?

For example, if the php script is called with a syntax like

http://xxxx/get_variables.php?form_v...le=value2&form
_variable=varia ble
>

, how do I iterate through all the values that form_variable has been
set to?

Thanks and regards,
Girish
if register_global s is ON then the variables are already set by php (which,
BTW, can cause all kinds of unexpected grief when html tag names end up as
php vars).

You can test the results by doing this:
<?php
if (!empty($_GET)) { # can't use !empty($_REQUES T) as it always has
something
echo "before foreach a=".$a.",b=".$b .",c=".$c."< br />"; // if
register_global s is on these are already set

# if register_global s is OFF this will set all request vars
foreach ($_REQUEST as $key =$value){
$$key = $value;
}
echo "after foreach a=".$a.",b=".$b .",c=".$c."< br />";
}
else {

echo <<<FORM
<form method="get" action="{$_SERV ER['PHP_SELF']}">
<input type="text" name="a" />
<input type="text" name="b" />
<input type="text" name="c" />
<input type="submit" />
</form>
FORM;
}
?>
but be aware that $_REQUEST has extra stuff in it.
Sep 27 '06 #3
"Girish" <gi************ @gmail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Hi Everyone,

I am passing a form to a php script for further processing.
I am able to retrieve the last value set for that given form variable
using
$variable=$_REQ UEST['form_variable'];
My question is, what is the Php way of retrieving all the values passed
for the same form variable?

For example, if the php script is called with a syntax like

http://xxxx/get_variables.php?form_v...iable=variable
, how do I iterate through all the values that form_variable has been
set to?

Thanks and regards,
Girish
Hi Girish,

In the example you have given, which equates to a Get, the variables will
indeed overwrite each other with only the last value surviving, as stated by
lorento.

You can however create an array by suffixing the variable name (in the HTML
form) this
<input name='inp[0]' type='hidden' value='first value'>
<input name='inp[1]' type='hidden' value='second value'>
<input name='inp[2]' type='hidden' value='third value'>

once this is received by your PHP script the values may be retrieved :

$myArray = $_REQUEST('ip') ; // note no subscripts required

$myArray['0'] will contain 'first value', $myArray['1'] the second value,
and so on

BTW Register globals should be OFF on any production machine so there is
little point in having it on in a development machine.

HTH

Ron
Sep 27 '06 #4

"Ron Barnett" <ro*@RJBarnett. co.ukwrote in message
news:451a9129.0 @entanet...
BTW Register globals should be OFF on any production machine so there is
little point in having it on in a development machine.

HTH

Ron

Agreed if you have control of the server, however many shared host LAMP
servers are stuck back at PHP4.x (most likely due to cpanel having not
caught up yet), and very often have register_global s on by default.
Of course if those hosts allow .htaccess files you can then turn it off.
Sep 27 '06 #5
Ron Barnett wrote:
In the example you have given, which equates to a Get, the variables will
indeed overwrite each other with only the last value surviving, as stated by
lorento.
Hi Ron and everyone else,

Thanks for your response. Let me expand a bit on my requirement. I have
a dynamically generated form where I am now trying something similiar.
To wit:

<input name='inp[0]' type='checkbox' value='first value'>
<input name='inp[1]' type='checkbox' value='second value'>
<input name='inp[2]' type='checkbox' value='third value'>
..
..
etc

Basically, I present a user with a dynamically generated checklist and
I am trying to see all the values that he has checked.
>
once this is received by your PHP script the values may be retrieved :

$myArray = $_REQUEST('ip') ; // note no subscripts required

$myArray['0'] will contain 'first value', $myArray['1'] the second value,
and so on

Doing as you have suggested, count($myArray) gives the correct number
of values ticked.

But only $myArray['0'] is set!
$myArray['1'], $myArray['2'] , ..., etc are not set!

Not to flame or anything, I AM quite surprised by how arcane processing
a checklist in php is turning out to be. :-)
Thanks again!
Girish

Sep 28 '06 #6
And this is how I have worked around this problem. Admittedly this is
not a very elegant solution, but I present it for the benefit of those
who like me fail to find a better solution and will trawl the archives
in search of this very workaround in the future. :-)

In the main form I have,

<input name='"choice0' "type="checkbox " value='"first value">
<input name='"choice1" type="checkbox" value='"second value">
<input name='"choice2" type="checkbox" value='"third value">
etc.
plus a hidden input field
<input name="displayed choices" type="hidden" value='"3"//equal to the
number displayed.

In the processing script

$choicesDisplay ed = $_REQUEST['displayedchoic es'];
for($i=0; $i< $choicesDisplay ed; $i++)
{
$read = 'choice'.$i;
$setvalue = $_REQUEST[$read];
echo "Value of $read is ".$setvalue ;
echo "<br>\n";
}
Cheers,
Girish

Sep 28 '06 #7
I have to wonder why you're using GET at all for form submission. If
you used post, you could just pass in an array:

<form method="POST" action="somesec ript.php">
<input type="text" name="values[]"/>
<input type="text" name="values[]"/>
</form>

And in the processing:

foreach ($_POST["values"]):

// do something with each value

endforeach;

~A!
lorento wrote:
Girish wrote:

http://xxxx/get_variables.php?form_v...iable=variable

Above request will be read by php like this:
$_REQUEST["form_varia ble"] = "value1";
$_REQUEST["form_varia ble"] = "value2";
$_REQUEST["form_varia ble"] = "value3";

so only the last value will be the result. Because the first will be
replaced with second value and second value will be replace with the
last value. I think its better you use different variable name.

http://xxxx/get_variables.php?form_v...able3=variable
You can get all the values of above variables.

---
http://www.theukmap.com
http://www.theaussiemap.com
Sep 28 '06 #8
Ron Barnett wrote:
"Girish" <gi************ @gmail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
>>Hi Everyone,

I am passing a form to a php script for further processing.
I am able to retrieve the last value set for that given form variable
using
$variable=$_REQ UEST['form_variable'];
My question is, what is the Php way of retrieving all the values passed
for the same form variable?

For example, if the php script is called with a syntax like

http://xxxx/get_variables.php?form_v...iable=variable
, how do I iterate through all the values that form_variable has been
set to?

Thanks and regards,
Girish


Hi Girish,

In the example you have given, which equates to a Get, the variables will
indeed overwrite each other with only the last value surviving, as stated by
lorento.

You can however create an array by suffixing the variable name (in the HTML
form) this
<input name='inp[0]' type='hidden' value='first value'>
<input name='inp[1]' type='hidden' value='second value'>
<input name='inp[2]' type='hidden' value='third value'>

once this is received by your PHP script the values may be retrieved :

$myArray = $_REQUEST('ip') ; // note no subscripts required

$myArray['0'] will contain 'first value', $myArray['1'] the second value,
and so on

BTW Register globals should be OFF on any production machine so there is
little point in having it on in a development machine.

HTH

Ron

Actually, it's even easier than that. No need to specify the index when
you're using the default.

<input name='inp[]' type='hidden' value='first value'>
<input name='inp[]' type='hidden' value='second value'>
<input name='inp[]' type='hidden' value='third value'>

inp[0] == 'first value'
inp[1] == 'second value'
inp[2] == 'third value'

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 28 '06 #9
Girish wrote:
Ron Barnett wrote:
>>In the example you have given, which equates to a Get, the variables will
indeed overwrite each other with only the last value surviving, as stated by
lorento.


Hi Ron and everyone else,

Thanks for your response. Let me expand a bit on my requirement. I have
a dynamically generated form where I am now trying something similiar.
To wit:

<input name='inp[0]' type='checkbox' value='first value'>
<input name='inp[1]' type='checkbox' value='second value'>
<input name='inp[2]' type='checkbox' value='third value'>
.
.
etc

Basically, I present a user with a dynamically generated checklist and
I am trying to see all the values that he has checked.

>>once this is received by your PHP script the values may be retrieved :

$myArray = $_REQUEST('ip') ; // note no subscripts required

$myArray['0'] will contain 'first value', $myArray['1'] the second value,
and so on

Doing as you have suggested, count($myArray) gives the correct number
of values ticked.

But only $myArray['0'] is set!
$myArray['1'], $myArray['2'] , ..., etc are not set!

Not to flame or anything, I AM quite surprised by how arcane processing
a checklist in php is turning out to be. :-)
Thanks again!
Girish
Girish,

First of all, it should be $myArray[0], not $myArray['0']. It has a
numeric index. And the fact count($myArray) has the correct value
indicates the values are correct.

Also, the way you have it coded, if the 'second value' checkbox is not
checked, myValue[1] will not be set and you'll get a notice if you try
to use it. Unless you absolutely have to have the indexes like that, a
better way is to use:

<input name='inp[]' type='checkbox' value='first value'>
<input name='inp[]' type='checkbox' value='second value'>
<input name='inp[]' type='checkbox' value='third value'>

Now inp[0] will contain the value from the first checked box, imp[1]
will contain the value from the second checked box, etc.

Also, I agree with Anthony but for different reasons. I suggest you use
POST instead of GET. Not because of the array (it should work in
either), but because it can get to be quite a long URL if the user
checks several boxes.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Sep 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
13223
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax: Hunter_69. Here is what the form looks like. I have the difficulty of inserting the multiple items selected by the user the first time he visits and uses the screen and then using an UPDATE when he visits later. Model | Original Price | Reduced Price...
7
4096
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
2
1161
by: mschelstrate | last post by:
I am attempting to allow a user to update multiple rows and columns in a datagrid, when the user clicks a "Save" button, the postback occurs, at which time I need to retrieve all valuse form the cells of the grid, and write them to the database. Will I be able to loop through the items in the grid, and retrieve the values?
12
3011
by: shank | last post by:
I'm trying to use online samples for submitting multiple records from ASP into a stored procedure. Failing! Through the below form, a user could be submitting many records at a time. I'm not getting any records inserted. For troubleshooting, I cut the form down to 1 textbox and when submitted it populated 5 rows of the same data. So I know I'm connected, but not getting the LOOP and NEXT correct? How do I set this up so many records can be...
0
2072
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records from database to a VB.net app, they retrieve the database fields as a record set eg. "select name, suburb from myTable"
1
1538
by: Captain Dondo | last post by:
I am not really experienced in Javascript so bear with me.... I am working with an embedded platform; no mouse, no keyboard. Just up, down, left, right keys and +/- keys for incrementing/decrementing the vaules in the form. So my website consists of forms... Each form has a number of columns and rows. I am generating each page using a php script and some templates. Because the exact layout of each page can change based on customer...
6
5855
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList ID="FldType_add" Runat="server" DataSource='< %#GetFieldType()%>' DataValueField="Type" DataTextField="Type" /> Oce the page is loaded all the values are added to the dropdown list. But when I thought of getting the selected value from the...
0
3391
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
2
2819
by: phpnewbie26 | last post by:
I currently have two drop down menus where the second one is populated from the first one. My second drop down menu should be able to do multiple selection. I have searched online and found out how to do so (add in multiple and put brackets after name), but unfortunately when I retrieve the data on the next page, it seems that none of the selections were added to the variable array. On the other hand, it "POST"-ed the null value which is 1. My...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.