简体   繁体   English

将AJAX变量传递给PHP

[英]Passing a AJAX Variable to PHP

Hi wondering how to send a AJAX variable to php, I thought I had it but apparently not. 您好想知道如何将AJAX变量发送到php时,我以为自己拥有了它,但显然没有。 In my console I get the error "Uncaught TypeError: Illegal invocation line 6" 在我的控制台中,出现错误“ Uncaught TypeError:非法调用第6行”

Im taking it there is something wrong with my code straight after the alert? 我在警报后立即认为我的代码有问题吗? (NOTE where it say "jquery" is in replace of $ simply because joomla does not like $ in scripts for some reason) UPDATED, Pay attention to (注意,由于某些原因joomla不喜欢$在脚本中使用$,因此它用$ j代替了“ jquery”)更新,请注意

Script to click and get rowID 单击并获取rowID的脚本

<script language="javascript" type="text/javascript">
  jQuery(document).ready(function()
  {
     jQuery("tr.getRow").click(function()
     {
       rowID = jQuery(this).find("td.idCell");
       alert(jQuery(rowID).text());
       //Send the row ID to ajaxupdate.php
       jQuery.post("ajaxupdate.php", { submit: "update", ID_ID: rowID})
       .done( function(data) {
       var results = jQuery.parseJSON(data);
       console.log( results );
       })
       .fail( function() {
       console.log("AJAX POST failed."); 
       });
     });
  });
</script>

Load first table(the one thats being clicked) 加载第一个表(即被单击的那个表)

<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 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 ?>
<br>
</table>

Second table, the one that im trying to get to load 第二张表,我试图加载的那个

<?php
// In ajaxupdate.php file

if( (isset($_POST['ID_ID'])) || (isset($_POST['submit']))) //im Sure this part is wrong
{
$ID_ID =($_POST['ID_ID']); // pass JS var as a PHP var

$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('".$ID_ID."'));

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


}
?>

3425742, 3425742,

I rewrote your script and tested it with this JSfiddle . 我重写了您的脚本,并使用此JSfiddle对其进行了测试。 Try it out. 试试看。

I see that you are using Joomla. 我发现您正在使用Joomla。 Diving into Joomla as a novice is daunting. 作为新手进入Joomla令人生畏。 Within ajaxupdate.php the script is expecting to see a $_POST['submit'] variable. 在ajaxupdate.php中,脚本期望看到$ _POST ['submit']变量。 Either remove that requirement or add it like I did below. 删除该要求或像我在下面那样添加它。 At the bottom of ajaxupdate.php add this line so that jQuery has something to test. 在ajaxupdate.php的底部添加此行,以便jQuery进行测试。

echo $results ? json_encode("succeeded") : json_encode("failed"); die();

Here is the jQuery ajax code: 这是jQuery ajax代码:

//Send the row ID to ajaxupdate.php
$.post("ajaxupdate.php", { submit: "update", TP_ID: rowID})
.done( function(data) {
   var results = $.parseJSON(data);
   console.log( results );
})
.fail( function() {
   console.log("AJAX POST failed.");                
});

Edit "ajaxupdate.php" to the correct location of that file. 将“ ajaxupdate.php”编辑到该文件的正确位置。 If ajaxupdate.php is in a different directory you have to tell jQuery to look there. 如果ajaxupdate.php位于另一个目录中,则必须告诉jQuery在该目录中查找。 For example, if your $.post is in index.php in the root of your webserver and ajaxupdate is in the /js directory change "ajaxupdate.php" to "js/ajaxupdate.php". 例如,如果$ .post在Web服务器的根目录中的index.php中,而ajaxupdate在/ js目录中,则将“ ajaxupdate.php”更改为“ js / ajaxupdate.php”。

you are passing rowID as an object, not as a single text-variable. 您将rowID作为对象而不是单个文本变量传递。 You'd need 你需要

$.post("ajaxupdate.php", ({ TP_ID: rowID.attr('id') }), function( data )
...

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

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