0

我在表中有一组用于更新密码的代码,这里我使用 AJAX 更新密码并在相应执行时获取弹出屏幕。在我的应用程序中使用该代码时,它正在正确执行,但我没有得到输出(密码未更新到表中)。我也没有收到任何错误。

html页面代码

<html>
<head>
<div><IMG src="karvy.jpg" ALT="image"></div>
<script language="javascript" type="text/javascript">

//Browser Support Code


var xmlHttp;
function fetch_javaScript(usr,oldpassword,newpassword,repassword)
        {
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
                {
                alert ("Browser does not support HTTP Request");
                return;
                }
        var usr = document.getElementById('usr').value;
        var oldpassword = document.getElementById('oldpassword').value;
        var newpassword = document.getElementById('newpassword').value;
        var repassword = document.getElementById('repassword').value;


        var url="changepwd1.pl";
        url=url+"?usr=" + usr;
        url=url+"&oldpassword=" + oldpassword;
        url=url+"&oldpassword=" + newpassword;
        url=url+"&repassword=" + repassword;
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.open("POST",url,false);
        xmlHttp.send(null);
        }

function stateChanged()
        {
                if (xmlHttp.readyState==4 && xmlHttp.status==200)
                {
                    document.getElementById("ajaxDiv").innerHTML=xmlHttp.responseText;
                }

        }

function GetXmlHttpObject()
        {
        var xmlHttp=null;
        try
        {
 // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
 //Internet Explorer
                try
                {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
                 catch (e)
                {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
        }
        return xmlHttp;
        }
</script>
</head>

<body bgcolor="#D2B9D3">
<form name='myForm'>
<center><table> <tr><td>
<div style="width:400px;height:280px;border:3px solid black;">
<center><h4>Please Enter your Password's</h4>

<p><b>User Name</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<INPUT TYPE=text NAME="usr" id = "usr" size = "15" maxlength = "15" tabindex = "1"/></p>
<p><b>Old Password:</b>&nbsp&nbsp&nbsp<INPUT TYPE=PASSWORD NAME="oldpassword" id = "oldpassword" size = "15" maxlength = "15" tabindex = "1"/></p>
<p><b>Password:</b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<INPUT TYPE=PASSWORD NAME="newpassword" id = "newpassword" size = "15" maxlength = "15" tabindex = "1"/></p>
<p><b>Re-Password:</b>&nbsp&nbsp&nbsp<INPUT TYPE=PASSWORD NAME="repassword" id = "repassword" size = "15" maxlength = "15" tabindex = "1"/></p>
<input type="submit" id="val" value="Submit" align="middle" method="POST"  onclick="fetch_javaScript()"/><INPUT TYPE="reset" name = "Reset" value = "Reset"/>
<p>Main Menu <A HREF = login.pl>click here</A></p>
</center>
</div>
</td></tr></table></center>
</form>

<div id='ajaxDiv'>Your result will display here</div>
<!--<div id="myDiv"></div>-->
</body>
</html>

Perl 代码

#!/usr/bin/perl

use DBI;
use strict;
use CGI;
my $cgi = CGI->new;

print $cgi->header;
print $cgi->start_html("Password Form");
print "Content-type: text/html\n\n";

my $request;

########################################        Query String


                if ($ENV{'REQUEST_METHOD'} eq "GET")
                        {
                        $request = $ENV{'QUERY_STRING'};
                        }
                elsif ($ENV{'REQUEST_METHOD'} eq "POST")
                        {
                        read(STDIN, $request,$ENV{'CONTENT_LENGTH'}) || die "Could not get query\n";
                        }

#$request="usr=sairam&oldpassword=password123&oldpassword=123456&repassword=123456";
my $j=0;
my ($i,@update_value,@value);
my @parameter=split(/&/,$request);

                for $i (@parameter)
                        {
                                        @value=split(/=/, $i);
                                        $update_value[$j] =$value[1];
                                        $j++;
                        }




                my $user=$update_value[0];
                my $oldpward=$update_value[1];
                my $newpward=$update_value[2];
                my $repward=$update_value[3];

 #$user = $_SESSION['username'];

if ($user)
        {
##      Database Connectivity
                my $DSN = q/dbi:ODBC:SQLSERVER/;
                my $uid = q/ivr/;
                my $pwd = q/ivr/;
                my $DRIVER = "Freetds";
                my %attr = (PrintError => 1,RaiseError => 1,);
                my $dbh = DBI->connect($DSN,$uid,$pwd,\%attr) or die $DBI::errstr;;

                my $sth=$dbh->prepare("select password from rpt_account_information where username='$user'") or die("Query didn't work");
                $sth->execute();
                my $oldpassworddb=$sth->fetchrow();
                #               check pass
                if ($oldpward==$oldpassworddb)
                        {
                        #       check twonew pass
                        if ($newpward==$repward)
                                {
                                #success
                                #change pass in db
                                 if (length($newpward)>10||length($newpward)<4)   #Here is the code
                                        {
                                         print  "<script>alert('Password must be betwwen 4 & 10')</script>";
                                        }

                                else
                                        {
                                        my $p_update = $dbh->prepare("UPDATE rpt_account_information SET password=? WHERE username=?");
                                        $p_update->execute($newpward,$user);
                                        #session_destroy();
                                        print 'Your pass has benn changed.<a href="login.pl">Return</a> to the main page';
                                        }
                                }
                        else
                                {
                                        print "<script>alert('New Pass does not match')</script>";
                                }
                        }
                else
                        {
                        print "<script>alert('Old Pass does not match')</script>";
                        }

}

这是我的完整代码,但我无法找出错误或输出请帮助我...

4

1 回答 1

0

innerHTML为包含元素的值分配一个值<script>不会导致该脚本被执行。

作为一种解决方法,您可以先在 JS 中解析 JavaScript,然后再解析eval它,但您最好处理结构化数据(例如 JSON)并将您的表示逻辑留给页面中的 JavaScript。

于 2013-09-11T13:56:28.920 回答