开发者

How to get Id name with jquery and pass to php script?

开发者 https://www.devze.com 2022-12-21 21:59 出处:网络
I have been trying to get the id of a button(in this case a link) with jquery and send the id or class name to a php script so I can enter that id or class name in a sql query. I thought that this wou

I have been trying to get the id of a button(in this case a link) with jquery and send the id or class name to a php script so I can enter that id or class name in a sql query. I thought that this would be the easiest thing to do, but it has turned out to be the most difficult thing since nothing seems to work.

I have on one page a l开发者_开发问答ink:

pagination.php:

<a href="#" class="category" id="marketing">Marketing</a>

then another script for the jquery (when the button is clicked):

jquery_pagination.php:

$("#pagination a#marketing").click(function () {


    Display_Load();

        var pageNum = $('#content').attr('data-page');

        $("#content").load("filter_marketing.php?page=" + pageNum, Hide_Load());

        });

And lastly, the php page where I want the 'marketing' id to go:

filter_marketing.php:

<?php
include('config.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
$start = ($page-1)*$per_page;
//$sql = "select * from explore order by id limit $start,$per_page";
$sql = "SELECT * FROM explore WHERE category='marketing' ORDER BY category LIMIT $start,$per_page";
$result = mysql_query($sql);
?>
<table width="800px">
<?php
while($row = mysql_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['site_description'];
$site_price=$row['site_price'];

?>
<tr>
<td><?php echo $msg_id; ?></td>
<td><?php echo $message; ?></td>
<td><?php echo $site_price; ?></td>
</tr>

<?php
}
?>
</table>

So all I want is to automatically place the id of 'marketing' in the sql query instead of hard coded as seen here:

$sql = "SELECT * FROM explore WHERE category='marketing' ORDER BY category LIMIT $start,$per_page";

If anyone can help on this that would be amazing.

Thank you.


You should really be more specifics than just saying "nothing works". Does the ajax call works? If not, it's probably a filepath. Heck, i might as well ask you if you're including jquery correctly.

Anyway, your serverside script is far from error-proof, so careful with that.

if all you want is to have the id sent to the script, try this (assuming your ajax code works);

$("#pagination a").click(function () {


    Display_Load();
    var this_id = $(this).attr('id');
    var pageNum = $('#content').attr('data-page');
    $("#content").load("filter_marketing.php?page=" + pageNum +'&id='+this_id, Hide_Load());
});

Then your serverside script:

<?php
include('config.php');
$per_page = 3;

$page= ($_GET['page']!='') ? $_GET['page']: false;
$id= ($_GET['id']!='') ? $_GET['id']: false;

$start = ($page-1)*$per_page;


if($page && $id){
   $sql = "SELECT * FROM explore WHERE category='$id' ORDER BY category LIMIT $start,$per_page";
}
else
{
   die('Error: missing parameters. Id= '.$id.' and page= '.$page);
}


$result = mysql_query($sql);
?>
<table width="800px">
<?php
while($row = mysql_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['site_description'];
$site_price=$row['site_price'];

?>
<tr>
<td><?php echo $msg_id; ?></td>
<td><?php echo $message; ?></td>
<td><?php echo $site_price; ?></td>
</tr>

<?php
}
?>
</table>


$("#pagination a#marketing").click(function () {

var $linkElement = $(this);
Display_Load();

    var pageNum = $('#content').attr('data-page');

    $("#content").load(
      "filter_marketing.php?page=" + pageNum + '&id='+$linkElement.attr('id'),
       Hide_Load());
    });

Now the id of the clicked link will be supplied in the id-parameter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号