0

这是我的预览问题的更清晰的代码,其想法是使用 ajax 发送和检索一个值,但该值没有被发送,ajax 似乎也不起作用。我更新了这段代码,因为这样可以很容易地在任何机器上进行测试。第一次使用ajax。这是代码:

Javascript

<script>
jQuery(document).ready(function() {
jQuery('#centro').click( function() {
$.ajax({
        url: 'request.php',
        type:'POST',
        data: $("#form").serialize(),
        dataType: 'json',
        success: function(output_string){
        alert(output_string);
                $('#cuentas').html(output_string);
            } // End of success function of ajax form
        }); // End of ajax call    

  });
 }
});
</script>

HTML:

  <?php
  $result = 'works';
  ?>

  <form id="form">
  <div id="centro">
  <a href="#">Click here</a>
  <br>
  <input type="hidden" name="centro" value="<?php echo $result; ?>">
  </form>

  <div id="cuentas">
  </div>

PHP 文件,request.php

 <?php
 $centro = $_POST['centro'];
 $output_string = ''.$centro;
 echo json_encode($output_string);
 ?>
4

4 回答 4

0

尝试这个:

HTML

<?php
    $i=0;
    $f=0;
    $consulta = $db->consulta("SELECT * FROM centro");
    if($db->num_rows($consulta)>0){
        while ($resultados1 = $db->fetch_array($consulta)){   ?>
        <form id="form_<?php echo $f++; ?>"> //begin form with its id
            <div class="centro" id="centro_<?php echo $i++; ?>">
                <a href="#"> <?php echo $resultados1['nombre_centro']; ?></a>
                <br>
                <input type="hidden" name="centro" value="<?php echo $resultados1['id_centro']; ?>">
                <!--this is the data that is going to be sent. I set up a hidden input to do it.-->
            </div>
        </form>
        <div id="cuentas" class="cuentas">
            <!--where data is  going to be displayed-->
        </div>
        <br>
        <?php  
        }
    } 
?>

Javascript:

<script>
jQuery(document).ready(function() {
    jQuery('.centro').on('click',function() {
        var formElem=jQuery(this).closest('form');
        jQuery.ajax({ 
            url: 'load_cuentas.php',
            cache: true ,
            type:'POST',
            dataType: 'json',
            data: $(formElem).serialize(),
            success: function(output_string){
                jQuery(formElem).next('div.cuentas').html(output_string);
                alert(output_string);
            } // End of success function of ajax form
        }); // End of ajax call 
    });
});

</script>

服务器页面:

<?php
    include ('mysql.php');
    $db = new mysql();
    $centro = $_POST['centro']; //this is not getting any data. the whole ajax thing
    $consulta = $db->consulta("SELECT * FROM cuenta WHERE id_center = '$centro'");
    $output_string=array();
    if($db->num_rows($consulta)>0){
        while ($resultados1 = $db->fetch_array($consulta)){   
            $output_string []= 'test text';
        }
    }               
    mysql_close();
    echo json_encode($output_string);
?>
于 2013-05-01T16:00:21.227 回答
0

看起来您从未告诉 AJAX POST 名称是“centro”,请尝试更改:

data: $("#form_"+i).serialize(),

为了这

data: { 'centro' : $("#form_"+i).serialize()},
于 2013-05-01T15:56:51.630 回答
0

我通过该POST方法调用的 ajax 调用遇到了同样的问题。我的数据实际上是在消息正文中传递的。我必须通过以下方法访问它:

php://input 

这是一个只读包装流,允许您从消息正文中读取原始数据。

有关此包装器的更多信息,请访问此链接

将以下内容添加到您的 PHP 文件中:

$centro = file_get_contents("php://input");
// Depending on how you pass the data you may also need to json_decode($centro)
echo json_encode($centro); 
// See if you get back what you pass in

这读取了消息正文(带有我发布的数据),并且我能够访问那里的值。

希望这可以帮助。

于 2013-05-01T16:05:43.117 回答
0

尝试在您的 ajax 帖子中使用此代码:

jQuery('#centro_'+i).click( function() {
$.ajax({

    data: $("#centro_"+i).closest("form").serialize(), 
    dataType:"html", 
    success:function (data, textStatus) {
                $('#cuentas').html(data);
                alert(data);}, 
    type:"post", 
    url:"load_cuentas.php"

        });
});
于 2013-05-01T16:10:43.743 回答