注意:未定义的索引Jquery$.post和php


Notice: Undefined index Jquery $.post and php

我的html:中有这个错误

注意:未定义的索引:在第3行发送C:''examplep''htdocs''licateiro''search_clients.php错误

我需要用mysql+php向我的html请求数据。我用方法贴;

我使用这个代码:

Jquery邮政编码:

$(function(){
  $("#send").click(function() {
  var json  = $.parseJSON($("#json").val());
        var url2 = $("#url2") .val();
         $.post(url2,json,function(data){
  $('div#status').html(data);
     });
});

HTML代码:

<div id="form1" class="send"> 
<p>URL:<input type="text" name="url2" id="url2" /></p>
<p> JSON:<textarea name="json" id="json" cols="45" rows="5"></textarea></p>
<p><input type="submit" class="button" name="send" value="send" id="send" /></p>
</div>
<div id="status">
  RETORNO:
<!--  <textarea name="comment" id="comment" cols="45" rows="5"></textarea> -->
</p>
</div>

PHP代码:

include ('conection.php');
$data = $_POST['send'];
$arr = json_decode($data);
//$arr = json_decode($_POST['json']);
$result = mysql_query("SELECT * FROM clientes WHERE client_descricao='".$arr[0]['client_descricao']."' OR client_razaosocial ='".$arr[0]['client_razaosocial']."' OR client_endereco ='".$arr[0]['client_endereco']."' OR client_cidade ='".$arr[0]['client_cidade']."' OR client_estado ='".$arr[0]['client_estado']."' OR client_telefone ='".$arr[0]['client_telefone']."'")or die(mysql_error());
$json = mysql_fetch_array($result, MYSQL_ASSOC);
echo json_encode($json);

我需要将请求的数据置于div状态,请帮助我。

您的POST变量名称错误,请尝试以下操作:

jQuery呼叫后

$.post(url2,{data: json},function(data){...

PHP var

$data = $_POST['data'];

或者,如果您仍然使用提交按钮而不是AJAX,请执行以下操作:

$data = $_POST['json'];

更新

好的,这是固定的jQuery代码,用于进行AJAX调用并阻止刷新:

function send(){
    var json = $.parseJSON($("#json").val());
    var url2 = $("#url2").val();
    $.post( url2, {data: json}, function( data ){
        $('div#status').html( data );
    });
    return false;
}

HTML表单元素:

<div id="form1" class="send" onsubmit="return send();">

PHP数据:

$data = json_decode($_POST['data']);