-1

I am having a bit of trouble, and i think my syntax and structure is correct but for some reason the function is failing.

products.php page

i have this function at the bottom of the page which takes the value of $child and adds it to the page, where it gets passed to the Javascript function get_child_options:

 jQuery(document).ready(function(){
      get_child_options(<?=$child;?>);
 });

The function get_child_options is added by my footer.php page

footer.php

function get_child_options(selected){
   if(typeof selected === 'undefined'){
       var selected = '';
   }
   var parentID = jQuery('#parent').val();
   $.ajax({
     url: '/admin/parsers/child_categories.php',
     type: 'POST',
     data: {parentID: parentID, selected: selected},
     success: function (data){
         jQuery('#child').html(data);
     },
     error: function(){alert("Something went wrong with the child options.")},
     });
  }
  jQuery('select[name="parent"]').change(get_child_options);

The error when i load products.php is get_child_options is not declared I have had a look online and via this forumn and i think the makeup of my function within my products.php page is correct, i just don't understand why its not recognising the function is declared within my footer.php and processing the function.

To add, i have tried this within the function on my products.php page but i got rid of the undefined error but the function didnt pass any data to my get_child_options function.

jQuery(document).ready(function(){
    function getchild(child){
        var child = <?=$child;?>
        get_child_options(child);
    }
});

If anyone can help, that would be great and i can't think of what i am doing wrong. TIA

I have reviewed How can I call PHP functions by JavaScript? and feel my situation is different to theres.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
Tom Thorpe
  • 49
  • 6
  • Does this answer your question? [How can I call PHP functions by JavaScript?](https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript) – Jared Smith May 11 '22 at 14:26
  • Your Javascript code runs in your users' web browsers and has no access to your PHP functions. – Jared Smith May 11 '22 at 14:27
  • This is not how this works, please make sure to understand the differences between JavaScript and PHP – Black Ops May 11 '22 at 14:27
  • @JaredSmith - the variable $child does get populated and i know from the products page it can access other js functions which are written in the footer page. – Tom Thorpe May 11 '22 at 14:30
  • @BlackOps - can you give suggestions on how it should, i feel what i am doing "should" work. – Tom Thorpe May 11 '22 at 14:31
  • 1
    @TomThorpe your *code* shows a legit (I think) problem, but your prose description makes it sound like you don't understand the difference between client-side and server-side. I've edited your question to hopefully clarify and removed my downvote and close vote. – Jared Smith May 11 '22 at 14:38
  • "*The error when i load products.php is get_child_options is not declared*" - what is the error, **exactly**, and have you tried searching for that error? Is `get_child_options()` declared inside your `jQuery(document).ready(function(){ ... })`? – Don't Panic May 11 '22 at 14:39
  • And what is the value of `$child` supposed to be? – Jared Smith May 11 '22 at 14:40
  • @JaredSmith - The value of $child is dynamically populated with whatever the select box, selected value is – Tom Thorpe May 11 '22 at 14:42
  • @Don'tPanic - He's the thing i have tried to create a new function with wither get_child_options() or a fresh name - getchild(), and when i refresh the products page, the error goes away, but the function doesn't pass the data ($child) to the get_child_options page, as it doesnt load my ajax call (which is within the get_child_options function – Tom Thorpe May 11 '22 at 14:44
  • @Don'tPanic - the error is: Uncaught ReferenceError: get_child_options is not defined – Tom Thorpe May 11 '22 at 14:45
  • @TomThorpe and how do you know what the user selected when you're on the server? I'm getting back to thinking you don't understand the difference. Please explain better what's going on here, as it stands your question is likely to be closed. How does $child get populated? – Jared Smith May 11 '22 at 14:45
  • @JaredSmith - I have an edit page, which when the edit button is clicked on products.php, it takes the id of the product clicked, and displays the data from the db in select and input boxes. I can display all data for said product except the child category, which i want to send the $child data to the get_child_options function, which will in the end populate the select box with the value for the child category from the db. – Tom Thorpe May 11 '22 at 14:47
  • Why does that require an extra round trip? If you already have the selected child on the server, why do you have to reload the page with it just so it can call a js function that turns right around and calls another php page with ajax? This doesn't make any sense. – Jared Smith May 11 '22 at 14:53
  • 1
    Does the error give you a line number, so you know which line is the problem? And, I'll ask again: did you search for what that error means? It is a very common problem, with *many* examples here on SO, and the answer is usually related to the other question I asked in my previous comment. If some of your code is outside your `jQuery(document).ready(function() { ... });`, and some inside, you can wind up calling code before it has been defined. – Don't Panic May 11 '22 at 15:25

1 Answers1

0

I found the issue and it was that my parsers file couldn't see my authentication file to get the function. Once i had add the correct path to the file it all worked.

Tom Thorpe
  • 49
  • 6