<!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=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function buttonPress(str)
{
alert(str);

}
</script>
</head>
<?php  session_start();
		error_reporting(E_ALL ^ E_NOTICE);
      include "dbconfig.php";
	 $id=$_SESSION["valid_user"];
 ?>
<body>
<form name="Item Search" method="post" action="search.php"> 
  Item Search:<br> 
  <label> 
  <input name="swords" type="text" size="30" maxlength="30"> 
  </label>  
  <br> 
  <label> 
  <input name="search" type="submit" id="search" value="Search"> 
  </label>
  <a href="logout.php">Logout</a> 
</form> 
 <?php
if (!$_POST['search']){ 
//determining the number of lenght of words

}else{ 
$searchwords = addslashes(htmlspecialchars($_POST['swords'])); 
if (strlen($searchwords) < 2){ 
echo "Your search must contain at least 3 characters. Please try again.<br><input type=button value='Back' onClick='history.go(-1)'>"; 
}else{ 
$words = explode(' ',$searchwords); 
$totalwords = count($words); 
$i = 0; 
$searchstring = ""; 
while ($i != $totalwords){ 
if ($i != 0 and $i != $wordcount){ 
$searchstring .= " and ";; 
} 
$searchstring .= "b_name LIKE '%$words[$i]%'"; 
$searchstring1 .= "b_author LIKE '%$words[$i]%'";
$i = $i + 1; 
} 
$starrow=10;
//getting query
$query = mysql_query("SELECT DISTINCT * FROM book where $searchstring or $searchstring1 LIMIT $starrow"); 
if (mysql_num_rows($query) == 0){ 
echo "No results were found.<br><input type=button value='Back' onClick='history.go(-1)'>"; 
}else{ 

		echo("<FORM METHOD=post id=form3>");
		echo("<H4 ALIGN=left>Books found matching search criteria :</H4><BR>");

		echo("</TABLE>");	
		//code in displaying the table 
        echo("<TABLE width='70%' border='2' cellpadding='3' ALIGN=CENTER>");
			
        echo("<TR bgcolor='B0C4DE'>"); 
        echo("<TH>Sr.No</TH>");
        echo("<TH>Title Of Book</TH>");
        echo("<TH>Author Name</TH>");
        echo("<TH>Number of Copies</TH>");
		echo("<TH>Borrow</TH>");
		echo("</TR>"); 
	
	//searching using title of book
	$count=0;
		while ($row = mysql_fetch_assoc($query)){ 
		
        echo("<TR bgcolor='B0C4DE'>"); 
	    $sr_num=$row['b_sr_num'];
		echo ("<td><input name=data id=data type=text id=data readonly=readonly  value=$sr_num></td>");
		echo ('<td>'.$row['b_name'].'</td>');
		echo ('<td>'.$row['b_author'].'</td>');
		echo ('<td>'.$row['b_avail'].'</td>');
	
		if($row['b_avail']>0)
		{
				if (!$_SESSION["valid_user"])
        {
        // User not logged in, redirect to login page
		echo("<td>Available</td>");
		}else
echo("<td><INPUT TYPE=button  id=show  name=show value=add onClick= buttonPress('$sr_num')></td>");
	
		}else
		echo("<td>No stock of book</td>");
		echo("</TR>");  
}	  
echo("</TABLE>"); 
		
		
		echo("<DIV ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=Next VALUE=Next>");
	    echo("<INPUT TYPE=SUBMIT NAME=Previous VALUE=Previous></DIV>");
		echo("</FORM>");
	
} 
} 
}
  
?>
<form>

<div align="right">
  <table width="200" border="1">
    <tr>
      <td colspan="2"><center>Reserve Books</center></td>
      </tr>
    <tr>
      <td width="92"><?php echo '<script type="text/javascript"> buttonPress(str)	  </script>'; ?></td>
      <td width="92">&nbsp;</td>
      </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      </tr>
  </table>
</div>
</form>
</body>

</html>

can anyone tell me how can i call buttonPress(str) in php when i clicked button in my table it will display its value in a table i know that js is a client side while php is a server side can anyone tell me the correct program ???

Member Avatar for diafol

Firstly, you need to pass a variable to buttonPress. str doesn't exist - wrap it in quotes and it should call correctly.

The other thing is that you haven't attached the script to any event (e.g. button press). I would assume that once you change:

<script type="text/javascript"> buttonPress(str)	  </script>

to

<script type="text/javascript">buttonPress("str")</script>

it will fire every time you refresh/reload.

I don't see why you used php to insert this bit of html/js - there's nothing dynamic about it.

this code is just a test im still trying to figure out what is the best code to use but
@ardav i tried to use your code and it dont work
My idea is to retrieve the value of the button and display it on the table

or you could tell me how to get the value of the button when it clicked

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.