How to call a php function from ajax?

前端 未结 4 1904
情话喂你
情话喂你 2020-12-06 01:03

I am familiar of how to get ajax to go to a php page an execute a series of things and then return json data. However is it possible to call a specific function which reside

相关标签:
4条回答
  • 2020-12-06 01:21
        $(document).ready(function(){
                $('#tfa_1117700').change(function(){
                    var inputValue = $(this).val();
                    var v_token = "{{csrf_token()}}";
                    $.post("{{url('/each-child')}}", { dropdownValue: inputValue,_token:v_token }, function(data){
                        console.log(data);
                        $('#each-child-html').html(data);
                    });
                });
            });
    
    
    
     public function EachChild(Request $request){
                $html ="";
                for ($i=1; $i <= $request->dropdownValue; $i++) { 
                    $html = $i;
                }
                echo $html;
    
            }
    
    0 讨论(0)
  • 2020-12-06 01:25

    You can't call a PHP function directly from an AJAX call, but you can do this:

    <? php 
        function test($data){
            return $data+1;
        }
    
        if (isset($_POST['callFunc1'])) {
            echo test($_POST['callFunc1']);
        }
    ?>
    <script>
        $.ajax({
            url: 'myFunctions.php',
            type: 'post',
            data: { "callFunc1": "1"},
            success: function(response) { console.log(response); }
        });
    </script>
    
    0 讨论(0)
  • 2020-12-06 01:33

    If I understand correctly, yes you can. Put all your functions in one php file and have the ajax pass as a parameter which one you want to call. Then with a switch or if structure, execute the one you want.

    0 讨论(0)
  • 2020-12-06 01:38

    For AJAX request

    1. Include jQuery Library in your web page. For e.g.

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
      
    2. Call a function on button click

      <button type="button" onclick="create()">Click Me</button>
      
    3. While click on button, call create function in JavaScript.

      <script>
          function create () {
              $.ajax({
                  url:"test.php",    //the page containing php script
                  type: "post",    //request type,
                  dataType: 'json',
                  data: {registration: "success", name: "xyz", email: "abc@gmail.com"},
                  success:function(result){
                      console.log(result.abc);
                  }
              });
          }
      </script>
      

    On the server side test.php file, the action POST parameter should be read and the corresponding value and do the action in PHP and return in JSON format e.g.

    $registration = $_POST['registration'];
    $name= $_POST['name'];
    $email= $_POST['email'];
    
    if ($registration == "success"){
        // some action goes here under php
        echo json_encode(array("abc"=>'successfuly registered'));
    }     
    
    0 讨论(0)
提交回复
热议问题