Jump to content

php and javascript select multiple boxes


raman

Recommended Posts

I have a page query.html from which one can select multiple names(name=Organism) from drop-down menu and multiple catgories(name=Category) and which pass to the  action attribute que4.php.The code for que4.php is :

<?php
$org=$_POST['Organism'];

$cat=$_POST['Category'];

$con= mysql_connect("localhost.localdomain","root","pichii13");

if(!$con)
  {
    die('Could not connect:'.mysql_error());
  }
else
  {
    echo"<head><script type='text/javascript' >

checked=false;
    function selectAll (field) {
    var aa= document.getElementById('field');
    if (checked == false)
    {
    checked = true
    }
    else
    {
    checked = false
    }
    for (var i =0; i < aa.elements.length; i++) 
    {
    aa.elements[i].checked = checked;
    }
}

</script></head>";
echo "<h2><div align='center'><font color='green'>Results of your query</font></div></h2><body bgcolor='wheat'>";
}
mysql_select_db("Protvirdb",$con);
foreach($_POST['Organism'] as $org){

  foreach($_POST['Category'] as $cat){ 
    $ret=mysql_query("SELECT * FROM cryptovir WHERE Organism='$org' AND Category='$cat'");
    $count=mysql_num_rows($ret);
    if ($count!=0){
      echo"<p> The number of records retrieved by your query is $count</p>";
      echo "<table border='1'><form id='field' action='' method='post'>
      <tr><th><input type='checkbox' name='checkall' onclick='return selectAll(field);'></th><th>Organism</th>
      <th>Category</th>
      <th>Protein</th>
      <th>Brief Description</th>
      </tr>";
    //  echo"<form action='down.php' method='post'>";
      while($row=mysql_fetch_array($ret))
{
  $ort=$row['Organism'];
  $tre=$row['Category'];
  $key=$row['SNo'];
  // echo"<form action='down.php' method='post'>";
  echo "<tr><td><input type='checkbox' name='pro[]'></td>";
       	  echo"<input type='hidden' name='ont' value='$ort'>";

  echo"<input type='hidden' name='catt' value='$tre'>";	  
  
  echo"<td><i>".$row['Organism']."</i></td>";
  echo"<td>".$row['Category']."</td>";
  echo"<td><a href=\"seq2.php?sn=$key\">".$row['Name']."</a></td>";
  echo"<td>".$row['Brief_Description']."</td>";
  echo "</tr>"; 

}
      echo"</table><br>";
      echo"<input type='submit' value='Download as excel file'></form>";
    }
    else
    {
      echo "<p><b> No records were found in the category $cat</b></p>";
    }
  }
}
mysql_close($con);
?>

I select multiple categories in query.html.

Here when I click on the first checkbox with name='checkall' the ones from the first table are selected/deselected but when I check the first checkbox of the second table the ones from second table are not selected/deselected but the ones from the first table only are selected.Why it does not operate like a loop.

 

Also if someone can tell me how I can save the selected data into a variable or array and then allow its download.

 

(edited by kenrbnsn to fix the


tags)

because javascript gets horribly confused when you have 2 id's with the same name, and call use that id in a function. that is what is happening on this line:

      echo "<table border='1'><form id='field' action='' method='post'>

if the loop runs x times, you have x forms named 'field', and javascript only treat the first. to solve add $i = 1; outside your foreach() loop, and increment it (++$i) at the end of each loop. then change this row:

      echo "<table border='1'><form id='field' action='' method='post'>

 

to this:

      echo "<table border='1'><form id='field'".$i." action='' method='post'>

 

and this line:

      <tr><th><input type='checkbox' name='checkall' onclick='return selectAll(field);'></th><th>Organism</th>

 

to this:

      <tr><th><input type='checkbox' name='checkall' onclick='return selectAll(field".$i.");'></th><th>Organism</th>

 

i think that will fix things for you.

Thanks ,that was the right track.

I have now slightly modified my same script ,I want to know how I can pass variables from this to the next php script to allow selective download of the data SELECTED BY THE USER.

code for que4.php:



<?php
$org=$_POST['Organism'];

$cat=$_POST['Category'];

$con= mysql_connect("localhost.localdomain","root","pichii13");

if(!$con)
  {
    die('Could not connect:'.mysql_error());
  }
else
  {
    echo"<head><script type='text/javascript' src='selctall.jsp'>

</script></head>";
echo "<h2><div align='center'><font color='green'>Results of your query</font></div></h2><body bgcolor='wheat'>";
}
mysql_select_db("Protvirdb",$con);

