dongxingchang9345 2014-01-19 16:24
浏览 65
已采纳

PHP-Javascript交互

So I've been attempting to get this page to work correctly for the past few days. As far as the database manipulation and what not it works like a charm. The problem is that it apparently isn't hitting my javascript function to redirect back onto itself. It will instead show a mostly blank page with no $_GET variables in the url which leads me to believe the problem is actually in my php function rather than my javascript function.

function edit_player()
{

try{
    if( isset( $_GET['player_id'] ) )
    {
        if( isset( $_GET['success'] ) && $_GET['success'] )
        {
            echo "<div class='success'> You have successfully edited this players info.</div>" ;
        }
        $playerid = $_GET['player_id'] ;
        $db = honneyconnect( ) ;  // db connection established
        if( mysqli_connect_error() )
        {
            throw new Exception( "Could not connect to the database") ;
        }
        $query = 'select * from roster where player_number ="'.$playerid.'"' ;
        $player = $db->query( $query ) ;
        if( !$player  )
        {
            throw new Exception ( "Query returned zero results" ) ;
        }
        else
        {
            $row = $player->fetch_row() ;
            echo '
            <div class="data_entry">
            <form id="player_info" method="post" action="editplayer.php" enctype="multipart/form-data" />
            <input type="hidden" name="MAX_FILE_SIZE" value="50000000" />
            <input type="hidden" name="player_number" value="'.$row[0].'" />
            <table>
            <tr><td>Player Number:</td><td>'.$row[0].'</td></tr>
            <tr><td>Player Name:</td><td><input type="text" size="50" name="name" value="'.$row[1].'"/></td></tr>
            <tr><td>Player Position:</td><td><input type="text" size="20" name="position" value="'.$row[2].'" /></td></tr>
            <tr><td>Years Skating:</td><td><input type="text" size="3" name="years" value="'.$row[3].'"/></td></tr>
            <tr><td>How You Chose Your Derby Name:</td><td><input type="text" size="100" name="choice" value="'.$row[4].'" /></td></tr>
            <tr><td>Life Outside of Derby:</td><td><input type="text" size="100" name="life" value="'.$row[5].'" /></td></tr>
            <tr><td>Current Photo: </td><td><img class="thumbnail" src="rosterpics/'.$row[6].'"></td></tr>
            <tr><td>Choose a different photo:</td><td><input type="file" name="photo" id="photo" /></td></tr>
            <tr><td>Sponsor:</td><td><input type="text" size="50" name="sponsors" value="'.$row[7].'"/></td></tr>
            </table>
            <input type="submit" value="Submit Data" />
            </form></div>' ;
        }
    }
    else
    {
        if( isset($_POST['player_number']) && isset($_POST['name']) && isset($_POST['position'])
        && isset($_POST['years']) && isset($_POST['choice']) && isset($_POST['life']) )
        {
            if( !file_exists( $_FILES['photo']['tmp_name'] ) || !is_uploaded_file($_FILES['photo']['tmp_name'] ) )
            {
                $db = honneyconnect( ) ;  // db connection established
                if( mysqli_connect_error() )
                {
                    throw new Exception( "Could not connect to the database") ;
                }
                $query = 'update roster set player_name = "'.$_POST['name'].'" ,
                    player_position = "'.$_POST['position'].'", player_years = "'.$_POST['years'].'" , player_choice = "'.$_POST['choice'].'",
                        player_life = "'.$_POST['life'].'" where player_number = "'.$_POST['player_number'].'"' ;
                $player = $db->query( $query ) ;
                if( !$player )
                {
                    throw new Exception( "Query failed" ) ;
                }
                else
                {
                    if( isset($_POST['sponsors']) )
                    {
                        $query = 'update roster set player_sponsor = "'.$_POST['sponsors'].'" where player_number = "'.$_POST['player_number'].'"';
                        $sponsor = $db->query( $query ) ;
                        if( !$sponsor )
                        {
                            throw new Exception( "Failed to update sponsor." ) ;
                        }
                        else
                        {
                            $url = "http://localhost/honeysproject/editplayer.php?player_id=".$_POST['player_number']."" ;
                            echo "<script type='text/javascript'>success_redirect( ".$url." ) ;</script>" ;
                        }
                    }
                    else
                    {
                        $url = "http://localhost/honeysproject/editplayer.php?player_id=".$_POST['player_number']."" ;
                        echo "<script type='text/javascript'>success_redirect( ".$url." ) ;</script>" ;
                    }
                }
            }
            else
            {
                if( $_FILES['photo']['size'] > 50000000 )
                {
                    throw new Exception( "File is to large for this server." );
                }
                if( !move_uploaded_file($_FILES["photo"]["tmp_name"], "C:/wamp/www/HoneysProject/rosterpics/" . $_FILES["photo"]["name"]) )
                {
                    throw new Exception( "There was a problem uploading the file" ) ;
                }
                else
                {
                    $db = honneyconnect( ) ;  // db connection established
                    if( mysqli_connect_error() )
                    {
                    throw new Exception( "Could not connect to the database") ;
                    }
                    $query = 'update roster set player_name = "'.$_POST['name'].'" ,
                    player_position = "'.$_POST['position'].'", player_years = "'.$_POST['years'].'" , player_choice = "'.$_POST['choice'].'",
                        player_life = "'.$_POST['life'].'", player_photo = "'.$_FILES['photo']['name'].'" where player_number = "'.$_POST['player_number'].'"' ;
                    $player = $db->query( $query ) ;
                    if( !$player )
                    {
                        throw new Exception( "Query failed" ) ;
                    }
                    else
                    {
                        if( isset($_POST['sponsors']) )
                        {
                            $query = 'update roster set player_sponsor = "'.$_POST['sponsors'].'" where player_number = "'.$_POST['player_number'].'"';
                            $sponsor = $db->query( $query ) ;
                            if( !$sponsor )
                            {
                                throw new Exception( "Failed to update sponsor." ) ;
                            }
                            else
                            {
                                $url = "http://localhost/honeysproject/editplayer.php?player_id=".$_POST['player_number']."" ;
                                echo "<script type='text/javascript'>success_redirect( ".$url."  );</script>" ;
                            }
                        }
                        else
                        {
                            $url = "http://localhost/honeysproject/editplayer.php?player_id=".$_POST['player_number']."" ;
                            echo "<script type='text/javascript'>success_redirect( ".$url." ) ;</script>" ;
                        }
                    }
                }
            }

        }
    }
}
catch( Exception $error )
{
    echo "<div class='error'>".$error."</div>" ;
    echo $_FILES['photo']['name'] ;
}
}



<html>
<head>
<style>
@import "honeysstyle.css";
</style>
<script type="text/javascript">
function success_redirect( url )
{
    window.location.replace( url ) ;

}
</script>
</head>
<body>
<?php
require( 'function.php' );

draw_masthead();
edit_player() ;


?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • douyan2970 2014-01-19 16:27
    关注
    success_redirect( ".$url." ) 
    
    should be
    
    success_redirect( '".$url." ') 
    

    and why don't you use the header function

    header("Location: $url");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料