Spaces in PHP post variables

I’m running php off my mac using MAMP, just wondering, I’m getting a huge headache trying to sort out post variables with spaces in them. When outputted, they appear normal, but $_POST[‘words’] doesn’t equal “Family Guy”, even when <input name=“Family Guy” />, ya know? I’d appreciate some help here. Is there a function that evaluates strings literally or something of the sort?

<input name=“words” value=“Family Guy” />

$_POST[‘words’] = “Family Guy”;

name is the $_POST key value is the key’s value make sure you aren’t confusing the two.

Yea, sorry, wasn’t thinking. That’s what I meant. It is still giving me problems.

What problems is it giving you?

You can type cast it to a string…


foreach ($_POST as $key => $value) $_POST[$key] = (string) $value;

but I think that your problem is elsewhere as normally this issue only occurs with strings being interpreted as ints. You may want to have a look at some of your other code.

please show us some code which demonstrates your problem. isolate the issue. theres a good chance you will find your mistake as you prepare a working snippit for us :slight_smile:

Here is a pretty close example to my actual code:

<input type=“checkbox” name=“Two Words” checked=“checked” />

the form is submitted and on the next page:

<?php
$v = “Two Words”;
if ($_POST[$v] != “”){
echo “Success”;
}

?>

With my current setup, “Success” will never be echoed.

print_r($_POST);

you shouldnt use spaces in the name attributes, unless your prepared to see them get converted :slight_smile:
stick to characters that are allowed in a normal php variable.

You need two equal signs here: if ($_POST[$v] != “”){

So it should like the following…


<?php
$v = "Two Words";
if (! isset($_POST[$v]) || $_POST[$v] !== ""){
echo "Success";
}
?>

You’ll notice I’ve added something to your ‘if’ condition. Sometimes, when the $_POST variable hasn’t been set, it can equal something other than “” (such as a ‘0’ or ‘false’). So to be on the safe side, it’s good to use the || operator (or).

So if you use the code above, you should be good to go.

When you say “converted”, clamcrusher, you’re saying I’m not crazy? Variable names with spaces are actually altered? How do I change it back?

I solved the issue by converting the space to a string, then swapping it back, but I still have this issue on another page where its part of a mysql query.

Why do you need the spaces in the variable name? Is any logic depending on what the key name of $_REQUEST is?

You should only be using the name part of the input as an identifier, not as a value, therefore you should so it like this:

html input:

<input type="checkbox" name="TwoWords" value="Two Words" checked="checked" />

the form is submitted and on the next page:

<?php

if ($_POST['TwoWords'] == "Two Words"){
echo "Success";
}

?>

that will work

Flrb, I’ve built a CMS, and don’t have control over the variable name (its based off the name of a mysql table field name).

The whole point of this is to see what fields a user wants in his “database rule” of Pheap. The input name is based off the field in a mysql database table. Like I said, everything works if the field is one word, however, if its two words, it looks like this:

<input type=“checkbox” name=“Two Words_selected” checked=“checked” />


 //(gets value of $v from a hidden input on previous page)
 //if I echo $v here, it gives me "Two Words";
 $v .= "_selected";
 if ($_POST[$v] != ""){
  echo "Selected";
 }
 

Does anyone know why the spaces throws off php, why the space is converted to something else? I’ve tried doing str_replace(“%20”," ",$v), but it still doesn’t work.

Is it data from the database or is it a column name? If it’s the latter you should consider changing the design. Don’t column names with spaces smell?

With register_globals on PHP would try to register your $_REQUEST-key as a local variable, but variables may not have spaces. Except for things like


$varname ="Two Words";
${$varname} = "abc";

which I don’t think anyone should do without strong reason.
Regards,
Frl. B.

Sorry, I wasn’t clear. A database rule allows the user to add, edit and delete rows from other database tables, not the ones the CMS uses to store information. Say the user had a table that had three fields: “ID”, “Event Date” and “Location”. If they wanted to be able to manipulate that content in the CMS, I can’t do anything about the way they named their fields, or the way their developer named their fields.

So, in answer to your question, FrlB, it is a column name, but its not my column name.

Your solution doesn’t seem relavent to my problem.

Again, and to make it simpler, does anyone know why $_POST[‘Two words’] doesn’t return the value of <input name=“Two words” />?

When you use name=“Two Words”
It will be changed to $_POST[‘Two_Words’] so it won’t work.

try something like

<?php
$a = "Two Words";
$a = str_replace(" ","", $a);
echo '<input type="text" name="'.$a.'" value="test case">';
?>

<?php
$v = "Two Words";
$v = str_replace(" ","", $v);
if ($_POST[$v] != ""){
echo "Success";
}
?>

probly there is a beter way then str_replace() but can’t come up with it right now.
I say this … simply couse the space is not the only character that could give you problems.

print_r($_POST);
stick to characters that are allowed in php variables. this is because if register globals was on, php needs to be able to convert those request variables into php variables. so php must convert invalid characters into some other characters which are allowed.

there is a special syntax for name attributes. if you use the chars at the end of the name, php will recognize this and apply special meaning, and parse them into an array for you.


<input type="checkbox" name="fields[]" value="two words">
<input type="checkbox" name="fields[]" value="now 3 words">
<input type="checkbox" name="fields[]" value="other.chars">


print_r($_POST['fields']);

now you can easily loop through the array and whatever fields you find, are “checked”. browsers wont send checkbox fields if they arent checked. if this bothers you, apply the same logic but use radio lists instead.

Very helpful clamcrusher, I wish I had known that as I was building it. I ended up taking an approach similar to Bodram’s, I replaced the space with a n obscure string, then reversed it on the other side.

And for the record, use the ` character when making mysql queries where database, table, or field names have more than one word. That also brought me much pain and suffering!

Thanks all who helped me out!