简体   繁体   English

如何在同一个文件中访问PHP脚本中的jQuery或javascript变量?

[英]How to access jQuery or javascript variable inside PHP script in the same file?

My javascript file is as follows. 我的javascript文件如下。 I want to use the value variable(stores the page number(pagination)) in my php script to do database related operations. 我想在我的php脚本中使用值变量(存储页码(分页))来执行与数据库相关的操作。

<script>
    var selector = '.links';

    $(selector).on('click', function(){
        $(selector).removeClass('active');
        $(this).addClass('active');
        var value = $(this).text();
        window.location.href="index.php?value";
    });
</script>

My PHP script is 我的PHP脚本是

<?php

        $link = mysqli_connect("localhost","root","","admin");

        if(mysqli_connect_error()) {

            die("There was an error connecting to the database");

        }

        $var = $_GET['value'];  

        $query = 'SELECT article_id, publisher, heading, date, views FROM admin LIMIT '.$var;

        $result=mysqli_query($link,$query);

        if ( false==$result ) {
          printf("error: %s\n", mysqli_error($link));
        }

        echo '<div class="table-full" id="table1"><div class="table-responsive"><table class="table" data-sort="table">';
        echo '<thead><tr><th>ARTICLE</th><th>PUBLISHER</th><th>HEADING</th><th>DATE</th><th>VIEWS</th></tr></thead><tbody>';
        while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
              echo '<tr><td><a href="#">' ."{$row[0]} "."</a></td>
                    <td>" . " {$row[1]} </td> ".
                   "<td>" . " {$row[2]} </td> ".
                   '<td style="min-width:88px">' . " {$row[3]} </td> ".
                   "<td>" . " {$row[4]} </td></tr> ";
        }
        echo "</tbody></table></div></div>";

        mysqli_close($link);
    ?>

I am not very much comfortable with PHP but have a basic understanding 我对PHP不太满意,但有一个基本的了解

Thanks a lot!!! 非常感谢!!! in advance to all those who tried to help me 提前给所有试图帮助我的人

You cannot use javascript variable in php. 你不能在php中使用javascript变量。 However you can use php variable within javascript. 但是你可以在javascript中使用php变量。 Javascript is client side and php is server side, if you want to use then send with ajax because its like sending from client to server. Javascript是客户端,php是服务器端,如果你想使用然后用ajax发送,因为它喜欢从客户端发送到服务器。

Besides the proposal using Ajax you can also simply build a hidden formular(style="display:none") containing the variables you change by JavaScript and call a form submit from JavaScript to submit it to the server. 除了使用Ajax的提议之外,您还可以简单地构建一个隐藏的公式(style =“display:none”),其中包含您通过JavaScript更改的变量,并从JavaScript调用表单提交以将其提交到服务器。

I think it's $(formName).submit(); 我认为这是$(formName).submit();

Or if you don't have problem with showing what you send you can use GET like submission eg by saying location.href="some.page.php?variablenName=someValue" 或者,如果您没有显示发送内容的问题,可以使用GET提交,例如通过说location.href =“some.page.php?variablenName = someValue”

PHP is a server side scripting language(runes on server) whereas javascript is a client side scripting language (runs on browser) so you can not simply use variable/value from one to another ,but there are means to do so. PHP是服务器端脚本语言(服务器上的符文),而javascript是客户端脚本语言(在浏览器上运行),因此您不能简单地使用变量/值从一个到另一个,但有办法这样做。

So in order to send value to variable value to server you need to make a request to server ,you can either make a form submission request by putting the value of value in a hidden field of your form so it goes to server when user submits the form or you can make an AJAX request . 因此,为了向服务器发送值到变量value ,您需要向服务器发出请求,您可以通过将值的value放在表单的隐藏字段中来生成表单提交请求,以便在用户提交时将其发送到服务器表单或您可以发出AJAX请求。

Putting value in form 把价值放在形式上

<input type="hidden" name="pagination_page" value="" id="pagination_page_value">

and then set the value of this field using jquery. 然后使用jquery设置此字段的值。

$("#").val(value);

AJAX Approach: AJAX方法:

$.get("savePaginationValue.php",{pagination_page_value:value}).done(function(data){
    //Success do whatever you want.
}).fail(function(){
    //Something bad happened.
});

and in savePaginationValue.php access this value easily using $_GET["pagination_page_value"] you can use either GET or POST depending on your requirement . 并且在savePaginationValue.php使用$_GET["pagination_page_value"]可以轻松访问此值,您可以根据需要使用GETPOST

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM