Jump to content

How Can I Pass an Array via HTTP_POST_VARS?


stervyatnik

Recommended Posts

Hello, all!

 

I have a PHP page, which queries a database and displays a multi-lined form, containing a state and an associated rate, like this:

[pre]

AL    0.0240

AR    0.0630

AZ    0.0580

CA    0.0250

CO    0.0680

[/pre]

The number of lines is variable - there can be as many as 51 (all states plus DC), but in some cases, not all states have rates, and are not displayed. You can edit/change the rate, but not the state. I want to pass each state and rate to a PHP page that will then load new values in the database. There is no primary key in the database; it uses the state and other values as a composite key. My initial thought is to do it like this (line numbers added for reference):

[pre]

01 $query = "SELECT * from tbl_Rates where carrier = '".$HTTP_POST_VARS["carrier"]."'

02 and level = '".$HTTP_POST_VARS["clevel"]."' and direction = '".$HTTP_POST_VARS["direction"]."' order by state";

03 $result = mysql_query($query);

04 $num_results = mysql_num_rows($result);

05 echo "<form name=\"form2\" method=\"post\" action=\"update_intrastate.php\">

06 <table>

07 <tr>

08  <td>Carrier</td>

09  <td>Direction</td>

10 </tr>

11 <tr>

12  <td>State</td>

13  <td>Rate</td>

14 </tr>";

15 for ($i=0; $i <$num_results; $i++)

16 {

17 $row = mysql_fetch_array($result);

18 echo "<tr>

19  <td><p><input type=\"hidden\" name=\"state".$i."\" value=\"".$row["state"]."\">".$row["state"]."</td>

20  <td><p><input type=\"text\" name=\"rate".$i."\" value=\"".$row["rate"]."\" size=\"10\"></td>

21 </tr>";

22 }

23 echo "</table>

24 <input type=\"hidden\" name=\"carrier\" value=\"".$HTTP_POST_VARS["carrier"]."\">

25 <input type=\"hidden\" name=\"clevel\" value=\"".$HTTP_POST_VARS["clevel"]."\">

26 <input type=\"hidden\" name=\"direction\" value=\"".$HTTP_POST_VARS["direction"]."\">

27 <input type=\"hidden\" name=\"results_total\" value=\"".$num_results."\">

28 <input type=\"submit\" name=\"Submit2\" value=\"Save\">

29 </form>";

[/pre]

In lines 19 and 20, I combine the count ($i) with the field name, and use it in a hidden and text input element, so I should get HTTP_POST_VARS like:

 

state0

state1

state2...etc.

 

and

 

rate0

rate1

rate2...etc.

 

In the processing page - update_intrastate.php - how can I reference these variables? I pass the variable "results_total" which gives me the seed for the counter I'd use, so I envision my code on the processing page would look something like this:

[pre]

for ($i=0; $i <$HTTP_POST_VARS["results_total"]; $i++)

{

$query = "update tbl_Rates set rate = '".$HTTP_POST_VARS["rate$i"]."'

WHERE carrier = '".$HTTP_POST_VARS["carrier"]."' and

level = '".$HTTP_POST_VARS["clevel"]."' and

state = '".$HTTP_POST_VARS["state$i"]."' and

direction = '".$HTTP_POST_VARS["direction"]."'";

mysql_query($query);

}

[/pre]

Can I reference the variable like this:

 

$HTTP_POST_VARS["rate$i"]

$HTTP_POST_VARS["state$i"]

 

INSIDE the quotes as part of the HTTP_POST_VARS name? I suspect this won't work. What would be the correct way? Any pointers would be greatly appreciated. Thanks!

 

Dan sends...

 

 

 

 

 

$HTTP_*_VARS are now depreciated and have been replaced with $_*, eg $HTTP_POST_VARS should be $_POST

 

Use:

$_POST['rate' . $i]

 

When trying to get rateX (X being a number). You do the same with state too:

$_POST['state' . $i]

Archived

This topic is now archived and is 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.