Disable Submit/Save Button if Value Exists from MySQL Database

As shown below:

First you need to create a database and name it as "syllabus" then import the sql file from the source code.

Here are the codes below for reference:

1. index.php

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Disable Button If Value Exists</title>

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


<script type="text/javascript">
function valid() {
   var params = "course_syllabus="+cs;
   var url = "chk_username.php";
                               type: 'POST',
                               url: url,
                               dataType: 'html',
                               data: params,
                               beforeSend: function() {
                               document.getElementById("chk").innerHTML= 'checking...'  ;
                               complete: function() {
                               success: function(html) {
                               document.getElementById("chk").innerHTML= html ;
							   if(document.getElementById("chk").innerHTML == "Please proceed..."){
							   else {document.getElementById("submit").disabled=true;}
<form action="#" method="post">
	 		$sql="select course_syllabus from program order by course_code asc;";

            echo"<select name='course_syllabus' id='course_syllabus' style='width:205px' onchange='valid();'>";
            echo"<option value=\"Default\">Select Course Syllabus</option>";

	  		$b  = $row['course_syllabus'];
           	echo"<option value='$b'>$b</option>";
<div id="chk" style="color:#009900"></div>
<input type="submit" id="submit" value="Submit" />
Already Exist<br/>
CSC 121 - Data Structures I<br/>
CSC 130 - System Analysis and DesignI<br/>
IT 106 - Concepts and Applications of RDBMS II<br/>


2. chk_username.php

$cs       = $_POST['course_syllabus'];
$sql      = "SELECT * FROM form where course_syllabus = '$cs' and ins_id='23989'";
$result   = mysql_query($sql);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
                echo "<font style='margin:10px;color: #B70404;'>Already Exists, Select Again.</font>";
} else {
                echo "Please proceed...";


$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "syllabus";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

That's it!

Popular posts from this blog

Add/Subtract Single Digit Number in Assembly Language

Set table width to 100% on PDF export in Datatables

A Better Archive Page for Blogger