PDA

View Full Version : Mysterious PHP problem...


Arthur
12-01-2000, 02:53 PM
<? if($state){echo&quot;<option value=$state>$state&quot;;}?>
If I understand your code correctly, I think the problem may be solved if you put quotes around $state like so;
<? if($state){echo&quot;<option value=\&quot;$state\&quot;>$state&quot;;}?>

HTH

Arthur

WayneAH
12-01-2000, 03:06 PM
Jeff,

I'll be interested to see if anyone has a solution for you. I have never been able get a PHP/MySQL query to correctly fill a form field containing multiple words - the string always breaks on the first whitespace.

One inelegant solution is to str_replace whitespaces with &quot;%20&quot;. The problem is that the %20 shows up in any form you return users to (e.g., &quot;District%20of%20Columbia&quot;), though it shows as innoccuous whitespace when outputted by html outside a form.

You also might want to look at the source code for PHPMyAdmin. Tobias Ratschiller obviously has figured a way around the break-on-whitespace-inside-a-form problem. I took a look myself, but his coding is beyond my understanding.

Sorry this doesn't help much - but a least you know you're not crazy.:)

Wayne

PaulKroll
12-01-2000, 03:47 PM
Oh, I suspect Arthur's solution is in fact &quot;it&quot;, since doing &quot;value=District of Columbia&quot; (remove quotes) in HTML will get you the same result. The reason it looks as if the &quot;of Columbia&quot; is truncated is that the text is still within the <option> tag: if jefbea looks at the resulting source from the browser side, I'd put down money the text is there.

This is an HTML problem: You need to put quotes around the value in a &quot;value=&quot; statement, and if you don't, it's only by the grace of browsers that they accept any of the words at all. (Even &quot;value=1&quot; should actually be 'value=&quot;1&quot;')

SneakyDave
12-01-2000, 06:20 PM
One inelegant solution is to str_replace whitespaces with &quot;%20&quot;. The problem is that the %20 shows up in any form you return users to (e.g., &quot;District%20of%20Columbia&quot;), though it shows as innoccuous whitespace when outputted by html outside a form.

You can also use the urlencode($variable) and urldecode($variable) functions so that all special characters like spaces, quotes, double quotes, can be used.

Something like:

print '<input type=text name=state value=&quot;'.urlencode($state).'&quot;>';


The code above might work if it was written:

<? if($state){echo&quot;<option value='&quot;.urlencode($state).&quot;'>$state&quot;;}?>

[This message has been edited by SneakyDave (edited 12-01-00@5:24 pm)]

jefbea
12-02-2000, 01:32 AM
I am not a very good PHP programmer, but I am getting better... I've got this code I was hoping someone could look at and maybe suggest a fix for me (this code is for a classified ad section on my site where people are able to list the state).[nbsp][nbsp]The problem is this:

When someone posts an ad, they must select their state.[nbsp][nbsp]States with two words (i.e. New York, New Jersey, District of Columbia) are giving me a problem.[nbsp][nbsp]When they initially post their ad, as long as they have filled in all the fields properly, there are no problems.[nbsp][nbsp]If they forget to fill in a field (like their city, for example), that's when the problem occurs.[nbsp][nbsp]The code re-prints the state they live in, but instead of printing the full name of the state (i.e. District of Columbia), it will only echo the word &quot;District&quot;, truncating the &quot;of Columbia&quot;.

I hope explained that right...

Here are the two sections of code I believe are pertinent:

[SECTION 1]

<?
if (!$state)
{
echo&quot;<FONT COLOR='#FF0000'>State:&amp;nbsp;</FONT>&quot;;
}
elseif($state){
$bad=false;
for( $i = 0; $i < $array_size; $i++ )
if( eregi($cuswords[$i], $price)) {
$bad=true;
}
if($bad)
{
print(&quot;<FONT COLOR='#FF0000'>State:&amp;nbsp;</FONT>&quot;);
}
else
{
echo&quot;State:&amp;nbsp;&quot;;
}
}
?>

[SECTION 2]

<? if($state){echo&quot;<option value=$state>$state&quot;;}?>

So I guess my question is, do you see any problems with this code?[nbsp][nbsp]Can you suggest any changes that might fix this?

Thanks again for any help.

Jeff