密码字段未使用Ajax到达php [英] Password field is not arriving to php using ajax

查看:79
本文介绍了密码字段未使用Ajax到达php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在调试问题所在的位置时遇到了问题.可能是Jquery或PHP问题.我已经做了很多警报测试,并且我相信问题出在ajax的if语句中,例如,如果发布时未禁用该字段,则我正在发布通行证.因为当执行echo $ _POST ['pass']和alert(data)时,我什么也没得到.因此,我相信php可以正常工作,请告诉我问题出在哪里,如果您也知道这种情况的解决办法.谢谢你们.

So i am having an issue debugging where the issue is. Could be Jquery or PHP issue. I have done quite a bit of alert testing and i believe the issue lies in the if statement of ajax, eg i am posting pass if the field is not disabled when posting. Because when doing a echo $_POST['pass'] and alert(data), i get nothing. So i believe the php is working fine please tell me where the issue lies and if you know also the remedy to this situation. Thanks Guys.

Ajax

$(document).on('submit', '#editaccsount', function(event) {
event.preventDefault();

myData = {
        contactname: $('input[name=contactname]').val(),
        business: $('input[name=business]').val(),
        email: $('input[name=email]').val(),
        code: $('input[name=code]').val(),
        phone: $('input[name=phone]').val(),
        priceband: $('input[name=priceband]').val(),
        address: $('input[name=address]').val(),
        active: $('input[name=active]').val(),
        mon: $('input[name=mon]').val(),
        tue: $('input[name=tue]').val(),
        wed: $('input[name=wed]').val(),
        thu: $('input[name=thu]').val(),
        fri: $('input[name=fri]').val(),
        sat: $('input[name=sat]').val(),
        sund: $('input[name=sund]').val(),
        adminname: $('input[name=adminname]').val(),
        accountid: $('input[name=accountid]').val(),
        isadmin: $('input[name=isadmin]').val(),
};

var isDisabled = $('input[name=pass]').prop('disabled');

if (isDisabled == false) {
        myData.pass = $('input[name=pass]').val();
}
$.ajax({
    url: 'php/editaccount.php',
    type: "POST",
    data: myData,
    success: function(data) {

        if ($('input[name=isadmin]').val() == 1) {
            $('input[name=accountsearch]').val($('input[name=email]').val());
            $('input[name=accountsearch]').submit();
        } else {
                $('input[name=accountsearch]').val($('input[name=business]').val());
            $('input[name=accountsearch]').submit();
        }

        alert(data);
    }
});
});

这是我的PHP,可以根据条件进行所有更新:

This is my PHP that does all the updating based on conditions:

<?php

require '../../core/init.php';

$auth = new Auth();


$accid = $_POST['accountid'];

echo $accid, '<br/>';
echo 'Password is: ', $_POST['pass'], '<br/>';
echo 'Is Admin: ', $_POST['isadmin'];

if (!empty($_POST['pass'])) {
    echo 'Pass Detected';
    echo 'Password: ', $_POST['pass'], "<br/>";

    $salt = $auth->randomString();
    $newpass = $salt . $_POST['pass'];
    $newpass = $auth->hashData($newpass);


    if ($_POST['isadmin'] == 0) {

        $customer = DBPDO::getInstance()->update('customer', $accid, array(
                'businessName'      =>  $_POST['business'],
                'contactName'       =>  $_POST['contactname'],
                'email'             =>  $_POST['email'],
                'code'              =>  $_POST['code'],
                'phone'             =>  $_POST['phone'],
                'priceBand'         =>  $_POST['priceband'],
                'deliveryAddress'   =>  $_POST['address'],
                'is_active'         =>  $_POST['active'],
                'mon'               =>  $_POST['mon'],
                'tue'               =>  $_POST['tue'],
                'wed'               =>  $_POST['wed'],
                'thu'               =>  $_POST['thu'],
                'fri'               =>  $_POST['fri'],
                'sat'               =>  $_POST['sat'],
                'sun'               =>  $_POST['sund'],
                'password'          =>  $newpass,
                'user_salt'         =>  $salt
            ));
    } elseif ($_POST['isadmin'] == 1) {

        $admin = DBPDO::getInstance()->update('admin', $accid, array(
                'adminName'         =>  $_POST['adminname'],
                'email'             =>  $_POST['email'],
                'phone'             =>  $_POST['phone'],
                'password'          =>  $newpass,
                'user_salt'         =>  $salt
            ));
    }


} elseif (empty($_POST['pass'])) {
    echo 'Pass NOT detected';

    if ($_POST['isadmin'] == 0) {

        $customer = DBPDO::getInstance()->update('customer', $accid, array(
                'businessName'      =>  $_POST['business'],
                'contactName'       =>  $_POST['contactname'],
                'email'             =>  $_POST['email'],
                'code'              =>  $_POST['code'],
                'phone'             =>  $_POST['phone'],
                'priceBand'         =>  $_POST['priceband'],
                'deliveryAddress'   =>  $_POST['address'],
                'mon'               =>  $_POST['mon'],
                'tue'               =>  $_POST['tue'],
                'wed'               =>  $_POST['wed'],
                'thu'               =>  $_POST['thu'],
                'fri'               =>  $_POST['fri'],
                'sat'               =>  $_POST['sat'],
                'sun'               =>  $_POST['sund']
            ));
    } elseif ($_POST['isadmin'] == 1) {

        $admin = DBPDO::getInstance()->update('admin', $accid, array(
                'adminName'         =>  $_POST['adminname'],
                'email'             =>  $_POST['email'],
                'phone'             =>  $_POST['phone']
            ));
    }

} 

输出为HTML的内容

<?php

