Hey Guys,


So I'm making a webpage that has a form that is based on the selected section of the website... And I got a few questions/issues.

  • Is it normal that I have to make a different name for a value from a section that needs a javascript (line 4-30 & 124 & 146 & 166 & 183 )
  • is there any way to do it simpeler and not have to add 2 times the same code
  • can you give me some tips on how to simplify & minimalise the amount of code
  • any other recommendation regarding this script?

I'm kind off a newbie in this, so any other tips are welcome!


Thanks! Jelle

<script  type="text/javascript" src="jquery.js"></script>

<script type='text/javascript'>
function Disabchat(val) {
		txtAD = document.getElementById('additionalinfo')
frm=document.forms[1]
if(val=="" && txtAD.value =="") 
{frm.Submit.disabled=true}
else if(val=="" && txtAD.value !="") 
{frm.Submit.disabled=true}
else if(val!="" && txtAD.value =="") 
{frm.Submit.disabled=true}
 else {frm.Submit.disabled=false}
}
</script>

<script type='text/javascript'>
function Disabpro(val) {
	txtAD2 = document.getElementById('additionalinfo2')
frm=document.forms[2]
if(val=="" && txtAD2.value =="") 
{frm.Submit.disabled=true}
else if(val=="" && txtAD2.value !="") 
{frm.Submit.disabled=true}
else if(val!="" && txtAD2.value =="") 
{frm.Submit.disabled=true}
 else {frm.Submit.disabled=false}
}

</script>

<script type='text/javascript'>
function show(part) {
document.getElementById('partchat').style.display='none';
document.getElementById('partprofile').style.display='none';
document.getElementById(part).style.display='block';
} </script>

<SCRIPT type="text/javascript">
<!--
/*
Credits: Bit Repository
Source: http://www.bitrepository.com/web-programming/ajax/username-checker.html
*/

pic1 = new Image(16, 16);
pic1.src="images/loader.gif";

$(document).ready(function(){

$("#username").change(function() { 

var usr = $("#username").val();

if(usr.length >= 2)
{
$("#status").html('<img src="images/loader.gif">&nbsp;Checking availability...');

    $.ajax({
    type: "POST",
    url: "checkusername.php",
    data: "username="+ usr,
    success: function(msg){  

   $("#status").ajaxComplete(function(event, request, settings){ 

	if(msg == "Username doesn't exist")
	{
		$("#username").removeClass('object_ok'); // if necessary
		$("#username").addClass("object_error");
		$(this).html(msg);
	}
	else
	{

		        $("#username").removeClass('object_error'); // if necessary
		$("#username").addClass("object_ok");
		$(this).html('&nbsp;<img src="images/tick.gif">');
	}  

   });

 } 

  }); 

}
else
	{
	$("#status").html('<font color="red">' +
'The username should have at least <strong>2</strong> characters.</font>');
	$("#username").removeClass('object_ok'); // if necessary
	$("#username").addClass("object_error");
	}

});

});

//-->

</SCRIPT>
<form>


<p>Section of website<br />

<? // Section declaration ?>
  <input type="radio" name="section" id="chat" value="chat" onclick="show('partchat');"  checked="checked"><label for='chat'> Chat</label>
  <input type="radio" name="section" id="profile" value="profile" onclick="show('partprofile');"><label for="profile"> Profile</label></form>
</p>

<? // end section decleration ?> 
<? // begin reason options ?>
<div id='partchat' style='display:block;'>
<form onsubmit="return validateFormOnSubmit(this)" action="reportsubmit.php" method="post">

  Username: <br /><input type="text" name="username" tabindex="2" id="username" /><div id="status"></div><br />

  <input type="hidden" name="eesection" id="chat" value="chat"></input>


Reason: <br  />
<select name="reason" onChange="Disabchat(this.value)">
	<option value="">Please select an option</option>
	<option value="abusive">Abusive behavior towards users/staff</option>
    <option value="c2c">Request for cam</option>
    <option value="predator">User is a predator</option>
    <option value="porn">Pornographic content</option>
    <option value="agelimit">Under/Overage</option>
    <option value="personal">Posting personal information (msn, skype, etc)</option>
    <option value="floodspam">User was spamming/flooding</option>
	<option value="hacking">Hacking threat</option>
    <option value="block">Blocking a moderator</option>
    <option value="impersonating">User is impersonating a moderator</option>
    <option value="multiple">User has multiple accounts</option>
    <option value="other">Other, please fill in additional information</option>
    </select> 
  
<br /><br />
      No IP Address has to be provided, this IP Address is recorded automaticaly.
  <br />
    <br />

Additional information: <br />
  <textarea name='additionalinfo' cols='30' rows='5' id='additionalinfo' tabindex='3' onchange="Disabchat(this.value)"></textarea>
  <br />
  <br />   
  Action: <br /><input type="radio" name="action" value="freeze" id="freeze"><label for="freeze">Freeze</label></input><input type="radio" name="action" value="kick" id="kick"><label for="kick">
  Kick</label></input>
<br/>
  
    <br />
  <input type="checkbox" name="requestban" id="requestban"/><label for="requestban">Request ban</label> <br />
  
