dongxia2030 2016-07-05 08:32
浏览 35
已采纳

mysqli使用wordpress主题中的ajax更新查询

I am working on a wordpress based application where I need to change the status of listed orders using ajax. The below code gives me the following error: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

I can not figure out why this is not working. Please help me out.

HTML:

<select id="status-<?php echo $order_id[$xx]; ?>" name="status-<?php echo $order_id[$xx]; ?>" onChange="return statuschange(this, '<?php echo $order_id[$xx]; ?>');">
    <option value="0"<?php if ($status_code[$xx] == 0){ echo' selected="selected"'; }; ?>>incoming</option>
    <option value="1"<?php if ($status_code[$xx] == 1){ echo' selected="selected"'; }; ?>>processing</option>
    <option value="2"<?php if ($status_code[$xx] == 2){ echo' selected="selected"'; }; ?>>processed</option>
    <option value="3"<?php if ($status_code[$xx] == 3){ echo' selected="selected"'; }; ?>>packing</option>
    <option value="4"<?php if ($status_code[$xx] == 4){ echo' selected="selected"'; }; ?>>ready</option>
    <option value="5"<?php if ($status_code[$xx] == 5){ echo' selected="selected"'; }; ?>>posted</option>
    <option value="6"<?php if ($status_code[$xx] == 6){ echo' selected="selected"'; }; ?>>received</option>
    <option value="7"<?php if ($status_code[$xx] == 7){ echo' selected="selected"'; }; ?>>returned</option>
    <option value="8"<?php if ($status_code[$xx] == 8){ echo' selected="selected"'; }; ?>>deleted</option>
</select>

JS (at the end of the file where the above HTML code is):

function statuschange(element, orderid){
    var oldValue = element.defaultValue;
    var newValue = element.value;
    var admincomment = prompt("Change order status?

Add comment:", "");
    if (admincomment != null) {
        var newValueecn = encodeURI(newValue);
        var admincommentenc = encodeURI(admincomment);
        var orderidenc = encodeURI(orderid);
        if (window.XMLHttpRequest) { 
            xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { element.defaultValue = newValue; location.reload(true); console.log(xmlhttp.responseText); } }
            xmlhttp.open("GET", "http://www.mydomain.tld/wp-content/themes/mycustomtheme/changestatus.php?newstatusz=" + newValueecn + "&comment=" + admincommentenc + "&orderid=" + orderidenc, true);
            xmlhttp.send(); }
        } else {
        element.value = element.defaultValue; } };

PHP (changestatus.php):

function cleaninput($input){
    $input = trim($input);
    $input = stripslashes($input);
    $input = htmlspecialchars($input);
    return $input;
}
if(isset($_GET['newstatusz'])){
    $newstatusz = cleaninput($_GET['newstatusz']);
} else { exit; }
if(isset($_GET['comment'])){
    $comment = cleaninput($_GET['comment']);
} else { exit; }
if(isset($_GET['orderid'])){
    $orderid = cleaninput($_GET['orderid']);
} else { exit; }
$current_user = wp_get_current_user();
$userloggedin = $current_user->user_login;
$mysqli1 = new mysqli("localhost", "myuser", "mypass", "mydatabase");
if ($mysqli1->connect_error){ die("DB connect error: " . $mysqli1->connect_error); };
$mysqli1->set_charset("utf8");
$newstatusz = $mysqli1->real_escape_string($newstatusz);
$userloggedin = $mysqli1->real_escape_string($userloggedin);
$comment = $mysqli1->real_escape_string($comment);
$mysqli1->query("UPDATE poszter_admin SET statusz_kod = '$newstatusz', user = '$userloggedin', comment = '$comment' WHERE order_id = '$orderid'");
$mysqli1->close();
exit;
  • 写回答

1条回答 默认 最新

  • doutian3269 2016-07-05 11:19
    关注

    Problem solved thanks to the comment of @dimlucas

    The wp_get_current_user() function was not defined in my PHP code. I have made a workaround to avoid using this function there.

    The working code is below.

    JS:

    function statuschange(element, orderid){
        var oldValue = element.defaultValue;
        var newValue = element.value;
        var userlogin = document.getElementById('userloggedin').innerHTML;
        var admincomment = prompt("Change order status?
    
    Add comment:", "");
        if (admincomment != null) {
            var newValueecn = encodeURI(newValue);
            var admincommentenc = encodeURI(admincomment);
            var orderidenc = encodeURI(orderid);
            if (window.XMLHttpRequest) { 
                xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { element.defaultValue = newValue; location.reload(true); console.log(xmlhttp.responseText); } }
                xmlhttp.open("GET", "http://www.mydomain.tld/wp-content/themes/mycustomtheme/changestatus.php?newstatusz=" + newValueecn + "&comment=" + admincommentenc + "&orderid=" + orderidenc + "&userloggedin=" + userlogin, true);
                xmlhttp.send(); }
            } else {
            element.value = element.defaultValue; } };
    

    PHP (changestatus.php):

    function cleaninput($input){
        $input = trim($input);
        $input = stripslashes($input);
        $input = htmlspecialchars($input);
        return $input;
    }
    if(isset($_GET['newstatusz'])){
        $newstatusz = cleaninput($_GET['newstatusz']);
    } else { exit; }
    if(isset($_GET['comment'])){
        $comment = cleaninput($_GET['comment']);
    } else { exit; }
    if(isset($_GET['orderid'])){
        $orderid = cleaninput($_GET['orderid']);
    } else { exit; }
    if(isset($_GET['userloggedin'])){
    $userloggedin = cleaninput($_GET['userloggedin']);
    } else { exit; }
    $mysqli1 = new mysqli("localhost", "myuser", "mypass", "mydatabase");
    if ($mysqli1->connect_error){ die("DB connect error: " . $mysqli1->connect_error); };
    $mysqli1->set_charset("utf8");
    $newstatusz = $mysqli1->real_escape_string($newstatusz);
    $userloggedin = $mysqli1->real_escape_string($userloggedin);
    $comment = $mysqli1->real_escape_string($comment);
    $orderid = $mysqli1->real_escape_string($orderid);
    $mysqli1->query("UPDATE poszter_admin SET statusz_kod = '$newstatusz', user = '$userloggedin', comment = '$comment' WHERE order_id = '$orderid'");
    $mysqli1->close();
    exit;
    

    Thank you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据