Jump to content

Why won't this php/javascript work


calabiyau

Recommended Posts

Okay I got the javascript code from someone else and all the comments said it worked.  I am trying to do this for content management site, use the javascript to insert tags into the text area, but all I get is "error on page" warning in bottom left of internet explorer 6.0 and nothing happens to the text area.  Anyone have any ideas?

 


<script type="text/javascript">

function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == ‘0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
</script>

and here is the call within the php page

[code]
<?php

if ($_GET['flag']==1)

{
echo '<form name="softarea" action="'.$current_url.'" method="post">';
echo '<textarea name="softstring" >This is just some sample text</textarea>';

echo '<input type="hidden" name="flag" value="1a"/>';
echo '</form>';
echo '<a href="#" onclick="insertAtCursor(document.softarea.softstring,'."'<h3></h3>'".');">insert h3</a>';
}


 

[/code]

Link to comment
Share on other sites

Okay if you are reading still, maybe this will help to narrow the problem down.  I have put an alert box at the very top of the function in the javascript but when clicking on the link that calls the function, no alert comes up.  So there is something wrong with my function call?

Link to comment
Share on other sites

The only problem that I saw in your original code was an incorrect quote in this line:

else if (myField.selectionStart || myField.selectionStart == ‘0') {

Change the backtick to a single quote:

else if (myField.selectionStart || myField.selectionStart == '0') {

 

Also, although it's not an error, change:

<?php
echo '<form name="softarea" action="'.$current_url.'" method="post">';
?>

to

<?php
echo '<form name="softarea" action="'.$_SERVER['PHP_SELF'].'" method="post">';
?>

 

I made the change of the quote and the code worked fine.

 

Ken

 

Link to comment
Share on other sites

Did it actually update the text area the way it was supposed to?  I made the change that previous poster mentioned by enclosing the first function parameter in escaped quotes and made the change with the single quote error within the script.  Now the alert boxes are coming up to say I have gotten inside the functions but it is still not making any change to the text area.

 

 

Here is the code now:

 

echo '<form id="softarea" action="'.$current_url.'" method="post">';
echo '<textarea id="softstring" >This is just some sample text</textarea>';

echo '<input type="hidden" name="flag" value="1a"/>';
echo '</form>';
echo '<a href="#" onclick="insertAtCursor(\'document.softarea.softstring\','."'<h3></h3>'".');">insert h3</a>';
echo '<a href="#" onClick="testing( );">testing</a>';


Link to comment
Share on other sites

Here's my code. Yes it puts up the text box and clicking the like inserts the text.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title></title>
<script type="text/javascript">

function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
</script>
</head>

<body>
<?php

if ($_GET['flag']==1)

{
echo '<form name="softarea" action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo '<textarea name="softstring" >This is just some sample text</textarea>';

echo '<input type="hidden" name="flag" value="1a"/>';
echo '</form>';
echo '<a href="#" onclick="insertAtCursor(document.softarea.softstring,'."'<h3></h3>'".');">insert h3</a>';
}
?>



</body>
</html>

 

Ken

Link to comment
Share on other sites

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.