Jquery ajax数据值未传递给目标url

时间:2014-02-20 15:34:00

标签: php jquery ajax

我遇到了一个错误,我的id值没有以某种方式传递给脚本你能帮帮我吗?可能是因为数据:从表单中获取所有数据并以某种方式覆盖了ID号?

var otherPro = $('.euserId').text();
console.log(otherPro);
$.ajax({
    type: "POST",
    url: "modifyuser.php?id=" + otherPro,
    data: $('form.editUser').serialize(),
    beforeSend: function () {},
    success: function (msg) {
        $("#msgholder1").html(msg)
        console.log(msg);
    },
    error: function () {
        $("#msgholder1").html("error")
    }
});

调试

consol.log#1    51 (index):66 - this is the value of "+ otherPro
        <br />

php response (var_dump($_POST['id']))    

<b>Notice</b>:  Undefined index: id in <b>C:\xampp\htdocs\platform\admin\modifyuser.php</b> on line <b>10</b><br />

     consol.log#2    NULL

php response (if no ID given)   

<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title">Form</h3>
</div>
<div class="panel-body">No Id Given<br></div>
</div> 

php文件

<?php
require_once("../checklogin.php");
require_once("../lib/functions.php");
require_once("../lib/Db.class.php");

    // Creates the instance  || preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $_POST['password'])
    $db = new Db();
    $user = new users();
    $error = array();
var_dump($_POST['id']);
if (empty($_POST['id'])) { 
        $error[] = 'Brak podanego id'; 
    } else {
        $username = $_POST['id']; 
    }

    if (empty($error))

    { // If everything's OK...

        $teto = $user->Find($_POST['id']);
        if( !empty( $teto ) )
        { 
            $update = $db->query("UPDATE Persons SET username = :username, name =:name, surname = :surname, password = :password, lastlogin = :lastlogin, isOnline = :isOnline, level = :level WHERE id = :id", array(
            "id" => $_POST['id'],
            "username" => $username,
            "name" => $name,
            "surname" => $surname,
            "password" => $password,
            "lastlogin" => '0000-00-00 00:00:00',
            "isOnline" => '0',
            "level" => $level
        ));
            ?>
                    <script>


            $(".editUser").hide();
            $('#editUser').hide();
            var result = '<p class="{$color}">Modyfikowano wpis.</p>', //html for main result
            delay = 200, //delay of sub results
            data = Array(
            '<li class="green">Polaczenie z baza <img src="http://www.jquery4u.com/images/green_tick_small.gif"></li>',
            '<li class="orange">Modyfikacja wpisu <img src="http://www.jquery4u.com/images/green_tick_small.gif"></li>'
            );

        //self executing function starting from array index 0
        (function process_els(el_index) {

            var el = data[el_index],
                precheckUl = $('#precheck ul'),
                loadingLi = $('<li class="loading"><img width="18px" height="18px" src="http://www.jquery4u.com/images/ajax_loader.gif"></li>'),
                sysPreId = "syspre_"+el_index;

          //show loading image
          precheckUl.append(loadingLi.clone().attr("id",sysPreId));

          //after simulated delay replace loading image with sub check result
          setTimeout( function()
          {
              precheckUl.find('li.loading:first').replaceWith(data[el_index]);

          }, delay);

          //to simulate the delay recursively call itself until all array elements have been processed
          if (el_index + 1 < data.length) {
            setTimeout(function() { process_els(el_index + 1); }, delay);
          }
            else
            {
                setTimeout(function() 
                {
                    //append the final result after all sub checks have been inserted
                    precheckUl.after(result);
                }, (delay*2));
            }
        })(0);

    </script>
    <div id="precheck">
    <h2>Modyfikacja wpisu</h2>
    <ul />
</div>
<?PHP
        }else{ 
            echo "<div class=\"errormsgbox\" >No ID Error</div>"; 
        }


    } else { //If the "error" array contains error msg , display them.... e.g....

        echo '<div class="panel panel-danger">
              <div class="panel-heading">
                <h3 class="panel-title">Bledy w formularzu</h3>
              </div>
              <div class="panel-body">';
        foreach ($error as $key => $values) {

            echo '' . $values . '<br>';

        }
        echo '</div>
            </div>';

    }

1 个答案:

答案 0 :(得分:1)

使用$_REQUEST['id']代替$_POST['id']。您在URL中传递id,而不是像通常的POST请求那样在正文中传递。实际上这对于POST来说并不合适:那就是GET。 这应该可以解决问题。

更好的解决方案是在'id'变量中添加data,以便将其放入请求正文中,例如:

$.ajax({
    type: "POST",
    url: "modifyuser.php",
    data: $('form.editUser').serialize()  + '&id=' + $('.euserId').text(),
    beforeSend: function () {},
    success: function (msg) {
        $("#msgholder1").html(msg)
        console.log(msg);
    },
    error: function () {
        $("#msgholder1").html("error")
    }
});

检查您的浏览器控制台,以便在需要时改进此代码段。