Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird behavior of $_POST var 3

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys :)

I have this :

Code:
<input type="text" name="my_var[]" value="1" />
<input type="text" name="my_var[]" value="2" />
<input type="text" name="my_var[]" value="3" />

.
.
.

Form is submitted

.
.
.

$_POST["my_var"] = implode(",", $_POST["my_var"]);

.
.
.

echo $_POST["my_var"]; // value is string "1,2,3"

.
.
.

store_post_vars_in_db(); // this function takes $_POST vars and store them in the db

.
.
.

// stored value is an array but not a string !!!

Why isn't the imploded variable in the db?
Is a copy of $_POST["my_var"] made in the process?

Thanks for the help :)
 
The problem could be in the [tt]store_post_vars_in_db()[/tt] function.

 
Without more code, it is hard to tell exactly where the problem is. I would start by not writing the imploded string back into the $_POST superglobal and defining another variable instead.

Use that variable in your store_post_vars_in_db() function and you should be home free.
 
Yeah, seemed logic to me too but I don't see what could be wrong in this function.

Inside this function, the value is grabbed this way :

$value = $_POST[$field_array[$i]];

$field_array[$i] being the input name "my_var".

But yeah, a test on $_POST[$field_array[$i]] shows that it is an array, which shoudn't after the implode.

So, $_POST[$field_array[$i]] calls the variable before implode when $_POST["my_var"] calls the variable after implode.

I don't see why :(

 
are you sure that the $field_array and $i variables are available inside the store_to_db function? if not then the key will be null and the db will get the entire $_POST array.

you can make the vars available through the global keyword.

otherwise - it's generally seen as bad practice to manipulate the contents of superglobals (other than $GLOBALS). but so long as you maintain control of your code and remember your steps (and document them for future maintainers) then it is (imo) no big deal.
 
Hey Jpadie back from holiday? ;)

You were spot on! I fixed the issue thanks to you.
Very impressive considered the lack of information ahaha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top