Jump to content

[SOLVED] Forms and $_POST


cyrilsnodgrass

Recommended Posts

Hi,

 

Getting nowhere fast with my forms problem haveing tried some mad things I think it is something basic I am missing. What  am doing is building  form using a foreach loop. I have hidden fields that are assigned the $key value from the foreach. Anytime I click submit though each of these fields contain the last item added to the array.

 

Is it something to do with references ?  Are the variables in the $_POST mechanism all set to the last item in the original array and if so how do I remedy this ? Using =& seems to have no effect.

 

Here is a code snippet extracted from the form.

 

<code>

/** Loop to retrieve and build table values from cart                                **/

 

  foreach ($_SESSION['cart'] as $key => $value)

  { 

      $cart_item =& $key;

      $disp_str .= "<tr><td><img src='images/";

      $disp_str .= $cart_item;

      $disp_str .= ".jpg' alt='";

      $disp_str .= $cart_item;

      $disp_str .= "'></img></td>";

      $disp_str .= "<td>$cart_item qty = ";

      $disp_str .= "$value </td>";

      $disp_str .= "<td><input type='submit' name='rem' value='remove'</input></td>";

      $disp_str .= "<td><input type='text' name='remitem' value='";

      $disp_str .= $cart_item;

      $disp_str .= "'></td><td></input><input type='text' name='cartaction' value='remove'</input></td></tr>";

  }

  $disp_str .= "</table></form>";

  echo $disp_str;

}

 

 

 

</code>

Link to comment
https://forums.phpfreaks.com/topic/47708-solved-forms-and-_post/
Share on other sites

you're concacting variable inside text strings incorrectly

 

$disp_str .= "$value </td>";

 

change to this:

 

$disp_str .= $value.' </td>';

 

the way concactation works is like this:

use single quotes ' ' ' ' '

when you do this: '.$variable.'

that echoes the value of that variable

when you do this '$variable' that echoes the text string: $variable

 

do some reading on properly concacting variables in text strings

but thats the basics

 

-good luck

Hi,

 

I am not sure about your post dsaba.....................

 

Concatenating inside double quotes will replace the variable declaration with the variable value. This works on all forms I've created. The replacement does not work in single quoted strings - as you correctly point out.

 

The $value bit is working fine.

 

What doesn't work is the setting of the remitem $_POST variable. When I use this variable in my action form, it is always set to the last $key from the cart array. This is why I am asking adumb questions as no-one seems to get what I am asking !

 

Hi,

 

Getting nowhere fast with my forms problem haveing tried some mad things I think it is something basic I am missing. What  am doing is building  form using a foreach loop. I have hidden fields that are assigned the $key value from the foreach. Anytime I click submit though each of these fields contain the last item added to the array.

 

Is it something to do with references ?  Are the variables in the $_POST mechanism all set to the last item in the original array and if so how do I remedy this ? Using =& seems to have no effect.

 

Here is a code snippet extracted from the form.

 

<code>

/** Loop to retrieve and build table values from cart                                 **/

 

   foreach ($_SESSION['cart'] as $key => $value)

   {   

      $cart_item =& $key;

      $disp_str .= "<tr><td><img src='images/";

      $disp_str .= $cart_item;

      $disp_str .= ".jpg' alt='";

      $disp_str .= $cart_item;

      $disp_str .= "'></img></td>";

      $disp_str .= "<td>$cart_item qty = ";

      $disp_str .= "$value </td>";

      $disp_str .= "<td><input type='submit' name='rem' value='remove'</input></td>";

      $disp_str .= "<td><input type='text' name='remitem' value='";

      $disp_str .= $cart_item;

      $disp_str .= "'></td><td></input><input type='text' name='cartaction' value='remove'</input></td></tr>";

   }

   $disp_str .= "</table></form>";

   echo $disp_str;

}

 

 

 

</code>

 

 

 

Why no try to user the var like so

 

$var = $var2."some html".$var3."some more html";

 

 

it works for me ...:D

I like building up a string in sections so that it's easier to understand and spot screw ups - well it is for me anyway.

 

I think folks are misunderstanding my problem or maybe do not know the answer !!!!!

 

The problem has nothing at all to do with concatenating text or variable substitution apologies if I haven't made that clear.

 

The code snippet works really well and renders everything to the user agent as you would expect and it looks brill. When I click the submit button to remove an item the $_POST['remitem'] always contains the last item from the array and I end up removing the wrong item in the action page.

 

It's driving me mental  ;D

you need to learn how to better explain your problem

now you have made it clear what your problem is

but still we are clueless to what your expected result should be

 

the code you supplied overwrites all the form variablies for each value of your array

so yes the last variables which will echo, will be the last value of that loop

    the script isn't doing anything wrong

 

 

like I said how can we give you some kind of algorithim if you have not explained what you WANT it to do specifically

we cannot read your mind on "fixing it", if you dont' tell us what "fixing it" means to you huh?

"Getting nowhere fast with my forms problem haveing tried some mad things I think it is something basic I am missing. What  am doing is building  form using a foreach loop. I have hidden fields that are assigned the $key value from the foreach. Anytime I click submit though each of these fields contain the last item added to the array."

 

I thought this was pretty clear and does not refer to string concatenation (which I note you seem to have a problem with elsewhere) ?

 

It specifically mentions what happens when I click on submit.......

 

However, I have to say that the penny has dropped. I am feeling a bit sheepish right about now as I have realised just how thick I have been !

 

Many many thanks to all who helped out

 

 

Should've posted my solution - don't know if it will help anyone but if anyone is interested.........

 


$disp_str .= "<table border='1'><tr><td colspan='3'><h2>Your Shopping Cart:</h2></td></tr>";

/** Loop to retrieve and build table values from cart                                 **/
/** each row needs to be in its own form in order to set the correct $_POST variable  **/

   foreach ($_SESSION['cart'] as $key => $value)
   {
      $cart_item =& $key; 
      $disp_str .= "<form method='post' action='cartaction.php'>";
      $disp_str .= "<tr><td><img src='images/";
      $disp_str .= $cart_item;
      $disp_str .= ".jpg' alt='";
      $disp_str .= $cart_item;
      $disp_str .= "'></img></td>";
      $disp_str .= "<td>$cart_item qty = ";
      $disp_str .= "$value </td>";
      $disp_str .= "<td><input type='submit' name='rem' value='remove'</input>";
      $disp_str .= "<input type='hidden' name='remitem' value='";
      $disp_str .= $cart_item; 
      $disp_str .= "'></input><input type='hidden' name='cartaction' value='remove'</input></td></tr>";
      $disp_str .= "</form>";
   }
   $disp_str .= "</table>";
   echo $disp_str;

 

Basically each item has to be in it's own form to ensure the $_POST is set up correctly for the action page.

 

 

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.