<input type="Submit" name="Submit" value="Submit" disabled="true"></p>
</form>
</div>
  
  <div id='partprofile' style="display:none">
  <form name="frm" action="reportsubmit.php" method="post" onsubmit="return validateFormOnSubmit(this)" >

  Username: <br /><input type="text" name="username" tabindex="2" id="username" /><div id="status"></div>
<input type="hidden" name="eesection" id="profile" value="profile"></input>
</p>Reason: <br  />
<select name="reason2" onChange="Disabpro(this.value)">
	<option value="">Please select an option</option>
    <option value="fakepic">Fake picture</option>
    <option value="forumpost">User made a bad post on forum (fill in additional information)</option>
    <option value="multiple">User has multiple accounts (fill in additional information)</option>
    <option value="agelimit">Under/Overage</option>
    <option value="personal">Posting personal information (msn, skype, etc)</option>
    <option value="behavior">User behavior is not good (fill in additional information)</option>
    <option value="other">Other, please fill in additional information</option>
    </select> 
    <br />
<br />
      No IP Address has to be provided, this IP Address is recorded automaticaly.<br />

<br />

Additional information: <br />
  <textarea name='additionalinfo2' cols='30' rows='5' id='additionalinfo2' tabindex='3' onChange="Disabpro(this.value)"></textarea>
  <br />
  <br />

  
  
<input type="Submit" name="Submit" value="Submit" disabled="true"></p>
</form>
  </div>

<?
	if (isset($_POST['Submit'])) { 
	
		if(isset($_POST["requestban"])) {
		$requestban = "1";
		} elseif(!isset($_POST["requestban"])){
		$requestban = "0";
	}
	if ($_POST['username'] == "") {
echo "
   <script type='text/javascript'>
       alert(\"Forgot to fill in username!\");
   </script>
";
		die('bye');} else {
	if (isset($_SESSION['emoderator'])){

$esection = $_POST['eesection'];
$euser = $_POST['username'];
$ereason = $_POST['reason'];
if ($esection = "chat") {
$ecomment = $_POST['additionalinfo'];
} elseif ($esection ="profile") {
	$ecomment = $_POST['additionalinfo2'];
}

$eaction = $_POST['action'];

include 'db.php';
	 
						 
	

//Start UserID lookup
// Look up UserID & if this user can be reported (moderators can't be reported for now)
// Or if the user has been banned already, it will not work either.


$getuserid = "SELECT username,gid,block,id FROM jos_users WHERE username = '".addslashes($euser)."'";
$getuseridresult = mysql_query($getuserid);
$rowuser = @mysql_fetch_array ($getuseridresult);
	$username = $rowuser['0'];
	$usergid = $rowuser['1'];
	$userblock = $rowuser['2'];
	$userid = $rowuser['3'];

					if ($usergid >= 19) {
echo "
   <script type='text/javascript'>
       alert(\"You can't report fellow moderators or higher staff!\");
   </script>
";
					die();
					} 


    
  	$getuserip = mysql_query("SELECT userip FROM jos_comprofiler_plug_iplog WHERE userid = '$userid' ORDER BY DATE_FORMAT(logdate, '%Y-%m-%d %H:%m:%s') DESC LIMIT 1");
	$rowuserip = @mysql_fetch_array ($getuserip);
	$userip = $rowuserip['0'];
	

//Start Reporting
			$emod= $_SESSION['emoderator'];
$querykick = "INSERT INTO modsite_reports (websitepart, moderator, user, reason, additionalinfo, action, ipaddress, rqban) VALUES('$esection','$emod','$username','$ereason','$ecomment','$eaction','$userip','$requestban')";

if (!mysql_query($querykick))
  {
  die('Error: ' . mysql_error());
  }

echo "
   <script type='text/javascript'>
       alert(\"Report added!\");
   </script>
		";

mysql_free_result();
mysql_close();
 
	}
	}
	}
	
//End Reporting
	
?>

Sorry mate, too busy to go through your whole thing but a few pointers just from looking at lines 4-30:
1. your javascript code lines should all end in semicolons like your php
2. you would probably have an easier time of things using jquery (as long as you make sure your form fields have both names and id's
3. your logic needs to be a bit cleaner possibly using && and || (or)
eg:

function Disabchat(val) {		
	txtAD = document.getElementById('additionalinfo')
	var frm=document.forms[1]
	var txtAD = frm.additionalinfo;
	frm.Submit.disabled=false;
	if(val==""||txAD.value==""){frm.Submit.disabled=true;}	
	}

4. You have jquery so you should use it rather than document.getElementById
5. do not make 'show' a function name - it is a reserved word.
example using lines 32-37

<script type='text/javascript'>
	function showpart(part) 
		{
			$("#partchat").fadeOut(0);
			$("#partprofile")fadeOut(0);
			$("#"+part).css({'display':'block'});
			} 
	</script>

That's all I have time for for now. Good luck.

Thanks man! This is a real help!

Oops. The first script should be more like this:

function Disabchat(val) {
	var frm=document.forms[1];
	var txtAD = frm.additionalinfo;	
	frm.Submit.disabled=false;
	if(val==""||txAD.value==""){frm.Submit.disabled=true;}		}
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.