iPixel Posted February 9, 2011 Share Posted February 9, 2011 it worked before, although now $value consists of a space and i think it may be messing things up. i pass a form field <input type="text" name="attributes[]" id="attributes[]"> and then i do this... In the case above the 3 attributes[] passed are "Value 1", "Value 2", "Value 3" $attribute = ""; foreach($_POST['attributes'] as $key => $value) { $attribute .= $value . "/"; } $atr = explode("/",$attribute); #print_r($atr); foreach($atr as $key => $value) { #THIS IS WHERE THE ISSUE ARISES... $_POST[$value] wont work echo $_POST[$value] . "<BR>"; // DOES NOT WORK #echo $value . "<BR>"; // WORKS - shows "Value 1", etc.... #echo $_POST["VALUE 1"] . "--<BR>"; // DOESNT WORK EITHER } any ideas? im 90% certain if the $value variable was just "Value" and not "Value 1" it would work as it has before. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 I think using spaces in an array key causes problems. Try Value_1 or value1 instead of having a space. Or just use the number and do a numbered array [1], [2] etc Quote Link to comment Share on other sites More sharing options...
iPixel Posted February 9, 2011 Author Share Posted February 9, 2011 The issue is, that is that attributes[] values are used to create fields such as text boxes or other... long story short i create a form and ask a few questions and use that information to dynamically create a database table and a form to enter data into that table based on the info provided. So in this case i just used value 1 as a dummy value, but in reality the value could be "Capacity lbs" so avoiding spaces is the very last thing i want to do. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 The issue is, that is that attributes[] values are used to create fields such as text boxes or other... long story short i create a form and ask a few questions and use that information to dynamically create a database table and a form to enter data into that table based on the info provided. So in this case i just used value 1 as a dummy value, but in reality the value could be "Capacity lbs" so avoiding spaces is the very last thing i want to do. Well, try not using those as the array keys, but rather an ID. Such as "question_1". Quote Link to comment Share on other sites More sharing options...
iPixel Posted February 9, 2011 Author Share Posted February 9, 2011 Cant do that either, my question form goes like this... How many attributes will you require? <text box > enter number user javascript to dynamically create the number of textboxes asked for user then enters a "name" value of each attribute[] ie: Color, Capacity, Construction, Material etc.... so i do not have any idea what the user enters outside of checking attributes[] also the array works fine, because if i echo $value withing my foreach loop, it prints Value 1 Value 2 Value 3 i think the issues is with $_POST["VALUE 1"] or rather $_POST[$value] not doing it's job, which also is because of the space that the string contains in $value. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 So each time they create a new attribute, you're saving it somehow, right? Into an array. So instead of trying to use the name as the key, just use the NUMBER as the key. Look I don't even know if this is the problem, but have you TRIED removing the spaces? Because I think it is. But I'm not going to keep trying to help you figure out a solution if you won't do a little debugging and see if removing the space works. Quote Link to comment Share on other sites More sharing options...
iPixel Posted February 9, 2011 Author Share Posted February 9, 2011 I did check without spaces... and it works 100% fine without them, it's the $_post not liking the space withing the [] Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 Okay, so instead of using those names as the keys, use a different identifier. When you get the array of what names they want, that array should already have array_keys of 0-x. Use those instead of the names. Or prepend "question_" to the front. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 9, 2011 Share Posted February 9, 2011 Why not simply use str_replace on the keys, turning any spaces into underscores? foreach($atr as $key => $value) { $value = str_replace(' ', '_', $value); // continue from there } Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 Why not simply use str_replace on the keys, turning any spaces into underscores? foreach($atr as $key => $value) { $value = str_replace(' ', '_', $value); // continue from there } Also a good option - but what happens when the user enters an apostrophe? Aren't there a few other characters you can't use in keys? I think it'd be better in the long run to use a name you know will be the same every time. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted February 9, 2011 Share Posted February 9, 2011 Why not simply use str_replace on the keys, turning any spaces into underscores? foreach($atr as $key => $value) { $value = str_replace(' ', '_', $value); // continue from there } Also a good option - but what happens when the user enters an apostrophe? Aren't there a few other characters you can't use in keys? I think it'd be better in the long run to use a name you know will be the same every time. True. Regex is always an option, too, but consistent key names are better. Quote Link to comment Share on other sites More sharing options...
iPixel Posted February 9, 2011 Author Share Posted February 9, 2011 yea that's what i did, i replaced all the spaces with underscores before the table and form get created... so basically i force underscores on the user whether they like it or not. Thanks Guy! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2011 Share Posted February 9, 2011 You would want to use the entered 'name' (Value 1, Capacity lbs, ...) as the Label for a field, not necessarily the field name you use in the underlying code. For a dynamically generated form based on user defined fields, you could generate your own field names and associate them with the correct field. Only your form and your form processing code would care what the actual field names are. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.