Jump to content

Internal Query string


hp1

Recommended Posts

I want to paste text into a text area based on whether the user clicks a linkI have:

<?php$string="Some text";if ($_GET['paste']){$pastestring=$string;}else{$pastestring='';}?><form  etc...><textarea name='admin' rows='10' cols='65'><?php echo $pastestring; ?></textarea><p/><a href="#paste&paste=1"> //or something like this?<span style="border:1px solid white;background-color:black;color:white;padding:1px;">Paste Code</span></a></form>

There is more to the form and I am using an external file to process it once its submitted. I just want the user to be able to paste text into the textarea by clicking the 'button'. Can this be done without reloading the page? The above doesn't work (tho i'm not surprised).

Link to comment
Share on other sites

If you want to do something in the browser (i.e. after the page loads but before going back to the server), you need to use JavaScript. Here's a full HTML page for demonstration...EDIT: Woops, post conflict. But note how this avoids AJAX:

<?php$string="Some text";if ($_GET['paste']){	$pastestring=$string;} else {	$pastestring='';}?><html>	<head>		<title></title>		<script type="text/javascript">			function paste(){				document.getElementById('admin').value += <?php echo $pastestring; ?>;			}		</script>	</head>	<body>		<form>			<textarea id='admin' rows='10' cols='65'></textarea>			<br />			<span style="border:1px solid white;background-color:black;color:white;padding:1px;" onclick="paste()">Paste Code</span>		</form>	</body></html>

Link to comment
Share on other sites

MMm its taken me a year to get to grips (or not) with PHP. Not sure i'm ready for Ajax yet. Is there a recognized method for dynamically completing textareas?Edit: jes, thanks for that. that looks simple enough, i'll give it a go.

Link to comment
Share on other sites

Note: I forgot to quote the PHP output in the JavaScript, so you'll need to do that.
Er...sorry my Javascrit knowledge is limited. What do you mean? Also, will the onClick atribute work without <a href="etc..> How will the <span> recognize a mouse click?Ta
Link to comment
Share on other sites

function paste(){	document.getElementById('admin').value += '<?php echo addslashes($pastestring); ?>';}

EDIT: But now all single-quotes must be escaped in the PHP output. Hmm...EDIT2: addslashes fixes that. But now you don't need that GET parameter; do you still need the PHP?

Link to comment
Share on other sites

function paste(){	document.getElementById('admin').value += '<?php echo addslashes($pastestring); ?>';}

EDIT: But now all single-quotes must be escaped in the PHP output. Hmm...EDIT2: addslashes fixes that. But now you don't need that GET parameter; do you still need the PHP?

No, the the string is returned earlier on in the script. The GET was there purely to send a variable so I could use it as above.I've added a cursor:pointer style to the span. Do i need to add any other javascript other than the above in the <head> and the onclick attribute?Edit: when I hover the mouse over the 'button' there isn't the usual java script:onlick() in the notification area of the browser
Link to comment
Share on other sites

Sorry, I forgot to answer your question earlier. Yes, any HTML element will receive an onclick event.If you don't need the PHP, just use JavaScript by itself:

function paste(){	document.getElementById('admin').value += 'Some text';}

No, that's all the JS you'll need for this.EDIT: That's because the browser doesn't show the URL for onclick. If you want it to, use this:

<a href="java script: paste();">Paste Code</a>

(It can be styled like the SPAN.)

Link to comment
Share on other sites

A h perhaps I didn't explain myself very well. The $string is the contents of a file that will change so the 'some text' has to be dynamic.Thanks for your patience, i'll have a fiddle and see how i get on.Edit: Fiddle has failed. Nothing happens. I've replaced the $pastestring in the javascript with the actual $string returned from the text file.I'll check back tomorrow (today!, bedtime). Thanks for all the help.

Link to comment
Share on other sites

I don't understand what the problem is... Could I get your code?
This is all for a basic admin panel. The user has to login to access these functions. This specific part is to allow a user to edit the contents of his/her home page. I wanted to give them the option to paste the old code into the textbox to edit it. Once the new code is in the textarea they can see what it will look like (Show Me) or just submit it to the server (Go Live).
$file = $_SERVER['DOCUMENT_ROOT'] . "/indexhome.php";$contents = file($file); $string = implode($contents);

<form action='index.php?page=adminaction' method='post'><textarea name='admin' rows='10' cols='65'></textarea><p/><input name='submit' type='submit' value='Go Live'/> <input name='test' type='submit' value='Show Me'/><span style="border:1px solid white;background-color:black;color:white;padding:1px;cursor:pointer" onclick="paste()">Paste Code</span></form>

I also have this in the <head>

<script type="text/javascript">function paste(){document.getElementById('admin').value += '<?php echo addslashes($string); ?>';}</script>

