0

Hi wondering how to send a AJAX variable to php, I thought I had it but apparently not. In my console I get the error "Uncaught TypeError: Illegal invocation line 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

Script to click and get 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();


}
?>
5
  • Can you show your html as well? Commented Mar 16, 2014 at 19:56
  • wow! jQuery rules! there are 2 post calls. remove the 1st one and change ({ TP_ID: $(rowID).attr('TP_ID') }) to { TP_ID: rowID.text() } and when we say post html, please post only the relevant html and NOT the whole web page! Commented Mar 16, 2014 at 20:10
  • Should it look like this now jQuery.post("ajaxupdate.php", ( { TP_ID: rowID.text() }), function( data ) also now im getting localhost/abac/index.php/ajaxupdate.php 404 (Not Found) Commented Mar 16, 2014 at 20:19
  • Change jQuery.post("ajaxupdate.php", { submit: "update", ID_ID: rowID}) TO jQuery.post("ajaxupdate.php", { submit: "update", ID_ID: rowID.text()}) Commented Mar 18, 2014 at 13:08
  • I know this must be a bother now so my appoligies but when i changed it with .text() i got Uncaught TypeError: Object 12 has no method 'text' Is there any for me to send you my script and maybe you could see what is going wrong? Commented Mar 18, 2014 at 15:31

2 Answers 2

1

3425742,

I rewrote your script and tested it with this JSfiddle. Try it out.

I see that you are using Joomla. Diving into Joomla as a novice is daunting. Within ajaxupdate.php the script is expecting to see a $_POST['submit'] variable. 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.

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

Here is the jQuery ajax code:

//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. If ajaxupdate.php is in a different directory you have to tell jQuery to look there. 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".

Sign up to request clarification or add additional context in comments.

16 Comments

ajaxupdate.php 404 (NOT FOUND) << is that a problem?
LShetty, Matthew, with matthews code I get POST localhost/abac/index.php/.php 404 (Not Found) jquery.min.js:6 AJAX POST failed. which for the most part means its working but them stopping somewhere this may be an extremely dumb question but what does "ajaxupdate.php" do? Just so i can have a better understanding. Also thank you for your time, ive been up for two days trying to get this to work
Let's say you are looking at index.php in your browser. When you click the row jQuery connects to "ajaxupdate.php," in the same directory, behind the scenes and gives it the variables "submit and "TP_ID". jQuery then starts to listen for a response from "ajaxupdate.php". Within ajaxupdate.php it starts up when jQuery sends it the variables and can grab those variables by reading $_POST['submit'] and $_POST['TP__ID']. ajaxupdate.php then does whatever its purpose is then echoes whether it succeeded or not. jQuery hears the response and tells you.
You get a 404 error and "AJAX POST failed" if your "ajaxupdate.php" file is not in the same directory as the page you are on. Joomla runs scripts relative to the site root so find where "ajaxupdate.php" is and change its location in the $.post. I edited the answer to give an example.
And you still have 2 post requests?
|
0

you are passing rowID as an object, not as a single text-variable. You'd need

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

4 Comments

It should be "rowID.attr('id')" as rowID is already an object.
Thanks for the quick reply! Now im getting Uncaught TypeError: Cannot call method 'attr' of null also where you have id do i change to TP_ID ?
user: please post your html! assumption is the mother of all blunders!
Posted, abit a messy! But as you can see i load first table then click on a row it gets ID, im then wanting to load another table where ID = ID if that makes sense. (extrmley new to coding I have been doing for about 5 days so sorry about my noobish/noviceness)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.