Jump to content

Why isnt this working?


iPixel

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.