0
<SCRIPT language="javascript">
   function abc(){
   
 <?php 
 $search1=mysql_query("select Lab_credit_hr from courses where Course_Number='$_GET[course]'");
   if (mysql_num_rows($search1) == 0) 
{
?>
document.fm.lab.disabled=true;
<?php
 }?>
  </SCRIPT>

i want to check mysql table row that if column "lab credit hour"= 0 then lab text field disabled help me this code?
and this javascript function call in "course" option drop down menu

5
Contributors
8
Replies
11
Views
5 Years
Discussion Span
Last Post by EvolutionFallen
0

Try this:

<script type="text/javascript">
function abc() {
    <?php
    //Connection
    $search = mysql_query("SELECT lab_credit_hr FROM courses WHERE course_number = '$_GET['course']'");
    $row = mysql_fetch_array($search);
    if($row['lab_credit_hr'] == 0) {
        echo "document.getElementById('id').disabled = true;";
    } else {
        echo "document.getElemenbById('id').disabled = false;";
    }
    ?>
}
</script>

I hope this helps. This is untested

Edited by fobos: n/a

0

You need to create an ajax request, you cannot mix php and js like that, only if you need a static result (like fobos' example).

Edited by pritaeas: n/a

0

Hi jabeen123,

First a couple of points on syntax:
a) You are missing a closing } . You open one on line 2 and one on line 7, but only close one on line 11.
b) You need quotation marks around the word course in $_GET[course] . You may want to change the whole statement to

$search1=mysql_query("select Lab_credit_hr from courses where Course_Number='" . $_GET['course'] . "'");

Now, can you explain your problem in a little more detail? I'm not certain I understand. When does abc() get called? Is it when you click on different <option>s in a <select> element, or is it when a link is clicked? I'm trying to clarify why you need to use JavaScript.

0

I can't see anything really wrong with the code. You can echo out js from php based on php processing. That's not an issue.

However, using php via js once a page has loaded needs AJAX. You can't get around this.

So if you're running a js function in response to say, a change in a select dropdown - this can be run in JUST js, if all return values are known before hand. Otherwise, if you need server-side intervention, e.g. data from a database, you need to use AJAX.

0

iam trying your code but it will disable all conditions if row lab 0 or 1 or 2.All conditions disable text field ? why
And i m trying also ajax code but also disabled all condtions??

<select name="course" id="course" onChange="getCourse(this.value);"/>
           <option>Select courses <?php echo $option2 ?></option>
         </select
function getCourse(str)
{
if (str=="")
  {
  document.getElementById("labdiv").innerHTML="";
  return;
 }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
   xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("labdiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","findcourse.php?course="+str,true);
xmlhttp.send("lab=" + document.getElementById("lab").value);
}
<head>

<link href="mystyle.css" rel="stylesheet" type="text/css">
</head>
<SCRIPT language="javascript">
   function abc(){
<?php
$name=$_GET['course'];

$link = mysql_connect("localhost","root",""); //changet the configuration in required
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("result_system_db",$link);
$query="select Lab_credit_hr from courses where Course_Number='$name'";

if($row=mysql_fetch_array($query)==0)
 
{

 echo "document.getElementById('lab').disabled = true";

    }
mysql_close($link); ?>}
</SCRIPT>
0
if($row=mysql_fetch_array($query)==0)

You're comparing the returned array to 0. I believe you should be doing

$row=mysql_fetch_assoc($query);
if( $row['Lab_credit_hr'] == 0 ) {
     // disable field
}
0

no work this code so far.
when i selected course option text field not disabled

<head>
<script type="text/javascript">
function abc() {
    <?php
    include_once("connect.php");
    $query = mysql_query("SELECT Lab_credit_hr FROM courses WHERE Course_Number = '$_GET[course]'");
	while($row = mysql_fetch_array($query))
	{
    
    if($row["Lab_credit_hr"] == 0) {
        echo "document.getElementById('lab').disabled = true;";
    } else {
        echo "document.getElemenbById('lab').disabled = false;";
    }
	}
    ?>
}
</script></head>
<body>
<?PHP
$option="";
include_once("connect.php");
$result = mysql_query("SELECT * FROM courses");
 
while($row = mysql_fetch_array($result))
{
$course2=$row["Course_Number"];
    $course3=$row["Course_Id"];
    $option2.="<OPTION VALUE=\"$course2\">".$course2.'</option>';
}
?>
      <div align="left"><strong>Course Number</strong>:      </div>
    
	</td>
	     <td><select name="course" id="course" onChange="javascript:abc()">
           <option>Select courses <?php echo $option2 ?></option>
         </select></td>
	   </tr>



</body>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.