使用ajax发送和检索值

时间:2013-05-01 15:51:35

标签: php jquery ajax

这是我的预览问题的一个更清晰的代码,我的想法是使用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 个答案:

答案 0 :(得分:0)

看起来你永远不会告诉AJAX POST名称是'centro',试着改变它:

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

这个

data: { 'centro' : $("#form_"+i).serialize()},

答案 1 :(得分: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);
?>

答案 2 :(得分:0)

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

php://input 

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

有关此包装器的更多信息,请访问this link

托盘将以下内容添加到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

这会读取邮件正文(包含我发布的数据),然后我可以访问那里的值。

希望这有帮助。

答案 3 :(得分: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"

        });
});