Disabling of hyperlink can be done by the help of Javascript, but can anyone tell me the code, how to disable a hyperlink by the help of php coding. In the web page, it is desired that when a user signs in as an administrator, he could access the hyperlink, otherwise this hyperlink should be visible to other users as disabled hyperlink. Is it possible by the help of php coding? (server side validation). I am new in PHP coding, and do not want to use javascript coding. Is this could be done by the help of html coding or using css. I hope, I shall be soon getting response from the talented coding experts of php and html coding.

Recommended Answers

All 12 Replies

you should use "if/else" statements..

<?php
if(isset($user) && (isset($pass)) { //whatever u want for this
    echo "<td><a href='www.something.com/file.htm'>Click</a><td>";
} else { 
    echo "<td>Click</td>";
}

Hope this helps..

Hi fobos. Thanks for providing the code. It is nice code, but I need a little more information regarding disabling of hyper link. I want that its color would become gray when user clicks on it and it should not perform any action of being redirected to link page. It should give a feeling to user that this is a proper link, but he is not having access right to use it. This can be done by using javascript, but I need server side validation. Hope you would have got my point. Again thanks for your help.

you should use "if/else" statements..

<?php
if(isset($user) && (isset($pass)) { //whatever u want for this
    echo "<td><a href='www.something.com/file.htm'>Click</a><td>";
} else { 
    echo "<td>Click</td>";
}

Hope this helps..

Add 'class' or 'id' for this anchor.

<?php
      if(isset($user) && (isset($pass)) { //whatever u want for this
      echo "<td><a href='www.something.com/file.htm' class='memberlink'>Click</a><td>";
      } else {
      echo "<td>Click</td>";
      }
?>

And you can change in css:

a.memberlink {
           }

Good luck.

If I understand right you'd like a link that doesn't go anywhere if they're not an admin.
Something like

if($user= "admin"){
print '<a href="page.php" class="enabled-link">Admin Section</a>';
}
else{
print '<a href="" class="disabled-link">Admin Section</a>';
}

with something like this in your css

.enabled-link {
color:blue;
text-decoration:underline;
}

.disabled-link{
color:grey;
text-decoration:underline;
}

Hi. i dont know how to post a thread. i really need help with registration form.
any one there please check this my form. its not uploading an image and when i check if the user exists. it is still performing validations.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>REGISTRATION</title>
<style>
  .Overall{  font: 100% Verdana, Arial, Helvetica, sans-serif;
  .errText {
        font-family: Arial;
        font-size: 10px;
        color: #C00;
        text-decoration: none;
        font-weight: normal;
    }

  </style>
</head>

<body class="Overall">

<?php
//REFFERING TO THE DATABASE FORM USED TO CONNECT TO THE DATABASE VTDI LIBRARY
include("includes/myfunctions.php");
if(isset($_GET["cmdreg"]))
    {

        //upload image and also compare username in database

        $fname=trim($_GET["fname"]);
        $lname=trim($_GET["lname"]);
        $uname=trim($_GET["txtuname"]); 
        $unamelength=strlen($uname);
        $pass=trim($_GET["pass"]);
        $repass=trim($_GET["repass"]);  
        $passlength=strlen($pass);
        $gender=($_GET["radgen"]);
        $term=($_GET["radterm"]);
        $country=trim($_GET["optcountry"]);
        $city=trim($_GET["optcity"]);
        $errDob=trim($_GET["date"]);
        $errAddress=trim($_GET["address"]);
        $errEmail= trim($_GET["email"]);
         $file="upload/".$_FILES["coverpage"]["name"];
         $file2= $_FILES["coverpage"]["name"];
         $filetype=$_FILES["coverpage"]["type"];
         echo $filetype;
         $filesize=$_FILES["coverpage"]["size"];
         if(file_exists($file))
            echo "file already exists";
         elseif(($filetype<>"image/pjpeg") && ($filetype<>"image/jpeg") &&($filetype<>"image/bmp"))
            echo "only bmp and jpg files can be uploaded";
         elseif($filesize>500000)
            echo"file size can be a maximum of 500KB";
         else
            {
                move_uploaded_file($_FILES["coverpage"]["tmp_name"],$file);
                $picture=$file2;
                $filename=$_FILES["coverpage"]["name"];
                createthumb($file,"thumbs/",100,$filename);
                $ans=1;
            }


//--------------------- VALIDATING EACH FIELD-----------------------------

        if ($uname=="")
        {

            echo "<font style='color:#F00';> User Name should be entered compulsory</style>";

        }

        elseif($unamelength!=8)
        {           

            echo "<font style='color:#F00';> Username should be eight characters long</style>";
        }

        elseif($pass=="")
        {

            echo    "<font style='color:#F00';> Password should be entered compulsory</style>";
        }

        elseif(is_numeric($pass))
        {

        echo "<font style='color:#F00';> Password should contain numbers and characters</style>";
        }

        elseif($passlength<6)
        {
        echo "<font style='color:#F00';> Password should be greater than 6 characters</style>";
        }
        elseif($repass=="")
        {

            echo "<font style='color:#F00';> Please enter Confirm Password</style>";
        }
        elseif($repass!=$pass)
        {
            echo "<font style='color:#F00';>Confirm password does NOT match password</style>";
        }

        elseif(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_GET["email"]) === 0)
        {

            $errEmail = '<p class="errText">Email must comply with this mask:                                            chars(.chars)@chars(.chars).chars(2-4)</p>';
        }

        elseif(preg_match("/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/", $_GET["date"]) === 0)
        {
          $errDob =  '<p class="errText">Date must comply with this mask: YYYY-MM-DD</p>';   


        }
        elseif($fname=="")
        {

            echo "<font style='color:#F00';><br>First Name should be entered compulsory</style>";
        }
        elseif($lname=="")
        {

            echo "<font style='color:#F00';><br>Last Name should be entered compulsory</style>";
        }



        // Address must be word characters only
        elseif(preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/", $_GET["address"]) === 0)
        {
          $errAddress = '<p class="errText">Address must be only letters, numbers or one of the following ". , : /"</p>'; 
        }




                        elseif($country=="Select Country")
                            echo "<font style='color:#F00';><br>Please ensure that the country  is selected</style>";
                        elseif($country!="Select Country")
                            {   
                                if($country=="Jamaica")
                                $ja_sel="selected"; 
                                elseif($country=="Usa")
                                $usa_sel="selected";
                                elseif($country=="Japan")
                                $jap_sel="selected";
                                if($gender=="")
                            echo "<font style='color:#F00';><br>Select Gender!</style>";
                                    elseif($gender!="")
                                        {
                                        if($gender=="Male")
                                            $maletype="checked";
                                        elseif($gender=="Female")
                                            $femaletype="checked";

                                            if($city=="Select City")
                                        echo "<font style='color:#F00';><br>Ensure that the city is selected!</style>";
                                            elseif($city!="Select City")

                                            if($city=="Kingston")
                                            $kgn_sel="selected";
                                            elseif($city=="St Catherine")
                                            $cath_sel="selected";
                                            elseif($city=="St Mary")
                                            $mar_sel="selected";
                                            elseif($city=="St Andrew")
                                            $and_sel="selected";
                                        if($term=="")
                                    echo    "<font style='color:#F00';><br>Terms and conditions must be checked!</style>";
                                         elseif($term!="")
                                            { 
                                              if($term=="agree")
                                                    $agreetype="checked";
                                             elseif($term=="disagree");
                                            }

                                        }

                            }

//-----------------CONNNECTING TO THE TABLES IN THE DATABASE------------
 if ($ans==1)
{ 


    connectdatabase();

  $result=mysql_query("select * from users where Username='$uname'");

  $numofrows=mysql_num_rows($result);

  if ($numofrows>0){

    echo "<br>Username already exist";
  }
  elseif($numofrows==0)
  {
$sqlinsert="insert into users(Username,Password,Email,Gender,Birth_date,Fname,Lname,Address,City,Country,Image) values('$uname','$pass','$errEmail','$gender','$errDob','$fname','$lname','$errAddress','$city','$country','$picture')";

mysql_query($sqlinsert) or die("cannot insert record");


        echo "Registration Successful";

     }
}
else

            echo "please try again";              

    }

?>
<form name="frmstudent" method="GET" action="#" enctype="multipart/form-data">

<table width="500" border="0" cellpadding="4" cellspacing="0" bordercolor="#33CCCC" bgcolor="#66FFFF">

      <tr align="center" bgcolor="#66CCFF">
        <td colspan="2" bgcolor="#66FFFF">Registration Form</td>
      </tr> 


<tr>
        <td>User Name</td>
        <td><input type="text" name="txtuname" value="<?php echo $uname ?>"  /></td>
</tr>

<tr>
        <td>Create-Password</td>
        <td><input type="password" name="pass" value="<?php echo $pass ?>" /></td>
</tr>

<tr>
        <td>Confirm Password</td>
        <td><input type="password" name="repass" value="<?php echo $repass ?>" /></td>
</tr>    

<tr>
        <td>Email:</td>
        <td>
          <input name="email" type="text" size="50" value="<?php echo $_GET["email"]; ?>">
          <?php  if(isset($errEmail)) echo $errEmail; ?>
        </td>
      </tr>

 <tr>
 <td>Gender</td>
        <td> <input type="radio" name="radgen" value="Male" <?php echo $maletype ?>/>Male</td>
        <td>  <input type="radio" name="radgen" value="Female" <?php echo $femaletype ?>/>Female</td>
 </tr>

<tr>
        <td>Upload Image</td>
        <td><input type="file" name="coverpage"></td>
</tr>

 <tr>
        <td>Birth Date:</td>
        <td>
          <input name="date" type="text" size="16" value="<?php echo $_GET["date"]; ?>">
          <?php  if(isset($errDob)) echo $errDob; ?>
        </td>
  </tr>

 <tr>
        <td>First Name:</td>
        <td>
          <input name="fname" type="text" size="50" maxlength="100" value="<?php echo $fname ?>">

        </td>
      </tr>
  <tr>
        <td>Last Name:</td>
        <td>
          <input name="lname" type="text" size="50" maxlength="100" value="<?php echo $lname ?>">

        </td>
      </tr>

 <tr>
        <td>Address:</td>
        <td>
          <input name="address" type="text" size="50" maxlength="250" value="<?php echo $_GET["address"]; ?>">
          <?php  if(isset($errAddress)) echo $errAddress; ?>
        </td>
</tr>        

<tr>
    <td>City</td> 
     <td><select name="optcity"  size="1">
                                <option value="Select City">- Select City -</option>
                                <option value="Kingston"<?php echo $kgn_sel ?> >Kingston </option>
                                <option value="St Catherine"<?php echo $cath_sel?>>St Catherine </option>
                                <option value="St Mary"<?php echo $mar_sel?> >St Mary </option>
                                <option value="St Andrew"<?php echo $and_sel?> >St Andrew </option>

                            </select></td>
</tr>

<tr>
        <td>Country</td>
        <td><select name="optcountry" size="1">
                                <option value="Select Country">- Select Country -</option>
                                 <option value="Jamaica"<?php echo $ja_sel ?> >Jamaica </option>
                                <option value="Usa"<?php echo $usa_sel?> >USA </option>
                                <option value="Japan"<?php echo $jap_sel?>>Japan </option>

                            </select>
        </td>
</tr>

<tr>
        <td>Terms and Conditions</td>

       <td><input type="radio" name="radterm" value="agree" <?php echo $agreetype ?>/> Agree</td>

        <td><input type="radio" name="radterm" value="disagree" <?php echo $distype ?>/>Disagree</td>

</tr>   

 <tr>
    <td><input type="submit" name="cmdreg" value="REGISTER" /></td>
   <td> <input type="reset" name="cmdreset" value="Clear Details" /></td>

 </tr>        
</form>

</body>
</html>

Click the big "START NEW THREAD" button at the top of the board

Hi, Dear daryII1
I am happy that you got my point regarding disabling of hyperlink. I have not yet checked your code, but before this I was interested to reply you. I hope your code will help a lot. I am using .css style sheet, and I am copying its portion which I think deals with hyperlink color and display. After using your piece of code , Will hyperlink color be gray?, if user places cursor on it and removes, and will the hand sign be there which is displayed when user places cursor on hyperlink. Will this style sheet not disturb disabled hyplerlink appearence?, because it is being used for enabled hyperlinks.
.css code is as under:

a {
color: #FF6600;
font-weight:bold;
text-decoration:none;
}

a:hover {
color: #FFBA00;
}

/* ID Styles */

#navigation a {
font:10px Arial, Helvetica, sans-serif;
color: #26354A;
text-decoration: none;
letter-spacing:.1em;
line-height:16px;
display:block;
padding: 0px 10px;
border-right:1px solid #FF6600;
}

#navigation a:hover {
color:#ffffff;
background: #FF6600;
}

If I understand right you'd like a link that doesn't go anywhere if they're not an admin.
Something like

if($user= "admin"){
print '<a href="page.php" class="enabled-link">Admin Section</a>';
}
else{
print '<a href="" class="disabled-link">Admin Section</a>';
}

with something like this in your css

.enabled-link {
color:blue;
text-decoration:underline;
}

.disabled-link{
color:grey;
text-decoration:underline;
}

Hi, daryll1

Checked your code, it gives desired gray color of disabled link, but when it is clicked, it opens the index.php file, which is not desired, now how this can be avoided.

Hey do you want to post up the part around where you are using it and i'll check it out for you

if($user= "admin"){
print '<a href="page.php" class="enabled-link">Admin Section</a>';
}
else{
print '<a href="" class="disabled-link">Admin Section</a>';
}

Why not just take the href out all together? Like so.
CSS:
.disabled-link {
color: gray;
}

if($user= "admin"){
print '<a href="page.php" class="enabled-link">Admin Section</a>';
}
else{
print '<span class="disabled-link">Admin Section</span>';
}

Or you could just hide the link from anyone not authorized.

if($user= "admin"){
print '<a href="page.php" class="enabled-link">Admin Section</a>';
}

Then the link would not show at all.

No from what I can gather they want the link to show, but not to have a target, so that it is still click able but won't go anywhere, so you need the href in it, not just the colour.

I was just going through one of my previous project and saw some code to disable the LinkButton, HyperLink and ImageButton; after going through it I remembered the issues we had faced with different browsers trying to disable these controls(with proper functionality, styling etc.). Hope posting these functions will help someone.

/// <summary>

/// Disables the link button.

/// </summary>

/// <param name="linkButton">The LinkButton to be disabled.</param>

public static void DisableLinkButton(LinkButton linkButton)

{

    linkButton.Attributes.Remove("href");

    linkButton.Attributes.CssStyle[HtmlTextWriterStyle.Color] = "gray";

    linkButton.Attributes.CssStyle[HtmlTextWriterStyle.Cursor] = "default";

    if (linkButton.Enabled != false)

    {

        linkButton.Enabled = false;

    }



    if (linkButton.OnClientClick != null)

    {

        linkButton.OnClientClick = null;

    }

}

===================
personal trainer certification
business intelligence consultants

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.