Click to See Complete Forum and Search --> : select box values causing invalid retreival


moondance
09-05-2003, 06:41 AM
I have on my page a select box:

echo "<select name = SearchType>
<option value = Keyword>Search by Keyword</option>
<option>-----</option>
<option value = In Progress>Status: In Progress</option>
<option value = Awaiting Information>Status: Awaiting Information</option>
<option value = Complete>Complete</option>
</select>";


and a text box to search for a keyword:
<input type = text name = SearchTerm>

$SearchTerm = $_POST['SearchTerm'];
$SearchType = $_POST['SearchType'];

Then the retreival:
if ($SearchType =="Keyword")
{
$result = mysql_query("select * from accounts where description like '$SearchTerm' ") or die ("Can't connect");
}
else
{
$result = mysql_query("select * from accounts where status = '$SearchType' ") or die ("Can't connect");
}

This acts as a simple search, where a user can search for records based on their status, complete, in progress or awaiting information.

Searching via keyword works fine, and so does searching for records with a status of complete. Yet searching with the other status' doesn't work. When i echoed $SearchType, it only stores the first word - thats why completed works (cos its one word) whereas awaiting information is echoed as 'awaiting', and in progress is echoed as 'in'.


Any ideas how to solve this?

pyro
09-05-2003, 08:01 AM
You need to quote your attibute's values... This is not only necessary for PHP, but also necessary to have good markup... http://www.w3.org/TR/xhtml1/#h-4.4 (XHTML)

echo '<select name="SearchType">
<option value="Keyword">Search by Keyword</option>
<option>-----</option>
<option value="In Progress">Status: In Progress</option>
<option value="Awaiting Information">Status: Awaiting Information</option>
<option value="Complete">Complete</option>
</select>';

and
<input type="text" name="SearchTerm">

moondance
09-05-2003, 08:22 AM
hmmm thats weird that is - when i started with php, whenever i'd quote a value within, say, an echo statement, i'd get an error message, about an unexpected T_STRING, expecting "," or ";" blah blah.

I just got into the habit of not quoting them.

The correction you gave me has solved the problem pyro, thanks, but out of interest, should i always use single quotes with echo tags?

echo 'some text';

instead of

echo "some text";

ta.

pyro
09-05-2003, 08:38 AM
Ok, a couple of things here...

First of all, I'd be willing to bet the reason you were getting the unexpected T_STRING error is because you did something like this:

echo "Someone said "Hello World!" to someone else";
#when it should have been
echo "Someone said \"Hello World!\" to someone else";Note the backslashes. You need to escape the inner slashes, or PHP thinks you are ending your echo statement.

Also, there is a difference between echo 'test' and echo "test", though not in that format. If you use single quotes, any variables will be echoed as their variable names, while with double quotes, the value of the varible will be echoed. Take a look at this:

$var = "test";
echo $var; #will echo test
echo "$var"; #will also echo test
echo '$var'; #will echo $varAnyway, the reason I used single quotes is because we didn't use any variables, and I didn't want to escape all the double quotes in the string. We could have also done this:

echo "<select name=\"SearchType\">
<option value=\"Keyword\">Search by Keyword</option>
<option>-----</option>
<option value=\"In Progress\">Status: In Progress</option>
<option value=\"Awaiting Information\">Status: Awaiting Information</option>
<option value=\"Complete\">Complete</option>
</select>";

moondance
09-05-2003, 08:42 AM
your absolutely right - thats how i've been using echo's up till now.

thanks for taking the time to explain it to me.

Looks like ol' moony's gonna be going through his code tonight with a fine toothcomb :(

pyro
09-05-2003, 08:44 AM
lol...Happy to help... :)