Jump to content

Indexed $_Request entries


ejsmith67

Recommended Posts

I am convrting a website from .ASP to .PHP and I have run into an isue I cannot resolve.  I have a data entry form/table where the variable names are the same in each row.  When submitting to a PHP page to write records teh query string is similia to below

http://post.php?parm123=1&parm2=123&parm3=123&parm1=234&parm2=234&parm3=234...

In .ASP, in the receiving page I can extract the following :

parm1 = 123,234,,,,,,
parm2=123,234,,,,,
parm3=123,234,,,,,

and treat them as arrays

in PHP when I go through the $_request, only the last value is available.  Is there an extension I need ot a coding trick in PHP to get the query string into an arry, similiar to what .ASP does ??

Thanks in advance for any amd all help.
Link to comment
Share on other sites

You are reusing the same variable name twice, so the latter instance will overwrite any earlier values.

If you want to know exactly what is being passed in the url parameters, then do a print_r of the $_GET array.

[code]echo '<pre>';
print_r($_GET);[/code]

Also, use $_POST and $_GET instead of $_REQUEST.
Link to comment
Share on other sites

in the code I do use $_post, the $_request was used as follows :

  print("<pre>");
  $count = count($_REQUEST);
  print("Number of values: $count\n");
  foreach ($_REQUEST as $key=>$value) {
    print("  $key = $value\n");
  }
  print("</pre></html>\n");

to see what PHP was "seeing" as the variables...I determined how it is behaving by filling in the last line of the form ad seeing that those values survived.

Is there any way to get PHP to behave like .ASP, in that, if the same variable name is used more than once, the $_post will treat the entries as an array ?
Link to comment
Share on other sites

php does just that... well teh browser does it regardless of the aqccepting script's language...

providing you have set the name of the input like so...

<input .... name="var[]"    ....

it is not the difference in language here its the methdo of returning the variables if you have a few inputs named as above then you coudl access them in the accepting script like so..

print_r($_POST['var']);

or

foreach($_POST['var'] as $key => $val)
{
echo $key . " = " . $val;
}
Link to comment
Share on other sites

Here is the generated html form :

[code]
<TR bgcolor=#CCFF99>
<td>
<select name="iPItemNum[]">
<option selected>
<option value="13">Artichokes
<option value="25">Baby Clams
<option value="14">Bacon
<option value="54">Bill's Apple Bread Pudding
<option value="58">Biscotti
</select>
</td>
<td>
<select name="iPSizeNum[]">
<option selected>
<option value="14">1 lb.
<option value="15">3 lb.
<option value="16">5 lb.
<option value="13">8oz Cup
</select>
</td>
<td>
<input type="text" name="iPrice[]" size="10">
</tr>
[/code]

repeated 20 times

testing the receiving page with the code you provided :
[code]
foreach($_POST["iPItemNum"] as $key => $val)
{
echo $key . " = " . $val;
}
foreach($_POST["iPSizeNum"] as $key => $val)
{
echo $key . " = " . $val;
}
foreach($_POST["iPrice"] as $key => $val)
{
echo $key . " = " . $val;
}
[/code]

yields no output, but the dump of the quiry string is :

iPItemNum%5B%5D=13&iPSizeNum%5B%5D=16&iPrice%5B%5D=10.00&iPItemNum%5B%5D=54&iPSizeNum%5B%5D=17&iPrice%5B%5D=3.50

I dont mean to be dense, but am I missing something ?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.