Ajax呼叫控制台的正确数字,但是php显示为空

问题描述 投票:0回答:1

我在stackoverflow上看到了许多相同的问题,只是无法弄清楚为什么我的php显示的是null而不是数字。

我有这个php:

<?php
$message .= "<br /><b>Beneficiaries:<b><br />\n\n"; 
$message .= "\n" . $_REQUEST['clickCount'] . "\n\n\n";
$benmax = 4; 
$var =json_encode($_GET['clickCount']);
echo $var;
?>

然后在我页面的下方,在我的html表单开始之前,我在ascript标记中添加了此标记。

 <script type="text/javascript">
        $(document).ready(function(){            
            console.log("ready");
            var clickCount=1;
                $('#showPartTwo').hide(); 

                $('#yesExisting').click(function(){
                    console.log("clicked");
                    $('#showPartTwo').show();           

                });
                $('#noExisting').click(function(){
                console.log("no isclicked");

                     $('#showPartTwo').hide();           

                });

            $('#addrow').click(function(){
                console.log("inside");
                clickCount=clickCount+1;
            console.log(clickCount);
            $('table').append('<tr><td><input class="form-control" type="text" name="benName'+clickCount+'" placeholder="Beneficiarys Name" /></td> <td><input class="form-control" type="text" name="benAddress'+clickCount+'"  placeholder="Address" ></td> <td><input class="form-control" type="date" name="benDob'+clickCount+'" placeholder="Date of Birth" ></td>  <td><input class="form-control" type="text" name="benRel'+clickCount+'" placeholder="Relationship" ></td> <td><input class="form-control" type="text" name="benPercentage'+clickCount+'" placeholder="Percentage of benefit" ></td></tr>');
            });
$('#sendbutton').click(function(){
    $.ajax({
    url :'index2.php',
    type:'POST',
    data: {'clickCount': clickCount},
    success: function(data){
            console.log("success");
        console.log(data);
    }   
    });

});
         });
    </script>

这是我的一些HTML表单

        <form name="lifeform" action="index2.php?flag=1" method="post">

            <h3 class="headingTitle">Beneficiaries</h3>
            <table class="table table-striped border-collapse">
                <thead>
                    <th>Name</th>
                    <th>Address</th>
                    <th>Date of Birth</th>
                    <th>Relationship</th>
                    <th>Percentage of benefit</th>
                <th><input type="button" value="+" id="addrow"/></th>
                </thead>
                <tbody>
                    <tr class="benif">
                        <td><input class="form-control" type="text" name="benName1" placeholder="Beneficiary's Name" required></td>
                        <td><input class="form-control" type="text" name="benAddress1" placeholder="Address" required></td>
                        <td><input class="form-control" type="date" name="benDob1" placeholder="Date of Birth" required></td>
                        <td><input class="form-control" type="text" name="benRel1" placeholder="Relationship" required></td>
                        <td><input class="form-control" type="text" name="benPercentage1" placeholder="Percentage of benefit" required></td>
                    </tr>
                </tbody>
            </table>
            <br/>
  <div style="text-align: right; margin-bottom:2rem;">
    <input class="btn btn-primary" type="submit" value="Send Application" id="sendbutton" name="sendbutton">
  </div>

</form>

有人可以告诉我为什么它返回null吗?顺序错了吗?我说错了吗?

php jquery ajax
1个回答
0
投票

您的js中的调用是POST方法,应使用$ _POST在PHP中获取正确的值

类似的东西:

$ var = json_encode($ _ POST ['clickCount']);

希望这对您有帮助

© www.soinside.com 2019 - 2024. All rights reserved.