我有一个动态创建的表。哪个工作得很好。我想采用概述的 php 函数,它在每个单元格中创建按钮(按钮不起作用但确实显示)并使用 javascript 或其他类似语言。我的最终目标是当用户单击按钮时会弹出一个框,用户可以在其中做出选择并提交,就像在 php 函数中一样。我对 javascript 没有太多经验,老实说,我找不到任何关于如何做到这一点的信息。
下面是我的 php 文件。
<?php
session_start();
$tbvbr= $_SESSION['gamecode'];
//user can only edit the row they created
$_GET["editmode"]=$_SESSION['playername'];
//start creating the table
function stripslashes2( $string )
{
if ( get_magic_quotes_gpc() ) {
return stripslashes( $string );
} else {
return $string;
}
}
function display_db_query( $tablename, $header_bool , $border )
{
// find out the number of columns in result
$result = mysql_query( "SHOW FIELDS FROM $tablename" );
while ( $row = mysql_fetch_assoc( $result ) ) {
if ( $row['Key'] == "PRI" )
$primarykey = $row['Field'];
else
$field[$row['Field']] = array( $row['Type'] );
}
if ( isset( $_POST["update$tablename"] ) ) {
$sql = sprintf( "update $tablename SET " );
$sqlfields = array();
foreach( $field AS $k => $v ) {
if ( !empty( $_POST["edit"][$k] ) )
$sqlfields[] = "$k='" . mysql_real_escape_string( stripslashes2( $_POST["edit"][$k] ) ) . "'";
else
$sqlfields[] = "$k = NULL ";
}
if ( count( $sqlfields ) > 0 ) {
$sql .= implode( " , " , $sqlfields ) . " WHERE $primarykey=" . intval( $_POST["updateid"] ) ;
mysql_query( $sql ) OR DIE( mysql_error() );
if ( mysql_affected_rows() > 0 )
print "Updated succesfully<br />";
}
else
echo "No change<br />";
}
// perform the database query
$result_id = mysql_query( "SELECT * from $tablename" )
or die( "display_db_query:" . mysql_error() );
if ( $header_bool ) {
echo "<table width='850' $border align='center' cellpadding='5' cellspacing='1' class='entryTable'>";
echo "<tr class='entryTableHeader'>";
foreach( $field AS $k => $v )
print( "<td><center><b>$k</b></center></td>" );
} else
echo "<table width='850' $border align='center' cellpadding='5' cellspacing='1' class='entryTable'> ";
while ( $row = mysql_fetch_assoc( $result_id ) ) {
print( "<tr>" );
// can edit only edit their row
if ( $_GET["editmode"] AND $_GET["editmode"] == $row[$primarykey] ) {
$editmodeison = true;
echo "<form method=\"post\" action=\"{$_SERVER["PHP_SELF"]}\">";
// this keeps users from editing other users rows
} else
$editmodeison = false;
foreach( $field AS $k => $v ) {
if ($editmodeison)
print( "<td class='content' align='center'><input type=\"button\" name=\"edit[$k]\" value=\"" . ( !empty( $row[$k] )?htmlspecialchars( $row[$k] ) : htmlspecialchars( '' ) ) . "\" /></td>\n" );
else
print( "<td class='content' align='center'>" . ( !empty( $row[$k] )?htmlspecialchars( $row[$k] ) : htmlspecialchars( 'change' ) ) . "</td>\n" );
}
if ( $editmodeison )
print( "<td class='content' align='center'><input type=\"hidden\" name=\"updateid\" value=\"{$row[$primarykey]}\"><input type=\"submit\" name=\"update$tablename\" value=\"update\"></form></td>\n" );
else
print( "<td class='content' align='center'><a href=\"{$_SERVER["PHP_SELF"]}?editmode=" . $row[$primarykey] . "\">change</a></td>\n" );
print( "</tr>\n" );
}
print( "</table>\n" );
}
?>
<HTML><HEAD><TITLE>Products Orderable table</TITLE></HEAD>
<BODY>
<TABLE><TR><TD>
<?php
/* For the following details, ask your server vendor */
$dbhost = "localhost";
$dbuser = "placeholder";
$dbpass = "placeholder";
$dbname = "placeholder";
mysql_connect( $dbhost, $dbuser, $dbpass ) or die ( "Unable to connect to MySQL server" );
mysql_select_db( "$dbname" );
mysql_query( "SET NAMES utf8" );
$table = $tbvbr;
display_db_query( $table, // $global_dbh,
true, "border='2'" );
?>
</TD></TR></TABLE></BODY></HTML>
这是我试图改变和使用 JavaScript 或其他东西的函数
if ($editmodeison)
print( "<td class='content' align='center'><input type=\"button\" name=\"edit[$k]\" value=\"" . ( !empty( $row[$k] )?htmlspecialchars( $row[$k] ) : htmlspecialchars( '' ) ) . "\" /></td>\n" );
else
print( "<td class='content' align='center'>" . ( !empty( $row[$k] )?htmlspecialchars( $row[$k] ) : htmlspecialchars( 'change' ) ) . "</td>\n" );
}
if ( $editmodeison )
print( "<td class='content' align='center'><input type=\"hidden\" name=\"updateid\" value=\"{$row[$primarykey]}\"><input type=\"submit\" name=\"update$tablename\" value=\"update\"></form></td>\n" );
else
print( "<td class='content' align='center'><a href=\"{$_SERVER["PHP_SELF"]}?editmode=" . $row[$primarykey] . "\">change</a></td>\n" );