Jump to content


Photo

Indexed $_Request entries


  • This topic is locked This topic is locked
6 replies to this topic

#1 ejsmith67

ejsmith67
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 20 August 2006 - 11:44 PM

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?parm...34&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.


#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 August 2006 - 11:47 PM

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.

echo '<pre>';
print_r($_GET);

Also, use $_POST and $_GET instead of $_REQUEST.

#3 ejsmith67

ejsmith67
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 August 2006 - 12:16 AM

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 ?

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 12:39 AM

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;
}
follow me on twitter @PHPsycho

#5 ejsmith67

ejsmith67
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 August 2006 - 01:05 AM

Here is the generated html form :

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

repeated 20 times

testing the receiving page with the code you provided :
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;
}

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 ?



#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 August 2006 - 01:10 AM

sorry matey i used $_POST...

you r sending via get so replace $_POST with $_GET
follow me on twitter @PHPsycho

#7 ejsmith67

ejsmith67
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 August 2006 - 01:21 AM

Thanks for the help, I'm gettng waht I need now.  It was the '[]' in the form that is required for PHP that wasnt required for ASP




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users