Jump to content

Questions about using post with PHP and getting the data back


dooh

Recommended Posts

Hello,

 

I am posting numbers.

 

the name = number for all names = ...and the value = different numbers eg...fx1005 or dx33 or sx9

the fx, dx, sx  is just added to the number and it tells me what to do with the value when I get it

 

here is the code I wrote to get the values back:

 

$numbers_received = array();

$numsA = array();

 

$counter = 0;

 

$numbers_received = $_POST['number'];

 

foreach($numbers_received as $value) {

$numsA[$counter] = "$value";

$counter = $counter + 1;

}

 

and now to look at what I received..

 

 

echo $numsA[4];

 

 

 

I get this error in the log: invalid argument supplied foreach()

 

It leads me to believe that what was posted was not an array. Is this true?

 

Is there a way for me to do this?

 

 

 

This was just a test sending 10 numbers and I had hoped that it would echo a result..it did not :(

 

I can do this a long way but, using perl reasoning fIigured something like this could be done in php too.. with fewer keystrokes

 

I'm new to php so don't laugh.

 

 

 

Andrew

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Here ya go,

 

<form action = "$self" method = "post">

<input type="hidden" name="number" value="dx2201">

<input type="hidden" name="number" value="dx108">

<input type="hidden" name="number" value="sx9">

<input type="hidden" name="number" value="fx1">

<input type = "submit" value = "Submit">

</form>

 

Andrew

Link to comment
Share on other sites

You know, I'm not understanding what exactly you're trying to accomplish here....

 

Anyway, you've got 1 variable (one!) in your form. How does your form pass the information to $numbers_received & $numsA? You're getting the error because those two variables don't have any information at all. Incidentally, $value up there would hold "dx2201" not "2201" like I suspect you're hoping. All that code up there does is copy the info from one array to another in a very difficult fashion.

 

Wouldn't it be easier to transfer your information in separated arrays? Something that would result in:

 

$dx = array (2201, 108);

$sx = array (9);

Link to comment
Share on other sites

In order to pass multiple values using the same name, you need to make that name an array:

<form action = "$self" method = "post">
   <input type="hidden" name="number[]" value="dx2201">
   <input type="hidden" name="number[]" value="dx108">
   <input type="hidden" name="number[]" value="sx9">
   <input type="hidden" name="number[]" value="fx1">
   <input type = "submit" value = "Submit">
</form>  

 

To reference the returned values, use "$_POST['number'][0]"  through "$_POST['number][3]".

 

Ken

Link to comment
Share on other sites

Thank you kenrbnsn,

 

Here is your answer back...

 

            $number1_received = $_POST['number'][0];

$number2_received = $_POST['number'][1];

$number3_received = $_POST['number'][2];

 

echo $number1_received;

echo "\n";

echo $number2_received;

echo "\n";

echo $number3_received;

echo "\n";

 

but...is this what you meant?

 

 

Andrew

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.