如何在同一文件中的PHP脚本中访问jQuery或javascript变量?
我的JavaScript文件如下.我想在我的PHP脚本中使用值变量(存储页码(分页))来执行与数据库相关的操作.
My javascript file is as follows. I want to use the value variable(stores the page number(pagination)) in my php script to do database related operations.
<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>
我的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);
?>
我对PHP不太满意,但是有基本的了解
I am not very much comfortable with PHP but have a basic understanding
非常感谢!!!提前向所有试图帮助我的人
Thanks a lot!!! in advance to all those who tried to help me
PHP
是服务器端脚本语言(在服务器上运行),而javascript
是客户端脚本语言(在浏览器上运行),因此您不能只需在一个变量之间使用变量/值,但是有这样做的方法.
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.
因此,为了将变量value
的值发送到服务器,您需要向服务器发出请求,您可以通过将value
的值放在表单的隐藏字段中来进行表单提交请求当用户提交表单或您可以提出AJAX请求时,该文件就会转到服务器.
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 .
以表格形式输入值
<input type="hidden" name="pagination_page" value="" id="pagination_page_value">
,然后使用jquery设置此字段的值.
and then set the value of this field using jquery.
$("#").val(value);
AJAX 方法:
AJAX Approach:
$.get("savePaginationValue.php",{pagination_page_value:value}).done(function(data){
//Success do whatever you want.
}).fail(function(){
//Something bad happened.
});
,并在savePaginationValue.php
中使用$_GET["pagination_page_value"]
轻松访问此值
您可以根据需要使用GET
或POST
.
and in savePaginationValue.php
access this value easily using $_GET["pagination_page_value"]
you can use either GET
or POST
depending on your requirement .
希望有帮助