Link to comment
Share on other sites

No, PHP ignores ID, but you can use both.EDIT: By the way, you probably want to take the + out of the JavaScript. See what it does when you click the button a second time?

Link to comment
Share on other sites

No, PHP ignores ID, but you can use both.EDIT: By the way, you probably want to take the + out of the JavaScript. See what it does when you click the button a second time?
MMm still nothing. If I do a view source of the page there is nothing between the single quotes in the Javascript where you would expect the string to have been echoed. Is this because we are calling the variable in the <head> before it has been returned by the file() function?
Link to comment
Share on other sites

Is there any way to write the javascript into the element itself instead of in the head?
Edit: Ok I managed to get the PHP string to echo out in th javascript but I'm thinking the reason why it doesnt work is because the javascript is making use of the 'value' attribute which is fine for <input> but not for <textarea>?
Link to comment
Share on other sites

Textareas have the value attribute. What does your code look like now?

Link to comment
Share on other sites

Textareas have the value attribute. What does your code look like now?
I've been fiddling after doing a bit of googling:
<script type="text/javascript">function paste(){document.myform.formtextarea.value += '<?php echo addslashes($string); ?>';}</script>

This doesn't work either! This is now in the body rather than he head.

<form name='homepage' action='index.php?page=adminaction' method='post'><textarea id='admin' rows='10' cols='65'></textarea><p/><input name='submit' type='submit' value='Go Live'/> <input name='test' type='submit' value='Show Me'/><span style="border:1px solid white;background-color:black;color:white;padding:1px;cursor:pointer" onclick="paste()">Paste Code</span></form>

Edit: OK a little progress. This works:

<span style="border:1px solid white;background-color:black;color:white;padding:1px;cursor:pointer" onclick="document.homepage.admin.value='This is a test';">Paste Code</span>

But this doesn't:

<span style="border:1px solid white;background-color:black;color:white;padding:1px;cursor:pointer" onclick="document.homepage.admin.value='\"This is a test\"';">Paste Code</span>

....even though I've escaped the double quotes.Edit 2: Ok it works but only if the string in value="string" is all on one line. That requires removing all line breaks...*sigh*

Link to comment
Share on other sites

Is there any way to write the javascript into the element itself instead of in the head?
Yes, but it's generally considered bad coding practice because it makes a mess.
<span style="border:1px solid white;background-color:black;color:white;padding:1px;cursor:pointer"onclick="document.myform.formtextarea.value = '<?php echo addslashes($string); ?>';">

or

<a style="border:1px solid white;background-color:black;color:white;padding:1px;cursor:pointer" href="java script: document.myform.formtextarea.value = '<?php echo addslashes($string); ?>';">Paste Code</a>

Really, neither your CSS nor your JavaScript should be in the tag, or even in the file, for that matter. Putting them in separate files, and these tags in your HEAD, would work just as well:

<script type="text/javascript" src="yourScript.js"></script><link rel="stylesheet" type="text/css" href="yourStyle.css">

EDIT: Rather than removing your linebreaks, encode them in PHP with this function:

<?phpfunction escapeNewlines(&$string){	$result = true;	switch(PHP_EOL){		case "\r\n":			$escaped = "\\r\\n";			break;		case "\r":			$escaped = "\\r";			break;		case "\n":			$escaped = "\\n";			break;		default:			$escaped = "\\n";			$result = false;	}	$string = preg_replace('/(?:\r\n|\r|\n)/', $escaped, $string);	return $result;}?>

Link to comment
Share on other sites

OK the javascript is now back in the <script> tags. Almost all of my CSS is in a seperate file. When I'm developing new stuff I use in line css because i find it quicker to see changes. Once I'm happy with it I usually assign it a class or id and pop it into the main stylesheet.Thanks for the sample code. I'll give it a try.

Link to comment
Share on other sites

Note that I've edited the code I gave. I'm testing it now. It returns whether a known platform's newline sequence was found in PHP_EOL. (Windows uses \r\n, Mac uses \r, Unix uses \n...)
Thanks. The only issue with stripping out all the newlines is that when its pasted into the Texarea it will be difficult to read.
The point of the above function is that the newlines will be displayed in the textarea but won't disturb the JavaScript. (It outputs '\r\n' or '\r' or '\n', rather than a literal newline, which will be rendered in the textarea as a literal newline.)
OK the javascript is now back in the <script> tags. Almost all of my CSS is in a seperate file. When I'm developing new stuff I use in line css because i find it quicker to see changes. Once I'm happy with it I usually assign it a class or id and pop it into the main stylesheet.
I do the same, and in fact that's why I gave my JavaScript as part of the HTML. But I wanted to be sure that you would remove it later.EDIT: I've edited the code again since that PM.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...