Chris.P Posted May 16, 2007 Share Posted May 16, 2007 I want to put a variable as the content of a hidden field. I realise that normally you would just open up a set of php tags and echo the variable in which I have done in the past, although I need the whole form in php tags as I have made the form a function in order to hide it with sessions when no body is logged in. This seems to be causing a problem as I can't get the content of the variable into the field its just echoing the actual statement in. Any help? Here's the function contained in functions.php that holds the form. <?php function addComment() { // Check if we have established an authenticated session if (isset($_SESSION["authenticatedUser"])) { echo '<form id="form1" name="form1" method="post" action="addcomment.php"> Comment: <br /> <input name="hiddenField" type="hidden" value="<?php echo $bandid ?>" /> <br /> <textarea name="commentBox" id="commentBox"></textarea> <br /> <br /> <input name="Submit" type="submit" class="button" value="Submit" /> </form>'; } else { echo '<h2>Please log in to post a comment.</h2>'; } } ?> And here's the page that pulls the function. <?php session_start(); $bandid = $_GET["bandid"]; addComment(); Quote Link to comment https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/ Share on other sites More sharing options...
hitman6003 Posted May 16, 2007 Share Posted May 16, 2007 "exit" from the single quote using another, then concatenate the variable with your echo statement... function addComment() { if (isset($_SESSION["authenticatedUser"])) { echo ' <form id="form1" name="form1" method="post" action="addcomment.php"> Comment: <br /> <input name="hiddenField" type="hidden" value="' . $bandid . '" /> <br /> <textarea name="commentBox" id="commentBox"></textarea> <br /> <br /> <input name="Submit" type="submit" class="button" value="Submit" /> </form>'; } else { echo '<h2>Please log in to post a comment.</h2>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/#findComment-254924 Share on other sites More sharing options...
Chris.P Posted May 16, 2007 Author Share Posted May 16, 2007 "exit" from the single quote using another, then concatenate the variable with your echo statement... function addComment() { if (isset($_SESSION["authenticatedUser"])) { echo ' <form id="form1" name="form1" method="post" action="addcomment.php"> Comment: <br /> <input name="hiddenField" type="hidden" value="' . $bandid . '" /> <br /> <textarea name="commentBox" id="commentBox"></textarea> <br /> <br /> <input name="Submit" type="submit" class="button" value="Submit" /> </form>'; } else { echo '<h2>Please log in to post a comment.</h2>'; } } I'm not entirely sure what you mean, when you say "exit" from the single quotes do you mean by using the single quotes you put round the variable? I tried the solution you posted and I'm getting nothing from that variable. I can echo out the variable just fine from outside the addComment() function though. ??? Quote Link to comment https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/#findComment-254942 Share on other sites More sharing options...
hitman6003 Posted May 17, 2007 Share Posted May 17, 2007 It needs to be passed to the function or made a global... function addComment($bandid) { if (isset($_SESSION["authenticatedUser"])) { echo ' <form id="form1" name="form1" method="post" action="addcomment.php"> Comment: <br /> <input name="hiddenField" type="hidden" value="' . $bandid . '" /> <br /> <textarea name="commentBox" id="commentBox"></textarea> <br /> <br /> <input name="Submit" type="submit" class="button" value="Submit" /> </form>'; } else { echo '<h2>Please log in to post a comment.</h2>'; } } or if it's passed in the POST... function addComment() { if (isset($_SESSION["authenticatedUser"])) { echo ' <form id="form1" name="form1" method="post" action="addcomment.php"> Comment: <br /> <input name="hiddenField" type="hidden" value="' . $_POST['bandid'] . '" /> <br /> <textarea name="commentBox" id="commentBox"></textarea> <br /> <br /> <input name="Submit" type="submit" class="button" value="Submit" /> </form>'; } else { echo '<h2>Please log in to post a comment.</h2>'; } } or, as a last resort, and not recommended at all, but given for example purposes (it's not good OOP practice to do it this way): function addComment() { global $bandid; if (isset($_SESSION["authenticatedUser"])) { echo ' <form id="form1" name="form1" method="post" action="addcomment.php"> Comment: <br /> <input name="hiddenField" type="hidden" value="' . $bandid . '" /> <br /> <textarea name="commentBox" id="commentBox"></textarea> <br /> <br /> <input name="Submit" type="submit" class="button" value="Submit" /> </form>'; } else { echo '<h2>Please log in to post a comment.</h2>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/#findComment-254948 Share on other sites More sharing options...
kenrbnsn Posted May 17, 2007 Share Posted May 17, 2007 The value of the variable $bandid is not available within the function unless you declare in global inside the function (not recommended) or pass it as an argument. If the value comes from one of the superglobal arrays, you can use the array reference here. (edit: beaten to it!) Ken Quote Link to comment https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/#findComment-254950 Share on other sites More sharing options...
Chris.P Posted May 17, 2007 Author Share Posted May 17, 2007 Ok thanks guys, obtaining the variable from the $_GET worked like a charm I should have thought of that! Can I ask out of curiosity why you don't recommend declaring a global? Quote Link to comment https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/#findComment-254962 Share on other sites More sharing options...
hitman6003 Posted May 17, 2007 Share Posted May 17, 2007 Aside from cluttering the global namespace, it places vars in a global context needlessly. So, if for example you have another function in your page that has a variable with local scope (to the function) that has the same name, it would be over ridden by the global. In writing your own scripts, you can probably avoid that, but if you decide to use another person's class or function for something, it can cause conflicts. Quote Link to comment https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/#findComment-254965 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.