用户配置文件通过ajax更新

时间:2014-10-15 15:25:37

标签: javascript php ajax

大家好我试图通过ajax更新用户详细信息,但它没有更新我的用户表,只是在拥有时停止执行...出现在这里是我的代码请尽可能帮助

edit_profile.php

<?php
if(isset($_POST["la"])){
$firstname = strip_tags(preg_replace('#[^a-z0-9]#i', '', $_POST['fi']));
    $lastname = strip_tags(preg_replace('#[^a-z]#', '', $_POST['la']));
    $age = strip_tags(preg_replace('#[0-9]#', '', $_POST['ag']));
    $gender = strip_tags(preg_replace('#[^a-z]#', '', $_POST['g']));

  // FORM DATA ERROR HANDLING
    if($gender == "" || $firsname == "" || $lastname == "" || $age == ""){
        echo "some account info is empty.";
        exit();
        }else{
      $sql = "UPDATE users SET gender='$gender', age='$age', lastname='$lastname, firstname='$firstname' WHERE username='$log_username' LIMIT 1";


     $query = mysqli_query($db_conx, $sql); 
echo "account_success";
        exit();

    }

    }
?>

ajax和js

<script type="text/javascript" language="javascript">
function _(x){
    return document.getElementById(x);
}
function toggleElement(x){
    var x = _(x);
    if(x.style.display == 'block'){
        x.style.display = 'none';
    }else{
        x.style.display = 'block';
    }
}

function restrict(elem){
    var tf = _(elem);
    var rx = new RegExp;
    if(elem == "firstname"){
        rx = /[^a-z0-9]/gi;
    } else if(elem == "lastname"){
        rx = /[^a-z0-9]/gi;
        } 
    tf.value = tf.value.replace(rx, "");
}
function emptyElement(x){
    _(x).innerHTML = "";
}
function ajaxObj( meth, url ) {
    var x = new XMLHttpRequest();
    x.open( meth, url, true );
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;    
    }
}
function update_profile() {
    var fi = _("firstname").value;
    var la = _("lastname").value;
    var g = _("gender").value;
    var ag = _("age").value;
    var status2 = _("status_profile");
    if(fi == "" || la == "" || ag == "" || g == ""){
        status2.innerHTML = "Fill out all of the form data";
        status2.style.color = "red";
    }else {
        _("profilebtn").style.display = "none";
        status2.innerHTML = 'processing ...';
        var ajax = ajaxObj("POST", "edit_profile.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                if(ajax.responseText != "account_success"){

                    _("profilebtn").style.display = "block";
                } 
            }
        }
        ajax.send("&fi="+fi+"&la="+la+"&g="+g+"&ag="+ag);   
    }
}
</script>

html

 <div class="row profile-classic">
<div class="col-md-12 m-t-20">
<form name="updateform" id="updateform" onsubmit="return false;">
<div id="profile_form" class="panel">
 <div class="panel-title line">
 <div class="caption"><i class="fa fa-phone c-gray m-r-10"></i>ACCOUNT</div>
 </div>
 <div class="panel-body">

 <div class="row">
<div class="control-label c-gray col-md-3">Username:</div>
<div class="col-md-6">
 <input type="text" class="form-control"  value="<?php echo $uname; ?>" disabled="disabled">
   </div>
   </div><br />
 <div class="row">
 <div class="control-label c-gray col-md-3">Firstname: *</div>
<div class="col-md-6">
<input type="text" onfocus="emptyElement('status_profile')" class="form-control" id="firstname" contenteditable="true" value="<?php echo $firstname; ?>">
 </div>
  </div><br />
 <div class="row">
  <div class="control-label c-gray col-md-3">Lastname: *</div> 
<div class="col-md-6">
 <input type="text" onfocus="emptyElement('status_profile')" class="form-control" id="lastname" placeholder="your lastname" value="<?php echo $lastname; ?>" >
 </div>
</div><br />

 <div class="row">
 <div class="control-label c-gray col-md-3">Age: *</div> 
<div class="col-md-6">
 <input type="text" onfocus="emptyElement('status_profile')" class="form-control" id="age" value="<?php echo $age; ?>">
</div>
 </div><br />
<div class="row">
 <div class="col-md-6">
<select class="form-control"  onfocus="emptyElement('status_profile')" id="gender" class="form-control">
<option value="<?php echo $gender; ?>"><?php echo $sex; ?></option>
<option value="m">Male</option>
 <option value="f">Female</option>

 </select>
 </div>
<button id="profilebtn" onclick="update_profile()" class="btn btn-sm btn-default"><i class="fa fa-floppy-o"></i> Save</button><span id="status_profile"></span>


</div><br />
  </div>
 </div></form>

</div></div>

1 个答案:

答案 0 :(得分:0)

您可以使用mysqli_error($db_conx)来确定错误是什么......只需让您的javascript很好地展示给您。

但是 - WHERE子句($log_username)中的变量未定义。