PHP函数接收AJAX变量

时间:2014-03-18 11:06:19

标签: javascript php jquery ajax variables

我试图将AJAX变量:rowID发送到我的php文件,但我一直在

  

注意:未定义的索引:phpID

我是PHP和AJAX的新手,请帮助。

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

jQuery(".getRow").click(function() {
console.log("Clicked a row...");
var rowID = jQuery(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
//jQuery.post("/abac/ajaxupdate.php", { what: "updateRow", PHP_ID: "rowID"})
jQuery.post("/abac/ajaxupdate.php", {phpID: "rowID"})
.done( function(data) {

var results = jQuery.parseJSON(data);
console.log(rowID);
})
.fail( function() {
console.log("AJAX POST failed.");
});
});

});

</script>

我的印象是isset($ _ POST [&#39; rowID&#39;]))将我的ajax中的rowID传递给$ _POST,但它不起作用?

<?php 

     if( (isset($_POST['submit'])) || isset($_POST['phpID'])))
     {     
        $rowID = $_POST['rowID'];
        $db = JFactory::getDbo();

        $query = $db->getQuery(true);
        $query
        ->select($db->quoteName(array('CV_ID', 'Classifier', 'Value', 'TP_ID')))
        ->from($db->quoteName('sessionta'))
        ->where($db->quoteName('TP_ID') . ' LIKE '. $db->quote('$phpID'));

        $db->setQuery($query);
        $results = $db->loadObjectList();

    }
?>

以防万一这是被点击的表格,我的表格是否正确?:

<form name="Permit" id="Permit" action="<?php echo JURI::current(); ?>" method="post">
<p style="width: 46px; position: relative; top: 0px; left: 207px"><input id="submit" name="submit" type="submit" value="save" /></p>
<table border="",th,td, width="500", align="center">
<tr>
<th>TP ID</th>
<th>Permit Deny</th>
<th>Level</th>
<th>Session</th>
<th>Information Specialist</th>
</tr>

<?php foreach ($results as $row): ?>

<tr class="getRow">
<td id="ID_ID" name="ID_ID" class="idCell"><?php echo $row->TP_ID ?></td>
<td><?php echo $row->Permit_or_Deny ?></td>
<td><?php echo $row->Level ?></td>
<td><?php echo $row->Session ?></td>
<td><?php echo $row->Information_specialist ?></td>
</tr>
<?php endforeach ?>
</table>
</form>

2 个答案:

答案 0 :(得分:0)

     function ajax_post()
                                        {


                                            $.ajax({
                                                url: 'backend.php',
                                                dataType: 'json',
                                                type: 'GET',
                                                timeout: 30 * 1000,
                                                data: {key: value, key1: value1},
                                                success: function(json) {

                                                   alert('success');

                                                },
                                                error: function() {
                                                alert('failed');
                                                }

                                            });

将此作为onclick事件使用。并在后端请求如下

 $keyvalue= $_REQUEST['key'];

答案 1 :(得分:0)

您可以使用以下代码。我认为这对你有帮助,你会理解

<script type="text/javascript">
$('.getRow').click(function(){
//Just to check whether click function is working
alert('Hiiii');

var rowID = $('td.idCell').text();

//Just to check whether rowID is getting assigned
alert(rowID);

$.ajax({
            type: 'POST',
            url: 'abac/ajaxupdate.php',
            data: 'phpID='+rowID,
            success:function(data, status)
            {
                alert(data);
                //Do success action here...
            }
        });
});
</script>
<form name="Permit" id="Permit" action="<?php echo JURI::current(); ?>" method="post">
  <p style="width: 46px; position: relative; top: 0px; left: 207px">
    <input id="submit" name="submit" type="submit" value="save" />
  </p>
  <table border="",th,td, width="500", align="center">
    <tr>
      <th>TP ID</th>
      <th>Permit Deny</th>
      <th>Level</th>
      <th>Session</th>
      <th>Information Specialist</th>
    </tr>
    <?php foreach ($results as $row): ?>
    <tr class="getRow">
      <td id="ID_ID" name="ID_ID" class="idCell"><?php echo $row->TP_ID; ?></td>
      <td><?php echo $row->Permit_or_Deny; ?></td>
      <td><?php echo $row->Level; ?></td>
      <td><?php echo $row->Session; ?></td>
      <td><?php echo $row->Information_specialist; ?></td>
    </tr>
    <?php endforeach; ?>
  </table>
</form>

并将以下代码放在abac / ajaxupdate.php

<?php
error_reporting(E_ALL);
if((isset($_POST['submit'])) || isset($_POST['phpID']))
{     
    echo $rowID = $_POST['phpID'];
    exit;
    //Do Php actions here...
}
?>

这样,当数据处理成功时,您将获得带有TP_ID的警报。如果不是这样,你必须检查你的PHP代码。