-2

I made a two PHP functions for my project, these two functions responsible for fetching different product category for different "online shop".

Now, I am implementing the function that when employee adding a new product, the employee first select which online shop does the new product belongs to (via a select), then depends on the online shop, the second select menu should display the correct options.

so, the first select looks like this

< Select name="Select_CCV_Webshop[]" id="ccv-webshop"        
onchange='loadNew_CCV_Category()'>

here I need help on how to execute /call /trigger the php function inside of method "loadNew_CCV_Category".

Inside of function "loadNew_CCV_Category", it will get the select value, and this value will be the parameters for the php function.

Please help :D, Thank you !!!!

Avi
  • 1,424
  • 1
  • 11
  • 32
  • 2
    show your code... it would be better to answer then... – Avi Aug 17 '18 at 06:44
  • Research [Javascript AJAX](https://www.w3schools.com/xml/ajax_intro.asp) – DKyleo Aug 17 '18 at 06:45
  • JavaScrpt runs client side. PHP Server side. You can try using AJAX like described in this question: https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript – Nicolo Lüscher Aug 17 '18 at 06:46
  • 1
    Possible duplicate of [How can I call PHP functions by JavaScript?](https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript) – Nicolo Lüscher Aug 17 '18 at 06:46

3 Answers3

0

You cannot execute php inside javascript. because php is server side program and javascript is browser related.

If you want to write php then you can try this.

alert("<?php echo 'hello' ?>")

or

$.ajax({
   url: 'yourphpfilepath.php',
   success: function(respon) {
   $('.result').html(respon);
 }
});

your code will be like this.

<Select name="Select_CCV_Webshop[]" id="ccv-webshop" onchange='loadNew_CCV_Category()'>
<div id=wheretoshowresult> </div>

<script>
function loadNew_CCV_Category(){
  $.ajax({
   url: 'yourphpfilepath.php', //url for your php function or file
   success: function(respon) {
    $('#wheretoshowresult').html(respon);
   }
  });
}
</script>
user7356972
  • 58
  • 12
Avi
  • 1,424
  • 1
  • 11
  • 32
  • i have tried alert, but i don't need to show the alert, so I was confused, i will try second suggestion from you – Levitt Shan He Aug 17 '18 at 06:51
  • if you could offer some example, that would be super! thank u – Levitt Shan He Aug 17 '18 at 07:01
  • google for events in jquery and ajax... read both of the... I know your are new in this and it is getting difficult for you to understand the code... learn about ajax.. I will try to help you with some url http://talkerscode.com/webtricks/dynamic-select-option-menu-using-ajax-and-php.php – Avi Aug 17 '18 at 07:05
  • look at above url that I have given you in comment.. that will help you in detail... – Avi Aug 17 '18 at 07:08
  • the link above from talkers code is perfect, it works for me perfectly – Levitt Shan He Aug 17 '18 at 11:46
  • but could you please help me more on this question? – Levitt Shan He Aug 17 '18 at 11:48
  • if you got the answer than you will also understand my solution... and it worked for you than accept my answer if your problem is resolved... – Avi Aug 17 '18 at 11:48
  • There is nothing to help... it is more likely to understand one problem and then use it... if you want to send data data in ajax then also you will get solution and example online.. just google it and if you cannot find find it then get back to me... – Avi Aug 17 '18 at 12:20
0

The short answer

You can not. PHP is executed server-side and only the PHP-generated page is then delivered to the browser, where JS takes over and can continue reacting to input, but only with information it already has.

The more helpful answer

You can, however, either deliver the information for both cases to the browser and decide which one should be shown, or, which in most cases will be what you would rather do, make another call to the server from within your JS.

That means you would have an additional PHP script, which executes the function and generates the page content you want to insert.

You would then access that script file using JS and insert the generated contents into your page.

As for how to do that, maybe this answer might help.

EDIT: Also refer to Avi's answer on that.

Anonymous
  • 1
  • 1
0

You can try this way,

<?php
function square($num)
{   
    echo $num * $num;
} 
?>

<select name="Select_CCV_Webshop[]" id="ccv-webshop"
onchange='loadNew_CCV_Category()'>
    <option>Select</option>
    <option>Test Php</option>
</select>

<script>
    function loadNew_CCV_Category(){
        var phpData = "<?php square(4) ?>"
        alert(phpData);
    }
</script>
Vikash
  • 643
  • 7
  • 13