Jump to content

assigning php variable value to form element value not working


Recommended Posts

Hello Everyone,


I am new to forum and could use some help with some php code that isn't working.  I am very new to php/html/javascript and all of what I have learned, I learned from forums like this one so first....thank you!


I am trying to assign a value from a php variable to the value of my form element.  I'm sure there must be a way to do this but I can't seem to get the syntax right.  here is my code...


first I set the value of $loginname elsewhere in the script like so...



$loginname =strtolower(htmlspecialchars(strip_tags($_GET["loginname"])));


This part works fine..


Then I try to set the value of my hidden text field inside the form to the value of $loginname to be passed to a javascript program.  Everything works except that the value passed ends up being <?echo and not the expected user name inside of $loginname.




echo '<form name ="currentactivity" Id="currentactivity" action="<?php'.htmlspecialchars($_SERVER['PHP_SELF']).'?>"      method="post">';


echo '<fieldset><legend><b>Your Current Activity Information</b></legend>';

echo '<input type="text" name="loginnm"  style="visibility: hidden" value="<?php echo $loginname;?>">';


echo "<label for='myactivities'>Activity Name:</label>";



echo "<select name='myactivities' Id='myactivities' onchange=\"showdetails(this.form)\" value=''>";

echo "<option value = 'Select an activity'>Select an activity</option>";

for ($i = 1; $i <=$rowcount; $i++) {

  echo"<option value=$row[activity_name]>$row[activity_name] </option>";


  $row = mysql_fetch_array($result);



echo "</select>";


echo '</fieldset>';


echo '</form>';



Please note..the rest of the code is working perfectly, it is just this one value I can't seem to get.

Any help you can give will be greatly appreciated. 


Link to comment
Share on other sites

Thank you so much AbraCadaver.  That worked.  Question though, I'm not sure why you have to put the single quotes inside double quotes with . for it to work  as in ... value="' . $loginname . '"

I tried putting just value ="$loginname" and I also tried value=$loginname none of which worked.  Sorry, like I said I'm new to php so I'm just trying to understand why I need to put '.$loginname.'


Thanks again.  I really appreciate your help.  You saved me from another night of frustration.  :)

Link to comment
Share on other sites

your problem is two fold.  first, the fact that you're in a current echo string, it is already quoted off.  you use the single quote when doing HTML because it makes it easier to determine where your HTML values are, because they use double quotes.  you need to stop the string that is being echo'd, so you use a single quote to stop what you started.  You concatenate (place periods) beside variables that you are putting in place because it connects the strings. 


Here is a bit of a helper:

echo '<div id ="' .$name. ' ">';

the div class here is dbl quoted in HTML.  So we are wrapping our variable $name in "".  But we have to stop the echo of HTML so we can call a PHP.  Then it's started again and finished.  break it up and it looks like:

echo '<div id="'  //echo string started, printing to the page that you want the next thing to start a quote
.$name.  //variable concatenated to show it has connections on the left AND right side to the string
'">';  //  finished off the quote to surround the name, and closed the tag


notice everything HTML is started with the single quote because you are in an echo statement

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.