无法在我的php页面中修复未定义的索引

时间:2014-03-06 06:17:47

标签: php jquery html ajax

我正在尝试从具有令牌的输入元素的html页面访问令牌 当我在Windows 7的服务器(wamp)上运行此页面时,我在“api_key”上收到一个错误的未定义索引。我如何解决它 ?  php代码:

  <?php
    $Cname=$_POST['api_key'];
    if($Cname=="To-do")
    {
    $api_key=$_POST['api_key'];
    $file1="i have some url here not mentioning it for valid reason".$api_key;
    $data = file_get_contents($file1);
    $json_o=json_decode($data);
    echo "<pre>";
    print_r($json_o);
    echo "</pre>";
    }
    ?>

//html page:
//script is within head
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script type="text/javascript">
$(document).ready(function() {
      $("#postJson").click(function(event){
          $.post( 
             "get_task_by_column.php",{hidden:$('#api_key').val()},
             function(data) {
            $('#response').html(data);

             }

          );
      });
});

</script>
//body starts
<div>
    <form>  
    <input type="hidden" name="api_key" value="024b427a5d41a62edd218bddb55ed1fc" id="api_key" >
    <input type="text" id="txthtml">
    <input type="button" value="Get tasks by column name" id="postJson"/>

    </div>

    <div id='response'></div>

    </form>

6 个答案:

答案 0 :(得分:3)

如果您需要$_POST,则hidden中的索引为api_key,然后更改

{hidden:$('#api_key').val()}

{api_key:$('#api_key').val()}

答案 1 :(得分:0)

更改这些

$Cname=$_POST['api_key'];
$api_key=$_POST['api_key'];

到这些

$Cname=isset($_POST['api_key'])?$_POST['api_key']:null;
$api_key=isset($_POST['api_key'])?$_POST['api_key']:null;

和js部分

"get_task_by_column.php",{hidden:$('#api_key').val()},

 "get_task_by_column.php",{api_key:$('#api_key').val()},

答案 2 :(得分:0)

使用isset()

<?php
    $Cname=isset($_POST['api_key']) ? $_POST['api_key'] : '';
    if($Cname=="To-do")
    {
    $api_key=$_POST['api_key'];
    $file1="i have some url here not mentioning it for valid reason".$api_key;
    $data = file_get_contents($file1);
    $json_o=json_decode($data);
    echo "<pre>";
    print_r($json_o);
    echo "</pre>";
    }
    ?>

答案 3 :(得分:0)

您发布的值将是$_POST['hidden'],因为您在jQuery中定义的是

如果你想要它是api_key

将其更改为

api_key: $('#api_key').val()

答案 4 :(得分:0)

您在发布api_value之前访问$_POST['api_key']

<?php
$Cname ="";
if(isset($_POST['api_key']))
{
    $Cname=$_POST['api_key'];
 }
    if($Cname=="To-do")
    {
    $api_key=$_POST['api_key'];
    $file1="i have some url here not mentioning it for valid reason".$api_key;
    $data = file_get_contents($file1);
    $json_o=json_decode($data);
    echo "<pre>";
    print_r($json_o);
    echo "</pre>";
    }
    ?>

//html page:
//script is within head
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script type="text/javascript">
$(document).ready(function() {
      $("#postJson").click(function(event){
          $.post( 
             "get_task_by_column.php",{hidden:$('#api_key').val()},
             function(data) {
            $('#response').html(data);

             }

          );
      });
});

</script>
//body starts
<div>
    <form action="index.php" method="post">   
    <input type="hidden" name="api_key" value="024b427a5d41a62edd218bddb55ed1fc" id="api_key" >
    <input type="text" id="txthtml">
    <input type="submit" value="Get tasks by column name" id="postJson"/>

    </div>

    <div id='response'></div>

</form>

答案 5 :(得分:0)

试试这个

<?php

    $Cname = false;
    if(isset($_POST['api_key'])){
        $Cname=true;
    }

    if(isset($_POST['toDo']) && $_POST['toDo'] == "To-do" && $Cname){
        $api_key=$_POST['api_key'];
        $file1="i have some url here not mentioning it for valid reason".$api_key;
        $data = file_get_contents($file1);
        $json_o=json_decode($data);
        echo "<pre>";
        print_r($json_o);
        echo "</pre>";
    }
?>

//html page:
//script is within head
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "> </script>
<script type="text/javascript">
    $(document).ready(function() {
       $("#postJson").click(function(event){
          $.post("get_task_by_column.php",{api_key:$('#api_key').val(), toDo:1}, function(data) {$('#response').html(data);});
      });
    });
</script>
//body starts
<div>
    <form action="index.php" method="post">   
        <input type="hidden" name="api_key" value="024b427a5d41a62edd218bddb55ed1fc" id="api_key" >
        <input type="text" id="txthtml">
        <input type="submit" value="Get tasks by column name" id="postJson"/>
    </form>
</div>
<div id='response'></div>