require '../../core/init.php';
$data['result_1'] = '';
$searchquery = $_POST['accountsearch'];

if ($_POST['accounttype'] == 'Users') {

$customer = DBPDO::getInstance()->get('customer', array(
                array('businessName', '=', "'$searchquery'" )
            ));

foreach ($customer->results() as $row)
{
    $data['result_1'] .= '

    <div class="row row-centered">
            <input class="btn btn-lg btn-primary" type="button" name="editbtn" value="Edit Account">
            <input class="btn btn-lg btn-danger remove-btn" type="button" name="removebtn" value="Remove Account" uid="'.$row->id.'">
    </div><br/>
        <form id="editaccount" action="php/editaccount.php" method="POST">
            <h3>General Details</h3>
            <div class="row row-centered">
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="contactname">Customer Name</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="contactname" class="form-control" value="'.$row->contactName.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="business">Business</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="business" class="form-control" value="'.$row->businessName.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="email">Email</label>                
                        <div class="input-group input-group-lg">
                            <input type="email" disabled="disabled" name="email" class="form-control" value="'.$row->email.'">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row row-centered">
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="code">Code</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="code" class="form-control" value="'.$row->code.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="phone">Phone</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="phone" class="form-control" value="'.$row->phone.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="priceband">Price Band</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="priceband" class="form-control" value="'.$row->priceBand.'">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row row-centered">
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="address">Delivery Address</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="address" class="form-control" value="'.$row->deliveryAddress.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="pass">Active</label>               
                        <div class="input-group input-group-lg">
                            <input type="test" disabled="disabled" name="active" class="form-control" value="'.$row->is_active.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="pass">Password</label>               
                        <div class="input-group input-group-lg">
                            <input type="password" disabled="disabled" name="userpass" class="form-control" value="'.$row->password.'">
                        </div>
                        <div class="input-group input-group-lg">
                            <input id="editpasswordbtn" class="btn btn-sm btn-primary" type="button" value="Edit Password">
                        </div>
                    </div>
                </div>
            </div>
            <div class="wrning" style="color:red;display:none;">
                <b>Warning: </b> If you do NOT want to edit Password please click Cancel!<br/><br/>
                <input type="button" class="btn btn-sm btn-danger" value="Cancel Password Change">
            </div>
            <br/><br/>
            <h3>Standing Order Details</h3>
            <div class="row row-centered">
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="mon">Monday</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="mon" class="form-control" value="'.$row->mon.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="tue">Tuesday</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="tue" class="form-control" value="'.$row->tue.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="wed">Wednesday</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="wed" class="form-control" value="'.$row->wed.'">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row row-centered">
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="thu">Thursday</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="thu" class="form-control" value="'.$row->thu.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="fri">Friday</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="fri" class="form-control" value="'.$row->fri.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="sat">Saturday</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="sat" class="form-control" value="'.$row->sat.'">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row row-centered">
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="sund">Sunday</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="sund" class="form-control" value="'.$row->sun.'">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row row-centered">
                    <input type="hidden" name="accountid" value="'.$row->id.'">
                    <input type="hidden" name="isadmin" value="0">
                    <input class="btn btn-lg btn-rj" disabled="disabled" type="submit" name="editbtn" value="Send to Database">
            </div>  
        </form>
    ';
}   
} elseif ($_POST['accounttype'] == 'Admins') {
$customer = DBPDO::getInstance()->get('admin', array(
                array('email', '=', "'$searchquery'" )
            ));

foreach ($customer->results() as $row)
{
    $data['result_1'] .= '
        <div class="row row-centered">
            <input class="btn btn-lg btn-primary" type="button"  name="editbtn" value="Edit Account">
            <input class="btn btn-lg btn-danger remove-btn adminremove-btn"  type="button" name="adminremovebtn" value="Remove Account" uid="'.$row->id.'">
        </div><br/>
        <form id="editaccount" action="php/editaccount.php" method="POST">
            <h3>General Details</h3>
            <div class="row row-centered">
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="contactname">Admin Name</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="adminname" class="form-control" value="'.$row->adminName.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="contactname">Email</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="email" class="form-control" value="'.$row->email.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="business">Phone</label>                
                        <div class="input-group input-group-lg">
                            <input type="text" disabled="disabled" name="phone" class="form-control" value="'.$row->phone.'">
                        </div>
                    </div>
                </div>
                <div class="col-md-3 col-centered" style="width:300px;">
                    <div class="form-group">
                        <label for="email">Email</label>                
                        <div class="input-group input-group-lg">
                            <input type="hidden" name="isadmin" value="1">
                            <input type="password" disabled="disabled" name="pass" class="form-control" value="'.$row->password.'">
                        </div>
                    </div>
                </div>
                <div class="row row-centered">
                    <input type="hidden" name="accountid" value="'.$row- >id.'">
                    <input type="hidden" name="isadmin" value="1">
                    <input class="btn btn-lg btn-rj" disabled="disabled" type="submit" name="editbtn" value="Send to Database">
                </div>  
            </div>
        </form>
    ';
}   
}


echo json_encode($data);
exit;

?>

推荐答案

您正在通过以下方式检查已禁用的属性:

You are checking the property of disabled with:

var isDisabled = $('input[name=pass]').prop('disabled');

如果我记得这将返回一个空字符串或disabled作为使用它的一种方式,则是:

If I recall this will return a string empty or disabled as a way to use this would be:

$('input[name=pass]').prop('disabled', 'disabled');

如果您正在寻找true/false,请尝试:

If you're looking for a true/false maybe try with:

var isDisabled = $('input[name=pass]').is(':disabled');

,它将返回true或false.

and it will return true or false.

这篇关于密码字段未使用Ajax到达php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