Jump to content

[SOLVED] Insert Text Into Text Field


Hard Powered

Recommended Posts

Hello,

 

I have a chatbox on my website, made by me and a friend. I'm only at basic level in PHP.

 

Could someone possibly give me the code so that when a button is pressed, it automatically inserts the text '/hide' into the text field. This will mean my staff don't always have to type '/hide' in when doing their Moderating duties. :)

 

Thanks

Link to comment
Share on other sites

This is a job for javascript - not PHP.

 

Basic idea would be to have this in the header section:

 

<script type="text/javascript>
function hide(){
document.getElementById('textfieldidhere').value = document.getElementById('textfieldidhere').value+'/hide';
}
</script>

 

You'll then have a button like:

 

<input type="button" value="Hide" onClick="hide()" />

 

You may also wish to have the form automatically submitted with javascript. If you google, you'll find plenty of examples. An alterative would be to have javascript do this all in the background, with AJAX.

Link to comment
Share on other sites

Hmm, I can't seem to be able to get that to work.

 

If I give you my code, then could you have a look? I want the hide button to be next to the 'Message:' text and I want it viewable to those with a permission of 1 and higher.

 

Thanks

 

<?php















//header("Location: http://img507.imageshack.us/img507/3341/udipiz3.jpg");















//header("Location: ../chatbox2/index.php");















//exit;















$db = sqlite_open("chdb.db", 0666);















function checkperm() {



global $_COOKIE, $db;







$str=explode("@", $_COOKIE['rszcbx']);







$id=strtolower(eatquotes(stripslashes($str[0])));



$pass=strtolower(eatquotes(stripslashes($str[1])));







$res=sqlite_query($db, "SELECT * FROM users WHERE id='$id'");



$res=sqlite_fetch_array($res);







//	print_r($res);







if($res['pass'] == $pass) { return $res['status'];}



else { return -1; }







}



$permission = checkperm();











