Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP script help 2

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
I am trying to populate my text field with a value from the database.
my database field name is address and it is varchar type.
for eg: if i store "100 Johnr road" in the database for the address field

and using the following line in php program

while ($row=mysql_fetch_array($result))
$address=$row["address"];
printf("<input type=text name=dbaddress
value=%s>",$address);

returns only the value 100 and not 100 johnr road.
How to modify the above statement or how to achieve the full address value?

 
You should always put doublequotes around the values of all HTML attributes. First, because it's part of the HTML spec. Second, because if, like your situation, you have a value with spaces in it, without quotes a browser will assume that the value ends at the first space and the second word of the value is another attribute, not a continuation of the value.

Try:

printf('<input type="text" name="dbaddress" value="%s">',$address);

or

print '<input type="text" name="dbaddress" value="' . $address . '">';



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
alternatively you can escape quotes (personally this gives me nightmars tho)..

print "<input type=\"text\" name=\"dbaddress\" value=\"" . $address . "\">";

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
thanks guys worked perfectly.
also being a newbie I am not sure how do I do this. I have a found a way but thats not efficient.


Again I am trying to populate my select field with a value from the database.

printf("<option>%s</option>",$sex);
printf("<Option value=Male>Male</Option>");
printf("<Option value=Female>Female</Option>");

If the person has chosen male upon registering, then on modifcation using the above code he would get

Male which will be shown selected
and again
Male
Female
totally he will get 3 options instead if two. This holds good for selct fields like Month,State etc..I have coded this way..but there should be some more efficient way.

Thanks again.

Muru
 
What about simply

Code:
echo "<option value=\"Male\"".(($sex=="Male")?" SELECTED":"").">";
echo "<option value=\"Female\"".(($sex=="Female")?" SELECTED":"").">";

Also, I would reiterate the comments above about putting quotes around your attribute values - it will save you time and effort in the long run.

-Rob
 
alternatively you can escape quotes (personally this gives me nightmars tho)..

print "<input type=\"text\" name=\"dbaddress\" value=\"" . $address . "\">";
if that seems as if it is a pain, then dont use the double quote delimiter. Easier:

Code:
print '<input type="text" name="dbaddress" value="' . $address . '">';
 
It was done to demonstrate an alternative.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I realize such sir, It was in no way an attempt to discredit your method, or hack you off. :)

--... ...--, Eric.
 
thanks guys and
broccoli2 - I will try it out and post back.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top