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">
<head>
<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>
</head>

<body>

<script type="text/javascript">
function valid() {
   cs=document.getElementById("course_syllabus").value;
   var params = "course_syllabus="+cs;
   var url = "chk_username.php";
                $.ajax({
                               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..."){
							   document.getElementById("submit").disabled=false;
							   }
							   else {document.getElementById("submit").disabled=true;}
                               }
                       });
 
}
 
</script>
<form action="#" method="post">
<table>
<tr>
<td>
<?php
 			include('connection.php');
	 		$sql="select course_syllabus from program order by course_code asc;";
	 		$result=mysql_query($sql);

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

      		while($row=mysql_fetch_array($result))
	 	  	{
	  		$b  = $row['course_syllabus'];
           	echo"<option value='$b'>$b</option>";
				}
 		   	echo"</select>";			
?> 
</td>
<td>
<div id="chk" style="color:#009900"></div>
</td>
</tr>
</table>
<input type="submit" id="submit" value="Submit" />
</form>
<br/><br/>
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/>


</body>
</html>

2. chk_username.php


<?php
include("connection.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...";
}
?>

3.connection.php


<?php
$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

Auto-calculate your Splinterlands Assets/Income

Creating a Simple Button for Demo

Splinterlands: Water Deck for Beginners