hi all,
can we call the parameter like this. And pass the value to javascript..

<?php
        $sql3="select userid from projectassign where projectassign='$projectassign'";
        mysql_error();
        $idresult=mysql_query($sql3);
        $idarry=mysql_fetch_array($idresult);
       ?>                                                   
        <td><select id="prolist" name="projectassign" onchange="return enable(<?php echo $idarry;?>)">//can we do like this.

Recommended Answers

All 10 Replies

Yes,
We can place php code in js, when page is loaded php code executed and place output between js.

But here in this code $idarry returns array, so might be you will not get value in enable function.
Just pass one value not array. e.g.$idarry

yes i am not getting any value.actually by that query i will get 1 or more userid's.so can u say me hw can i pass them...

<script>
  function enable(uid)
  {
  	alert(uid);
  }
  </script>
  <select id="prolist" name="projectassign" onchange="if(this.value!=''){return enable(this.value);}">
  	<option value="">Select User</option>
	<option value="1">User 1</option>
	<option value="3">User 3</option>
	<option value="5">User 5</option>
  </select>

Here ,i have given one example,
In your code hope option tags are dynamicaly created.but here in this example it is static.
try it, you will get idea.

is this u said...correct me if i am wrong..

function enable(list)
        {
            var idlist =list;
            for(var i=0;i<idlist.length;i++)
            {
              alert(idlist[i]);
            }
}

<?php
        $sql3="select userid from projectassign where projectassign='$projectassign'";
        mysql_error();
        $idresult=mysql_query($sql3);
        $idarry=mysql_fetch_array($idresult);
        ?>
     
        <td><select id="prolist" name="projectassign" onchange="return enable(<?php echo $idarry;?>)">

Nope,
NO need to write loop in JS.loop will be only in php.
'this.value' in onchange will pass dropdown's selected user id to JS.

<script>
  function enable(uid)
  {
  	alert(uid+' is selected');
  }
</script>
<?
	$sql3="select userid from projectassign where projectassign='$projectassign'";
	mysql_error();
	$idresult=mysql_query($sql3);
?>
<select id="prolist" name="projectassign" onchange="if(this.value!=''){return enable(this.value);}">
<?
	while($idarry=mysql_fetch_array($idresult))
	{
	 ?><option value="<?=$idarry['userid']?>"><?=$idarry['userid']?></option><?
	}

?>
</select>

Is this right what you need?

actually my problem is...I am having a page with projects in dropdown box.And list of users below that in a table with each having a check box. so we can select a project from dropdown and we can assign users to that project by checking on the check box of the user.
so till now it is working fine..
Now here is my requirement..wen i select a project in the dropdown i should get the users assigned to that project as checked and when i change the project in the drop down the checkbox should be checked according to the users assigned to that project.

$sql3="select userid from projectassign where projectassign='$projectassign'";
     mysql_error();
    $result3=mysql_query($sql3);
    $num1=mysql_num_rows($result3);
    $i=0;
  
    while ($i < $num1)
    {
        $f1=mysql_result($result3,$i,"userid"); 
        echo $f1;        
        $i++; 
    }

here $f1 prints the users who are assigned to the project based on selection from the dropdown box.
so i think u got me..

<script>
  function enable(pid)
  {
  	var users = document.getElemtnBYId('project'+pid).value;
	// split users using "," which will give array say userArray
	for loop for userArray
	{
		// check user's checkbox
		document.getElementBYId('user'+userArray[i]).checked=true;
	}	
  }
</script>
<!--  this is for tracking that project 1 have userid 2,3 and project 2 have userid 1,3.....etc -->
<input type="hidden" name="project1" value="2,3" />
<input type="hidden" name="project2" value="1,3" />

<!-- project drop down -->
<select onchange="enable(this.value);">
	<option value="1">Project 1</option>
	<option value="2">Project 2</option>	
</select>

<!-- user checkbox -->
<input name="" id="user1" type="checkbox" value="" />User 1
<input name="" id="user2" type="checkbox" value="" />User 2
<input name="" id="user3" type="checkbox" value="" />User 3

Here i have posted logic.hidden inputs will have project id as name and userid(concated by commas) as value.
It is static just giving you hint. you can do it for dynamic.
Take a look to this logic..

ok.now here is my problem..Now project 1 is assigned to user1,user2.and project 2 to user3. Now in the drop down wen i select project 1,i need to show that user1 and user2 are checked.Now on changing this project1 to project2 i need user 3 to be checked.

thats what above js enable function will do.
one thing remained was first of all you need to uncheck all checkbox then for loop will check(line # 9) selected project's user checkbox.

To complete the decision suggested from vibhadevit :

// just for clarity ....
//mysql_connect("localhost", "username", "pass") or
//  die("Could not connect: " . mysql_error());
//mysql_select_db("YuorDataBaseName");

//mysql_error();
// I don't know why you do this - mysql_error();
//  If you want to check for errors you have to put this i some logical operator.
//  Like :
//    if( mysql_errno || mysql_error();) {
//         echo mysql_errno($link) . ": " . mysql_error($link). "\n";
//    }

// here must be your colums names and table name
$sql3     = "SELECT userid , projectid, name FROM users";

$idresult = mysql_query($sql3);
$idarry   = array();
$users    = array();

while ($row = mysql_fetch_array($idresult, MYSQL_ASSOC )) {

     $users[$row['userid']] = $row['name'];

      if(isset($idarry[$row['projectid']])) {

          $idarry[$row['projectid']] =  $idarry[$row['projectid']].','.$row['userid'];

      } else {

         $idarry[$row['projectid']] = $row['userid'];
      }
}
?>
<script>
  function enable(field, pid)
  {
    for (i = 0; i < field.length; i++) {
  	   field[i].checked = false ;
    }

    var users       = document.getElementById('project'+pid).value;
    // split users using "," which will give array say userArray
    var userArray = users.split(",");

    for ( i=0; i < userArray.length; i++)
    {
      // check user's checkbox
      document.getElementById('user'+userArray[i]).checked=true;
    }
  }
</script>
<!--// creat a form -->
<form name="myform" method="POST" action=""  id="myform">
<!--  this is for tracking that project 1 have userid 2,3 and project 2 have userid 1,3.....etc -->
<?php
  foreach ( $idarry as $k => $v) {
?>
    <input type="hidden" name="project<?php echo $k?>" id="<?php echo "project".$k?>" value="<?php echo $v?>" />
<?php
  }
?>
<!-- project drop down -->
<select onchange="enable(document.myform.user, this.value);" name="projects">
<?php
  foreach ( $idarry as $k => $v) {
?>
    <option  value="<?php echo $k?>"><?php echo "project".$k?></option>
<?php
  }
?>
</select>

<?php
  foreach ( $users as  $key => $val) {
  echo $val." : ";
?>
    <input type="checkbox" name="user" id="user<?php echo $key?>"/>
<?php
  }
?>
</form>

NOTE: tested, just be careful when rename definitions and names with yours

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.