Reputation: 789
Let's say I have this javascript:
<script language="javascript" type="text/javascript">
function addtext() {
var newtext = document.myform.inputtext.value;
document.myform.description.value += newtext;
}
</script>
and I want to enable it so I can click on some html link that says "add text" and I want the text to be @ . $username . (using PHP to insert the username). So when you click the "add text" link it'll put into the textarea @username. It is hard to visualize and I'm not sure where to put the text and PHP exactly. Thanks!
Textarea:
<form Name ="myform" action="<?$posted = $_POST['description'];
$object->post_tweet($posted); ?>" method="post">
<table align="left" border="0" cellspacing="1" cellpadding="5">
<tr>
<td class="2">
<textarea name='description' class="color" COLS=84 ROWS=2 type="text" id="eBann" name="bannerURL" maxlength="100" size="60" onKeyUp="toCount('eBann','sBann','{CHAR} characters left',140);"></textarea>
<br>
<span id="sBann" class="minitext">140 characters left.</span>
</td>
</tr>
</table><BR><BR><BR>
<p><input type='submit' value='Tweet!' /><input type='hidden' value='1' name='submitted' /> </p>
</form>
This is what I want to do with the link:
<a href="#" onclick="addtext("@'. $twit->user->screen_name .'"); return false>reply</a>'
This gives me an error though (I added the addtext function too).
EDIT: Got it! I had mistakes with ' and " haha wow, stupid mistake. Thanks everybody!
Upvotes: 3
Views: 6091
Reputation: 361984
Ooh, tough one. This is a fantastic mish-mash of HTML, JavaScript, and PHP, isn't it?
<script language="javascript" type="text/javascript">
function addtext(text) {
document.myform.description.value += text;
}
</script>
...
<a href="#" onclick="addtext('@<?php echo htmlspecialchars(addslashes($userName)) ?>'); return false"
>reply</a>
Let's break that down: pretend $userName
is "TeaCast"
. Then the HTML that will get sent to the browser after the <?php ?>
part has executed would look like:
<a href="#" onclick="addtext('@TeaCast'); return false"
>reply</a>
Ah!
Additional notes:
href="#"
sets up a fake link that gives you the hand cursor but doesn't do anything. addslashes()
in the PHP code puts a backslash before any quotes.htmlspecialchars()
call ensures that a user name that contains weird characters like '<'
or '&'
won't mess up your page. Say some evil user who named themselves "<script>alert('haha')</script>"
(yes, their user name is a snippet of HTML).Upvotes: 7
Reputation: 2239
John's answer works fine, but another alternative you can do is store the username in the javascript, such as:
<script language="javascript" type="text/javascript">
var username = '<?php echo htmlspecialchars(addslashes($userName)) ?>';
function addtusername() {
document.myform.description.value += "@" + username;
}
</script>
...
<a href="#" onclick="addusername(); return false">reply</a>
This way, you can use the variable username in the Javascript for other functions later, instead of using a bunch of php.
Upvotes: 0