echo "<table border='1'><form id='field' action='down1.php' method='post'>
      <tr><th><input type='checkbox' name='checkall' onclick='return selectAll(field);'></th><th>Organism</th>
      <th>Category</th>
      <th>Protein</th>
      <th>Brief Description</th>
      </tr>";
$arry=array();
foreach($_POST['Organism'] as $org){
  foreach($_POST['Category'] as $cat){
    $ret=mysql_query("SELECT * FROM cryptovir WHERE Organism='$org' AND Category='$cat'");
    $count=mysql_num_rows($ret);
    $counter+=$count;
    if ($count==0){
      array_push($arry,$cat);
    }
  }
  echo "<div>No records found for categories: ";
  for ($i=0;$i<count($arry);$i++){
    echo"$arry[$i] ";
  }
  echo "for <i>$org</i></div>";
}
echo"<p>The total number of records retrieved by your query is $counter.</p>";

foreach($_POST['Organism'] as $org){
  foreach($_POST['Category'] as $cat){
    $ret=mysql_query("SELECT * FROM cryptovir WHERE Organism='$org' AND Category='$cat'");
    $count=mysql_num_rows($ret);
    if($count==0){

    }else {
      //echo"<p> The number of records retrieved by your query is $count</p>";
 
      while($row=mysql_fetch_array($ret))
{
  $ort=$row['Organism'];
  $tre=$row['Category'];
  $key=$row['SNo'];
  // echo"<form action='down.php' method='post'>";
  echo "<tr><td><input type='checkbox' name='pro[]'></td>";
  echo"<input type='hidden' name='ont' value='$ort'>";

        echo"<input type='hidden' name='catt' value='$tre'>";  
 
  echo"<td><i>".$row['Organism']."</i></td>";
  echo"<td>".$row['Category']."</td>";
  echo"<td><a href=\"seq2.php?sn=$key\">".$row['Name']."</a></td>";
  echo"<td>".$row['Brief_Description']."</td>";
  echo "</tr>";
         
}
    }
  }
 
}
echo"<input type='submit' value='Download as excel file'></form></table><br>";


mysql_close($con);
?>

 

code for download script to which I wish to pass the variables from here.

<?php

$kyu=$_POST['pro'];

 

$conn= mysql_connect("localhost.localdomain","root","pichii13");

if(!$conn)

  {

    die('Could not connect:'.mysql_error());

  }

mysql_select_db("Protvirdb",$conn);

 

// file name to be appear in output name. User can change their file name

// but this will give him a option for file name.

$file = 'testExcelFile.xls';

// start buffring

ob_start();

// sample dynamically generated data

echo '<table border="1"> ';

echo '<tr><th>Category</th><th>Protein</th><th>Brief Description</th></tr>';

foreach($_POST['pro'] as $kyu){

  $re=mysql_query("SELECT * FROM cryptovir WHERE SNo='$kyu'");

  while($row=mysql_fetch_array($re))

    {

    echo"<tr>";

    echo"<td>".$row['Category']."</td>";

    echo"<td>".$row['Name']."</td>";

    echo"<td>".$row['Brief_Description']."</td>";

    echo "</tr>";

   

    }

 

}

mysql_close($conn);

?>


 

I GET ERROR :

Warning: Invalid argument supplied for foreach() in /usr/local/apache2/htdocs/protvirdb/down1.php on line 19

try 2 things:

comment out the first line of code on the second script you posted (the download page):

$kyu=$_POST['pro'];

 

since you later (in the foreach) do the same thing.

 

also try to add a condition that will only enter the foreach loop if $_POST['pro'] isn't empty (the foreach requires that $_POST['pro'] contain at least 1 item).

This is what the problem is .that I want to pass the values of selected checkboxes to the dorm down1.php where I want to allow the download of the same table as excel file.However the value $key against the checkbox which is the primary key in my mysql database is not passing on the next form down1.php.Please advise how I pass the entire set of $keys against selected checkboxes shown on que4.php to retrieve the same information again on down1.php.

 

 


while($row=mysql_fetch_array($ret))

  {

    $ort=$row['Organism'];

    $tre=$row['Category'];

    $key=$row['SNo'];

    // echo"<form action='down.php' method='post'>";

    echo "<tr><td><input type='checkbox' name='pro[]' value='$key'></td>";

    echo"<input type='hidden' name='ont' value='$ort'>";

 

            echo"<input type='hidden' name='catt' value='$tre'>";   

   

    echo"<td><i>".$row['Organism']."</i></td>";

    echo"<td>".$row['Category']."</td>";

    echo"<td><a href=\"seq2.php?sn=$key\">".$row['Name']."</a></td>";

    echo"<td>".$row['Brief_Description']."</td>";

    echo "</tr>";

       

  }

    }

  }

 

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.