-2

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" ); 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
st15jap
  • 91
  • 1
  • 2
  • 11
  • You can't just take a piece of code that runs on the serverside and that relies on a database, and "convert" it to a language that runs on the clientside ? – adeneo Sep 09 '13 at 02:32
  • ajax is the way to go. – Class Sep 09 '13 at 02:32
  • Thank you for your suggestion Class! can you please give me more insight into what i need to do? – st15jap Sep 09 '13 at 02:37
  • 1
    google it would be a start – Class Sep 09 '13 at 02:42
  • 2
    You're going to want to read some ajax tutorials, specifically how to submit forms through ajax. If you don't already know about [jQuery](http://jquery.com/), it's a framework which helps simplify writing javascript, I recommend you use it. Also, your html shouldn't be uppercase. This answer may help you with ajax http://stackoverflow.com/a/6960586/1599191 – Graham Walters Sep 09 '13 at 02:43
  • Thank you @GrahamWalters and Class for pointing me In the correct direction. – st15jap Sep 09 '13 at 05:48

1 Answers1

-1

Once again i have to give credit to the above people that helped me.

The below code is the answer to my question.

My edited code

if ($editmodeison) 
                         print( "<td class='content' align='center'><input type=\"button\"onClick=\"reply_click(this.name)\" name=\"edit[$k]\"  value=\"" . ( !empty( $row[$k] )?htmlspecialchars( $row[$k] ) : htmlspecialchars( '' ) ) . "\" /></td>\n" ); 

and of course i had to add the scripts to my code as well.

  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

  <script type="text/javascript">
function reply_click(clicked_name)
{
alert(clicked_name);
 }
</script>

Thanks again for pointing me in the correct direction!

st15jap
  • 91
  • 1
  • 2
  • 11