如何使用ajax和php提交表单

时间:2013-10-06 14:59:41

标签: php jquery ajax

我正在尝试在我的网站上添加更改密码功能。我决定尝试使用ajax,因此该网站不必自行更新。但我有一个问题。当我将表单提交到我的PHP文件时没有任何事情发生我只是从ajax文件获得成功。并且密码在我的SQL数据库中没有变化。

这是html文件。

<div class="boxPW">
                        <div class="boxInner">
                         <a href="#"><img class="closeBox" style="float: right;" src="images2/zoom/closebox.png" /></a>
                           <h2>Endre passord ditt</h2>

                           <div id="lineBreak" style="margin-top: -19px; width: 70%;"></div>

                           <h4>Skriv inn et nytt passord for <?php echo $fname.' '.$lname.' ';?>
                           Vi anbefaler at du oppretter et unikt passord – 
                           et du ikke bruker på noen andre nettsteder. <h4>
                           <div class="boxInner2">
                                <form id="changePw" name="changePw" method="POST" action="">                                                                           
                                   <input type="password" id="oldPw" name="oldPw" placeholder="Nåværende passord"  />
                                   <label id="error_oldPw" class="error">Fyll inn nåværende passord</label>
                                   <p style="margin: 0; width:100px; font-size:9px; color: #38C6DA; ">Glemt ditt passord?</p>
                                   <div class="divider"></div>
                                   <input type="password" id="newPw" name="newPw" placeholder="Nytt passord"/>
                                   <label id="error_newPw" class="error">Fyll inn nytt passord</label>
                                   <div class="divider"></div>
                                   <input type="password" id="conNewPw" name="conNewPw" placeholder="Bekreft nytt passord"/>
                                   <label id="error_conNewPw" class="error">Gjenta ditt passord</label>
                                   <div class="divider"></div>
                                   <input id="buttonpw" class="button" type="button" name="submit" value="Endre passord" />   
                               </form>


                           </div>
                        </div>  
                   </div>

这是我的Jquery文件(returnPwBox.js)

$(document).ready(function() {
    $('.error').hide();
    $('#buttonpw').click(function(){
        //Validate inputData

        $('error').hide();
            var oldPw = $("input#oldPw").val();
                if(oldPw == ""){
                    $("label#error_oldPw").show();
                    $("input#oldPw").focus();
                    return false;
                    }
            var newPw = $("input#newPw").val();
                if(newPw == ""){
                    $("label#error_newPw").show();
                    $("input#newPw").focus();
                    return false; 
                    }
            var conNewPw = $("input#conNewPw").val();
                if(conNewPw != newPw){
                    $("label#error_conNewPw").show();
                    $("input#conNewPw").focus();
                    return false;
                    }

                    var dataString = 'oldpw='+ oldPw + '&newPw=' + newPw + '&conNewPw=' + conNewPw;

                    $.ajax({
                    type: "POST",
                    url: "changePw.php",
                    data: dataString,
                    success: function(){
                        $('.boxInner2').html("<div id='message' style='width: 300px;'></div");
                        $('#message').html("<h2>Endring fullført</h2>")
                        .append("<h4>Ditt passord er nå endret</h44>")
                        .hide()
                        .fadeIn(1000, function(){
                            $('#message').append("<img id='checkmark' src='imgaes2/pitcharrow.gif'/>");
                        });
                    }
                });
        return false;           
    });

这是我的changePw.php:

    <?php
    include('conn.php');
    require_once('auth.php');
    include('fetchMemData.php');


function clean($str){
    $str=@trim($str);
    if(get_magic_quotes_gpc());{

        $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }

$id=$_SESSION['SESS_MEMBER_ID'];
$oldPw = clean($_POST['oldPw']);
$newPw = clean($_POST['newPw']);
$conNewPw = clean($_POST['conNewPw']);

$oldPw = strip_tags($oldPw);
$newPw = strip_tags($newPw);
$conNewPw = strip_tags($conNewPw);

$oldPw = md5($oldPw);
$newPw = md5($newPw);
$conNewPw = md5($conNewPw);

if($oldPw == $password)
    {
        mysql_query("UPDATE reguser SET password='$newPw' WHERE mem_id='$id'");
    }else{
    echo ("Feil nåværende passord");
    }



?>

如果有人看到任何错误或任何建议,请大声喊出来!我需要一些帮助:)

1 个答案:

答案 0 :(得分:1)

根据您编写的代码,$ password值未分配(除非您在另一个文件中执行)因此$ password为NULL,而if:

if($oldPw == $password)
{
    mysql_query("UPDATE reguser SET password='$newPw' WHERE mem_id='$id'");
}else{
echo ("Feil nåværende passord");
}

返回false