将AJAX变量传递给PHP

时间:2014-03-17 20:55:16

标签: php jquery mysql ajax post

大家好,我的目标是点击一个表获取其ID,然后使用其ID加载另一个表。到目前为止,我能够获取ID,但是当我尝试加载第二个表时,我得到了错误

  

"未定义的索引:C:\ xampp \ htdocs \ abac \ ajaxupdate.php中的Projec_ID   第6行"

这是我的代码

AJAX脚本(控制台打印rowID所以它正在获取,变量我认为在尝试传递它时会出错?)

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
    var log = $("#log");

    $(".getRow").click(function() {
    console.log("Clicked a row...");
    rowID = $(this).find("td.idCell").text();

    //Print the row ID in the log cell to make sure we got the right one.
    log.text("You 1clicked row "+rowID);
    console.log("You cl2icked row "+rowID);

    //Send the row ID to ajaxupdate.php

     $.post("/abac/ajaxupdate.php", { what: "updateRow", Projec_ID: rowID})
    .done( function(data) {
    var results = $.parseJSON(data);
    console.log(rowID );
    })
    .fail( function() {
    console.log("AJAX POST failed.");
    });
    });

    });
</script>

PHP文件(ajaxupdate.php)我认为这里有什么问题我猜测

<?php 

     if( (isset($_POST['submit'])) ||  (isset($_POST['Projec_ID'])) )
     {     
        $Projec_ID =($_POST['Projec_ID']);
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query
    ->select($db->quoteName(array('CV_ID', 'Classifier', 'Value', 'TP_ID')))
    ->from($db->quoteName('sessionta'))
          //also is the like  part right?
    ->where($db->quoteName('TP_ID') . ' LIKE '. $db->quote($_POST['Projec_ID']));

    $db->setQuery($query);
    $results = $db->loadObjectList();
    //echo $Classifier;
     }
?>

3 个答案:

答案 0 :(得分:0)

除非您设置,否则

$_POST['submit']将为false: [...] { "what": "updateRow", "Projec_ID": rowID, "submit" : "true"}

这就是您收到错误消息&#34; Undefined Index&#34; ! -

此外,您始终可以:

<?php 
echo "<pre>"; 
print_r($_POST);
echo "</pre>"; 
die("The data above was sent via POST"); 

解决这些问题。

答案 1 :(得分:0)

您的问题可能是您正在使用||操作员而不是&amp;&amp;操作

if( (isset($_POST['submit'])) ||  (isset($_POST['Projec_ID'])) )

这意味着如果在某些时候你传递$ _POST ['submit']而不是$ _POST ['Projec_ID']它仍会运行此代码。给你

"Undefined index: Projec_ID in C:\xampp\htdocs\abac\ajaxupdate.php on line 6 "

尝试将代码更改为:

if( (isset($_POST['submit'])) &&  (isset($_POST['Projec_ID'])) )

答案 2 :(得分:-1)

我认为Project_ID不是一个字符串..

试试这段代码。

<script language="javascript" type="text/javascript">
    $(document).ready(function() {
    var log = $("#log");

    $(".getRow").click(function() {
    console.log("Clicked a row...");
    rowID = $(this).find("td.idCell").text();

    //Print the row ID in the log cell to make sure we got the right one.
    log.text("You 1clicked row "+rowID);
    console.log("You cl2icked row "+rowID);

    //Send the row ID to ajaxupdate.php

     $.post("/abac/ajaxupdate.php", { what: "updateRow", "Projec_ID": rowID})
    .done( function(data) {
    var results = $.parseJSON(data);
    console.log(rowID );
    })
    .fail( function() {
    console.log("AJAX POST failed.");
    });
    });

    });
</script>

我刚用“。

附上了Projec_ID