" id="sub_menu"> -6ren">
gpt4 book ai didi

php - 如何将php值传递给jquery并获取

转载 作者:行者123 更新时间:2023-12-03 22:14:15 25 4
gpt4 key购买 nike

我在传递值时遇到一个问题,无法通过查询字符串传递它。我的 PHP 是:

 <div class="menu">
<div class="sub_menu_header">ABOUT</div>
<?php
$query1="SELECT id,title FROM aboutus_tbl";
$resulto=mysql_query($query1);
while ($row = mysql_fetch_array($resulto)) {
?>
<a href="about" class="menuid" id="<?php echo $row['id'];?>"><div class="<?php echo $row['title'];?>" id="sub_menu"><?php echo $row['title'];?>
</div></a>
<?php
}
?>
</div>

jQuery:

 <script>
$(".menuid").click(function() {
date_time = $(this).attr('id');
console.log(data);
$.ajax({
type: 'POST',
url: 'about',
data: {"aboutus_id" : date_time},
success: function(data){
alert( data );
}
});
});
</script>

完整代码:

<?php ob_start(); include('web/header.php');?>
<!--main-->
<div class="main_btm">
<div class="wrap">
<div class="main">
<div class="new_head"><font size="5" color="#FC2B5F">
<script>
$(".menuid").click(function() {
date_time = $(this).attr('id');
console.log(data);
$.ajax({
type: 'POST',
url: 'about',
data: {"aboutus_id" : date_time},
success: function(data){
alert( data );
}
});
});
</script>
<?php
if (isset($_POST['aboutus_id'])) {
$id=$_POST['aboutus_id'];
echo $id;
}
else{
$id=1;
}
$que="SELECT title FROM aboutus_tbl WHERE id='".$id."'";
$re=mysql_query($que);
$r=mysql_fetch_array($re);
echo $r[0];
?>
</font></div>
<div class="menu">
<div class="sub_menu_header">ABOUT</div>
<?php
$query1="SELECT id,title FROM aboutus_tbl";
$resulto=mysql_query($query1);
while ($row = mysql_fetch_array($resulto)) {
?>
<a href="about" class="menuid" id="<?php echo $row['id'];?>"><div class="<?php echo $row['title'];?>" id="sub_menu"><?php echo $row['title'];?>
</div></a>
<?php
}
?>
</div>
<div class="contant">
<div class="con_details">
<?php
$query="SELECT * FROM aboutus_tbl WHERE id='".$id."'";
$res=mysql_query($query);
$result=mysql_fetch_array($res);
$d1_per=explode(" ", $result['desc1']);
$d1_lenth=sizeof($d1_per);
$d2_per=explode(" ", $result['desc2']);
$d2_lenth=sizeof($d2_per);
$d3_per=explode(" ", $result['desc3']);
$d3_lenth=sizeof($d3_per);
if ($result['sub_title1']) {
?>
<legend><font size="5"><?php echo $result['sub_title1'];?></font></legend>
<?php
}
if ($result['desc1']) {
for ($i=0; $i < $d1_lenth; $i++) {
echo "<p>".$d1_per[$i]."</p>";
}
}
if ($result['sub_title2']) {
?>
<legend><font size="5"><?php echo $result['sub_title2'];?></font></legend>
<?php
}
if ($result['desc2']) {
for ($i=0; $i < $d2_lenth; $i++) {
echo "<p>".$d2_per[$i]."</p>";
}
}
if ($result['sub_title3']) {
?>
<legend><font size="5"><?php echo $result['sub_title3'];?></font></legend>
<?php
}
if ($result['desc3']) {
for ($i=0; $i < $d3_lenth; $i++) {
echo "<p>".$d3_per[$i]."</p>";
}
}
?>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
window.onload = function() {
$(document).ready(function() {
$('.<?php echo $result['title']?>').addClass('sub_menu_selected');
});
}
</script>
<?php include('web/footer.php'); ob_flush();?>

在上面的代码中,我在<div>中显示了一个菜单。和<a>标签在外面,这样我就可以点击它。我的问题是,当我单击菜单时,它必须显示 id menuid的类但它不工作并且 $_POST['aboutus_id']没有获得任何值(value)。

最佳答案

试试这个代码:-如果您想重定向用户:

header('Location: http://ashvin.com/page.php?' . http_build_query($_GET, '', '&')); die();

如果您只想获取页面,请使用:

file_get_contents('http://ashvin.com/page.php?' . http_build_query($_GET, '', '&'));

还有另一个:-

<a href="/exsomeurl.php?id=1&name=Jose" class="ajax-link"> Click </a>
<a href="/exsomeurl.php?id=2&name=Juan" class="ajax-link"> Click </a>
<a href="/exsomeurl.php?id=3&name=Pedro" class="ajax-link"> Click </a>
...
<a href="/exsomeurl.php?id=n&name=xxx" class="ajax-link"> Click </a>

<script type="text/javascript">
$(function() {
$('.ajax-link').click( function() {
$.get( $(this).attr('href'), function(msg) {
alert( "Data Saved: " + msg );
});
return false; // don't follow the link!
});
});
</script>

关于php - 如何将php值传递给jquery并获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32044786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com