繁体   English   中英

PHP函数接收AJAX变量

[英]PHP function to receive AJAX variable

我试图将AJAX variable: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 ['rowID']))将行ID从我的Ajax传递到$ _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>
     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'];

您可以使用以下代码。 我认为这将对您有所帮助,您将了解

<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代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM