Jump to content

Recommended Posts

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();

Link to comment
https://forums.phpfreaks.com/topic/51750-problem-with-variable-in-form-field/
Share on other sites

"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>';
}
}

"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.  ???

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>';
}
}

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.