2

I have an ajax call that passes data to another php file, createTest2.php, as below.

But the createTest2.php file throws error

"Notice: Undefined index: aaa in C:\xampp\htdocs\TestProj\Test\createTest2.php on line 2

I have no clue how to fix it.

caller.php

$(document).ready(function(){
    $("#button_submit").click(function() 
  {

    $.ajax({
      type:"POST",
      url:"createTest2.php",
      data:{aaa : "UNIT_TEST"},
      success:function()
      {
        alert("success");
      }
    });
 });
});

createTest2.php

$test_name = $_POST['aaa'];
4

3 回答 3

0
    if you are using 
    $test_name = $_POST['aaa'];
 you have to call ajax like this
$(document).ready(function(){
    $("#button_submit").click(function() 
  {

    $.ajax({
      type:"POST",
      url:"createTest2.php",
     data:"aaa=UNIT_TEST",
      success:function()
      {
        alert("success");
      }
    });
 });
});

最主要的是“数据:”aaa = UNIT_TEST“,”

于 2013-04-18T08:04:25.270 回答
0

尝试这个:

//sample.php
<?php
if(isset($_POST) && isset($_POST['aaa'])){
    echo json_encode("hello world! Your data was: " . $_POST['aaa']);
}
?>

//your client side page
<html>
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">

        function send(){
            $.ajax({
                type:"POST",
                url: 'sample.php',
                data:{aaa:"UNIT_TEST"},
                dataType: 'json'
            }).done(function(data){
                alert(data);
            });
        }

    </script>
</head>
<body>
<a href="#" onclick="send();">Send</a>
</body>
</html>
于 2013-04-18T08:05:29.533 回答
0

例如,在 index.html 中试试这个:

<script>
$(document).ready(function(){
    $("#button_submit").click(function() 
  {

    $.ajax({
    global: false,
    type:"post",
    dataType: "html",
    cache: false,
    url: "createTest2.php",
    data: {aaa:'test'},
    success:function(html)   {    alert(html);  },
    error: function(e) {alert(e);}
    });
 });
});
</script>

并在您的 createTest2.php 中放置它以查看是否收到了 ajax 调用:

<?php
echo $_POST['aaa'];

?>
于 2013-04-18T08:18:30.510 回答