Forum Moderators: coopster

Message Too Old, No Replies

variable question

         

hal12b

8:27 pm on Jun 16, 2008 (gmt 0)

10+ Year Member



Hello
If this is my line of code -->
echo '<td> <form method="post" action="update1.php"> <input name="textfield" type="text" value="ddasads" size="q" /> <input name="dsds" type="submit" value="Update" /></form></td>';

How could I replace the value of ddasads with a variable? I've tried a few options, but nothing worked. The variable is $row['HOBBY']

The problem I think is with the quotes and how to end it and begin them again. I am fairly new to PHP

Thanks,
Hal

Little_G

9:17 pm on Jun 16, 2008 (gmt 0)

10+ Year Member



Hi,

echo '<td> <form method="post" action="update1.php"> <input name="textfield" type="text" value="', $row['HOBBY'], '" size="q" /> <input name="dsds" type="submit" value="Update" /></form></td>';

should work.

Andrew

trigoon

10:54 pm on Jun 16, 2008 (gmt 0)

10+ Year Member



What Little_G said should work. The reason for what you might have been doing to not have worked is the type of quotes you are using. The '' quotes take anything put into them and treats it as text, therefore if you put a variable in it you do not have the variable treated as one. There are multiple ways to bypass this, one is to use quotes like these: "". However for those you need to make sure that any other quotes in the thing you are echoing are escaped. Example: type=\"submit\"

A better way is to concatenate the variable with the string. The above is right, some tutorials on PHP may say to do this:
echo '<td> <form method="post" action="update1.php"> <input name="textfield" type="text" value="'.$row['HOBBY'].'" size="q" /> <input name="dsds" type="submit" value="Update" /></form></td>';

The result is the same.

hal12b

12:11 pm on Jun 17, 2008 (gmt 0)

10+ Year Member



Thanks to both of you. I will test this when I have time later today.

cantona

3:30 pm on Jun 17, 2008 (gmt 0)

10+ Year Member



If you want to echo a variable I'd use double quotes. Then you can just add in your variable wrapped in curly brackets.

echo "There are {$count} results";

Easier to read when you have to look at the code again in 2 years ;)