izzus Posted March 10, 2011 Share Posted March 10, 2011 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... <?php $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. <?php 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. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted March 10, 2011 Share Posted March 10, 2011 It is also in the form action, but the problem is that you are already in PHP and already echoing so why do <?php echo? echo '<input type="text" name="loginnm" style="visibility: hidden" value="' . $loginname . '">'; Quote Link to comment Share on other sites More sharing options...
izzus Posted March 11, 2011 Author Share Posted March 11, 2011 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. Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 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 Quote Link to comment Share on other sites More sharing options...
izzus Posted March 11, 2011 Author Share Posted March 11, 2011 ahhhhh, O.K. I get it now. Thank you so much for the explanation Mahngiel. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.