I have a table that is created dynamically. Which works just fine. I would like to take the outlined PHP function which creates buttons in each cell(the buttons do not work but do show up) and use JavaScript or another like language. My ultimate goal is when the user clicks the button a box pops up where the user makes a choice and is submitted just like in the PHP function. I do not have a lot of experience with JavaScript and honestly cant find anything on how to do this.
Below is my PHP file:
<?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>
And this is the function I'm trying to change and use JavaScript or something else with:
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" );