function eatquotes($str) {















$str=str_replace("<", "<", $str);















$str=str_replace(">", ">", $str);















$str=str_replace("'", "&#39;", $str);















$str=str_replace("\"", """, $str);















return $str;















}































function checklogin() {















global $_COOKIE, $db;































$str=explode("@", $_COOKIE['rszcbx']);































$id=strtolower(eatquotes(stripslashes($str[0])));















$pass=strtolower(eatquotes(stripslashes($str[1])));































$res=sqlite_query($db, "SELECT * FROM users WHERE id='$id'");















$res=sqlite_fetch_array($res);































//	print_r($res);































if($res['pass'] == $pass && $res['name']!="") { return true;} else { return false; }















































}































function getUN() {















global $_COOKIE, $db;































$str=explode("@", $_COOKIE['rszcbx']);































$id=strtolower(eatquotes(stripslashes($str[0])));















$pass=strtolower(eatquotes(stripslashes($str[1])));































$res=sqlite_query($db, "SELECT * FROM users WHERE id='$id'");















$res=sqlite_fetch_array($res);































return(ucfirst($res['name']));































}















function getPW() {















global $_COOKIE, $db;































$str=explode("@", $_COOKIE['rszcbx']);































$id=strtolower(eatquotes(stripslashes($str[0])));















$pass=strtolower(eatquotes(stripslashes($str[1])));































$res=sqlite_query($db, "SELECT * FROM users WHERE id='$id'");















$res=sqlite_fetch_array($res);































return(($res['pass']));































}































































//Ok, ok, check bans. 















/*  // Luls legacy code 















$ip=$_SERVER['REMOTE_ADDR'];















$res=sqlite_query($db, "SELECT * FROM users WHERE ip='$ip'");















$row=sqlite_fetch_array($res);















if($row['status']=='-2') { exit; }















*/































$ip=$_SERVER['REMOTE_ADDR'];















$res=sqlite_query($db, "SELECT * FROM bans WHERE ip='$ip'");















$row=sqlite_fetch_array($res);















if($row['id']=='0') { exit; }































































if(	checklogin()==true ) { $line=getUN(); }































































?><html><style type='text/css'>















body {















background-color: #000000;















margin: 0px;















}















.tinput {















border: 1px solid #999999;















background-color: #000000;















font-family: arial;















font-size: 12px;















color: #FFFFFF;















width: 100%;















}















.iname {















border: 1px solid #999999;















background-color: #222222;















font-family: arial;















font-size: 12px;















color: #FFFFFF;















width: 200px;















}















.ipass {















border: 1px solid #999999;















background-color: #222222;















font-family: arial;















font-size: 12px;















color: #FFFFFF;















width: 200px;















}















.imessage {















border: 1px solid #999999;















background-color: #222222;















font-family: arial;















font-size: 12px;















color: #FFFFFF;















width: 99%;















}















.but {






z-index: 20;








border: 1px solid #999999;















background-color: #222222;















font-family: arial;















font-size: 12px;















color: #FFFFFF;















width: 50px;















}















.tbl {















border: 1px solid #FFFFFF;















background-color: #000000;















font-family: arial;















font-size: 12px;















color: #FFFFFF;















}















.dafrm {















width: 100%; 















height: 245px;















overflow: auto;















}















a {















font-family: arial;















font-size: 12px;















color: #FFFFFF;















}















a.hover {















font-family: arial;















font-size: 12px;















color: #FFFFFF;















text-decoration: underline;















}















a.visited {















font-family: arial;















font-size: 12px;















color: #FFFFFF;















}































</style>















<script type='text/javascript'>


function tag_remove(poststr) {

var target;

//loadim1=document.getElementById("loadim1");

//loadim2=document.getElementById("loadim2");

//loadim1.style.display=""

//loadim2.style.display=""

var xmlHttp;
var strip_str;

try

  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }

catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
{
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
  
    strip_str=xmlHttp.responseText; 

//	loadim1.style.display="none"
//	loadim2.style.display="none"

working=0
xmlHttp = null;

      }
    }
var url = 'tag_strip.php' + "?" + "rand=" + Math.round(1000000000*Math.random())

  xmlHttp.open("POST", url, false);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.send('str='+poststr);
  
  return strip_str;

  }

function copyText(str) {

str = tag_remove(str);
str = escape(str);

if (document.getElementById('flashy')) {
var res = document.getElementById('flashy');
document.getElementById('container').removeChild(res);
 }

var copier = document.createElement('embed');
copier.setAttribute('src', 'clipboard.swf');
copier.setAttribute('FlashVars', 'clipboard='+str);
copier.setAttribute('type', 'application/x-shockwave-flash');
copier.setAttribute('height', '0');
copier.setAttribute('width', '0');
copier.setAttribute('id', 'flashy');

document.getElementById('container').appendChild(copier);

}


var usesound=0 //1 for guitar riff on posting messages 















/*















function postit() {















boxID=document.getElementById("daframe");















msgID=document.getElementById("msg");















usrID=document.getElementById("name");































user=usrID.value















user.replace(/&/, '%26')































mesg=msgID.value















mesg.replace(/&/, '%26')































msgID.value=""















locstr="chat.php?user="+user+"&message=" + mesg















frames['daframe'].location.href=locstr















//setTimeout("frames['daframe'].location.href='chat.php'", 1000)















}*/





working=0

function grab(url, trgid, getstr, poststr) {

var target;

if(trgid!="Nowhere") {


target=document.getElementById(trgid);

//target.innerHTML=""

}


//loadim1=document.getElementById("loadim1");

//loadim2=document.getElementById("loadim2");

//loadim1.style.display=""

//loadim2.style.display=""

var xmlHttp;

try

  {

  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();

  }

catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
{
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
if (trgid != "Nowhere") {
    target.innerHTML=xmlHttp.responseText; }

//	loadim1.style.display="none"
//	loadim2.style.display="none"

working=0
xmlHttp = null;

      }
    }
url=url + "?" + getstr + "&rand=" + Math.round(1000000000*Math.random())

  xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.send(poststr);

  }



function postit() {































passID=document.getElementById("pass");















msgID=document.getElementById("msg");















usrID=document.getElementById("name");















dangID=document.getElementById("dang");































name=usrID.value















msg=msgID.value















pass=passID.value















msgID.value=""































name=name.replace(/\&nbsp/g, ' ')















msg=msg.replace(/\&nbsp/g, ' ')















name=name.replace(/\&shy/g, '')















msg=msg.replace(/\&shy/g, '')















msg=msg.replace(/\&thinsp/g, '')















name=name.replace(/\&thinsp/g, '')































name=name.replace(/\&/g, '?')















msg=msg.replace(/\&/g, '%26')















pass=pass.replace(/\&/g, '%26')































name=name.replace(/\#/g, '%23')















msg=msg.replace(/\#/g, '%23')















pass=pass.replace(/\#/g, '%23')































name=name.replace(/\+/g, '%2b')















msg=msg.replace(/\+/g, '%2b')















pass=pass.replace(/\+/g, '%2b')































name=name.replace(/          /g, ' ') // Apologies for the pyramid of randomness















name=name.replace(/         /g, ' ')  // All to stop the impersonators. 















name=name.replace(/        /g, ' ')















name=name.replace(/       /g, ' ')















name=name.replace(/      /g, ' ')















name=name.replace(/     /g, ' ')















name=name.replace(/    /g, ' ')















name=name.replace(/   /g, ' ')















name=name.replace(/  /g, ' ')















if (name==' ') { name = "Space"; }







if (msg==' ') { msg = "Space"; }















//















































grab("chat.php", "daframe", "name="+name+"&pass="+pass+"&msg="+msg, "")















if(usesound==1) { dangID.innerHTML='<embed src="dang.mp3" autostart=true hidden=true></embed>' }















}


function reload() {















if(working==0) {















working=1















grab("chat.php", "daframe", "", "")















}















setTimeout("reload()", 5000)















}































function pop(url,w,h,n){















windork=window.open(url, n, "width=" + w + ", height="+h+", toolbar=no, scrollbars=yes, status=no, resizable=yes");















try{















x=screen.width;















y=screen.height;















windork.moveTo((x/2)-(w/2)-100,(y/2)-(y/4));















windork.focus();















}















catch(e){















}















}























function DoPin(sid, spec) {



	if (!spec) {

		grab("admin.php", "Nowhere", "autoclose=true&pin=" + sid, "") }



	else {

		grab("admin.php", "Nowhere", "autoclose=true&pinhide=" + sid, "") }



	document.getElementById("st"+sid).style.cursor = "wait";



	}	







function DoUnPin(sid, spec) {



	if (!spec) {

		grab("admin.php", "Nowhere", "autoclose=true&unpin=" + sid, "") }



	else {

		grab("admin.php", "Nowhere", "autoclose=true&unpinhide=" + sid, "") }



	document.getElementById("st"+sid).style.cursor = "wait";



}







function DoHide(hid, spec) {

	if (!spec) {

		grab("admin.php", "Nowhere", "autoclose=true&hide=" + hid, "") }



	else {

		grab("admin.php", "Nowhere", "autoclose=true&pinhide=" + hid, "") }



	ChangeBackground("hb"+hid, "im/updown.gif")



}







function DoUnHide(hid, spec) {

	if (!spec) {

		grab("admin.php", "Nowhere", "autoclose=true&unhide=" + hid, "") }



	else {

		grab("admin.php", "Nowhere", "autoclose=true&pin=" + hid, "") }



	ChangeBackground("hb"+hid, "im/updown.gif")



}







function ChangeBackground(tid, img) {







	Chbgid=document.getElementById(tid);







	Chbgid.style.background="url("+img+")"







	Chbgid.style.backgroundRepeat="repeat-x"







}















</script>















<body onload='grab("chat.php", "daframe", "", "");setTimeout("reload()", 2000)'>























<table class='tbl' border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'>















<form name="formit" action='javascript:postit()'>















<tr>















	<td colspan='5'>















		<div id='daframe' class='dafrm'><center> If you see this text for longer then a few seconds, your browser doesn't support AJAX, which is needed for this chatbox to function.















		</div>















	</td>















</tr>















<tr>















	<td width='60' height="23">















		Name: 















	</td>















	<td width='100'>















		<input class='iname' type='text' id='name' name='name' value='<?php echo $line ?>' maxlength='15'> 















	</td>































	<?php 































	if(checklogin()==false) { 















	echo "<td width='70'>















		<!-- 	Password:--> 















	</td>















	<td width='200'>















		 <input type='hidden' id='pass' name='pass' value=''>















	</td>";















	} else {
















$lmbolt="<img src='im/modbolt.gif'>";
$labolt="<img src='im/adminbolt.gif'>";
if ($permission=="1")
echo "<td align='center'>Logged in as: " . $lmbolt . getUN() . " <input type='hidden' id='pass' name='pass' value='" . getPW() . "'></td>";
elseif ($permission>="2")
echo "<td align='center'>Logged in as: " . $labolt . getUN() . " <input type='hidden' id='pass' name='pass' value='" . getPW() . "'></td>";
else
echo "<td align='center'>Logged in as: " . getUN() . " <input type='hidden' id='pass' name='pass' value='" . getPW() . "'></td>";

















	}















































	?>































	<td align='center'> <a href='javascript:pop("conf.php",300,300,"Options")'>Options</a>  

</td>















</tr>















<tr>















	<td colspan='1'>















		Message:















	</td>















	<td colspan='4'>















		<input class='imessage' type='text' id='msg' name='msg' maxlength='250'>















	</td>















</tr>















<tr class='ht'>















	<td colspan='3' align='center' >















		<image src='load.gif' id='loadim1' style='display:none'><input type='submit' value='Post!' class='but' onclick='postit()'>







        <image src='load.gif' id='loadim2' style='display:none'> </td>















	<td colspan='2'> <a href='javascript:pop("smileylist.php",160,400,"Options")'>Smiley List</a>



<?php if ($permission>=1) {

echo "<a target='_blank' href='admin.php'>Mod Cp</a>";

}?>



<a href='javascript:pop("BBCode.htm",300,400,"Options")'>BBCode List</a>

<div style='top:500px;left:300px;z-index:5000;display:inline;visibility:hidden;width:0px;height:0px' id='container'> </div>
	</td>

</tr>















</form>





</table>


<div id='dang' style='display:none'></div>































 

P.S

 

Sorry for the large amounts of space, that will be dealt with another time. :)

 

Thanks

Link to comment
Share on other sites

Meh, this is taking too long. I've done some of it.

 

Would it be better if I gave you the part of the code where I want the hide button to be?

 

Just so you know, the Text area ID is 'msg'.

 

		?>

	<td align='center'> <a href='javascript:pop("conf.php",300,300,"Options")'>Options</a>  

</td>

</tr>

<tr>


	<td colspan='1'>

		Message:

	</td>

	<td colspan='4'>

		<input class='imessage' type='text' id='msg' name='msg' maxlength='250'>

	</td>

</tr>

<tr class='ht'>

	<td colspan='3' align='center' >

		<image src='load.gif' id='loadim1' style='display:none'><input type='submit' value='Post!' class='but' onclick='postit()'>

        <image src='load.gif' id='loadim2' style='display:none'> </td>

	<td colspan='2'> <a href='javascript:pop("smileylist.php",160,400,"Options")'>Smiley List</a>



<?php if ($permission>=1) {

echo "<a target='_blank' href='admin.php'>Mod Cp</a>";

}?>



<a href='javascript:pop("BBCode.htm",300,400,"Options")'>BBCode List</a>

<div style='top:500px;left:300px;z-index:5000;display:inline;visibility:hidden;width:0px;height:0px' id='container'> </div>
	</td>

</tr>

 

Thanks

Link to comment
Share on other sites

I expect you'll want the table altering and what-not?

 

I'm sure you'll be able to position the button for yourself once it's working. And im sure you can get it to only display for a particular group, since you do it further down the page.

 

I did notice an error in the javascript i gave you. Try:

 

<script type="text/javascript">
function hide(){
document.getElementById('msg').value = document.getElementById('msg').value+'/hide';
}
</script>

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.