Using php in javascript

Hello,

I get a syntax error
How should I do this?

$(document).ready(function(){

    var query = '<?php echo isset($_GET['urun_kodu']) ? ''.$_GET['urun_kodu'].'' : ''; ?>';

load_data(1, 10);
function load_data(page, per, query = '')

});

You need to include the syntax error so we can see what the problem is.

Now it came to my mind, This code is in the “pagination.js” file
php won’t work, right?

That’s right. You can probably make it work by renaming the file to pagination.js.php; your webserver will detect that it’s a php file and run the code through php first. If you’re going to do that, remember that php runs on the server, before the browser gets the file; javascript runs in the browser, when the server has finished sending the file. You can use php to fill out bits of javascript, but you can’t make the two interact any more than that.

Is there a difference between the two on behalf of the file?

pagination_js.php or pangination.js.php

How should I include it on the page?

require_once("pagination_js.php");
require_once("pagination.js.php");
OR
<script src="pagination.js.php"></script>

Either file name should be fine; it’s the .php extension that will make your server interpret it as PHP.

Even though it has some PHP in it, your script is still javascript and will have to be loaded like any other javascript: using the <script> tag.

You can get a better idea of what is happening by viewing the source of your pagination.js.php in your browser; you will see that the PHP code has been interpreted by the time the browser sees it.

My codes are below but not working
Where am I doing wrong

in pagination.js.php

$(document).ready(function(){
    <?php
    if(isset($_GET['duzelt'])){
    ?>
        load_data(1, 10, '<?php echo $_GET['urun_kodu']; ?>');
    <?php
    }else{
    ?>
        load_data(1, 10);
    <?php
    }
    ?>
    function load_data(page, per, query = '')
    {
        $('#satir_table').hide();
        $('#sayfalama_table').hide();
      $.ajax({
        url:"load_data.php",
        method:"POST",
        dataType: "json",
        data:{ page:page, query:query, per:per, "linkler" : "1", "satirlar" : "<?php echo $satirlar; ?>" },
        success:function(data)
        {
            $('#satir_table').show();
            $('#sayfalama_table').show();
            $('#yukleniyor').hide();
            $('#satirlar').html(data.satirlar);
            $('#linkler').html(data.linkler);
        }
      });
    }
    function ajax_load_data(page, per, query = '')
    {
      /*
        $('#satir_table').hide();
        $('#sayfalama_table').hide();
        */
      $.ajax({
        url:"load_data.php",
        method:"POST",
        dataType: "json",
        data:{ page:page, query:query, per:per, "linkler" : "1", "satirlar" : "<?php echo $satirlar; ?>" },
        success:function(data)
        {
          /*
            $('#satir_table').show();
            $('#sayfalama_table').show();
            $('#yukleniyor').hide();
            */
            $('#satirlar').html(data.satirlar);
            $('#linkler').html(data.linkler);
        }
      });
    }
    $(document).on('click', '.page-link', function(){
      var page = $(this).data('page_number');
      var per = $(this).data('per_number');
      var query = $('#search').val();
      ajax_load_data(page, per, query);
    });
    $('#sayfada').change(function() {
      var per = $(this).val();
      var query = $('#search').val();
      ajax_load_data(1, per, query);
    });
    $('#search').on('keyup', function(){
      var query = $('#search').val();
      var per = $('select[name=sayfada] option').filter(':selected').val();
      ajax_load_data(1, per, query);
    });
});

in index.php

<?php
    $satirlar = "multiswitch";
  ?>
  <script type="text/javascript" src="pagination.js.php"></script>

pagination.js.php is now being interpreted as PHP on the server. That means that when the browser loads the file the server runs the code in the file as PHP, then returns the result to the browser as the content of the file. This means the request to pagination.js.php is a separate request to the original request for your page, and the variables you’re looking for are not available.

Thank you very much for your help
I can’t understand because I’m a novice
Works smoothly as below

$satirlar = "multiswitch";
require_once("js/pagination.js.php");
Sponsor our Newsletter | Privacy Policy | Terms of Service