【问题标题】:passing id name on click using ajax to php使用 ajax 在点击时将 id 名称传递给 php
【发布时间】:2013-01-30 17:11:41
【问题描述】:

我正在使用 Ajax 来制作过滤搜索系统。我有三个不同的选项卡,用户可以在其中按名称、类别和位置进行搜索。 当用户在搜索框(tab-1)中输入名称时,我可以搜索。 在第二个选项卡中,我如何使用相同的 Ajax,所以当用户单击链接时,id 会在 ajax 脚本中传递给我的 php,并且该 id 在我的 mysql 查询中作为变量传递。

第一次使用 Ajax,非常感谢任何帮助。

AJAX 脚本:

$(document).ready(function () {
    $("#search_results").slideUp();
    $("#button_find").click(function (event) {
        event.preventDefault();
        search_ajax_way();
    });
    $("#search_query").keyup(function (event) {
        event.preventDefault();
        search_ajax_way();
    });
});

function search_ajax_way() {
    $("#search_results").show();
    var search_this = $("#search_query").val();
    $.post("search.php", {
        searchit: search_this
    }, function (data) {
        $("#display_results").html(data);
    })
}

html:

<form id="searchform" method="post">
    <input id="search_query" name="search_query" placeholder="What You Are Looking For?"   
        size="50" type="text" />
    <input id="button_find" value="Search" type="submit" />
</form>
<div id="display_results">
</div>

<div class="tab">
    <input id="tab-2" name="tab-group-1" type="radio" />
    <label for="tab-2">Search by Category</label>
    <div class="content">
        <div id="searchbycategory">
            <div id="nav_1_a">      
                <ul>
                    <li><a href="#">All Categories</a></li>
                    <li><a href="#" id="dummy">Category-1</a></li>
                    <li><a href="#">Category-2</a></li>
                    <li><a href="#">Category-3</a></li>
                </ul>
                <div id="display_results">
                </div>
            </div>
            <!-- END nav_1_a -->
        </div>
    </div>
</div>
<div class="tab">
    <input id="tab-3" name="tab-group-1" type="radio" />
    <label for="tab-3">Search by location</label>
    <div class="content">
        <div id="searchbylocation">
            <div id="nav_1_a">      
                <ul>
                    <li><a href="#">All</a></li>
                    <li><a href="#">Location-1</a></li>
                    <li><a href="#">Location-2</a></li>
                    <li><a href="#">Location-3</a></li>
                    <li><a href="#">Location-4</a></li>
                </ul>
            </div>

搜索.php:

<?php
$connection = mysql_connect('localhost', 'user', 'pwd');
$db = mysql_select_db('db', $connection);
$term = strip_tags(substr($_POST['searchit'],0, 100));
$term = mysql_escape_string($term); 
echo "Enter name to search";
else{
$sql = mysql_query("select col1,col2 from tab2 where tab2.somecolm like 
  '{$term}%'", $connection);

 echo "<ul>";
if (mysql_num_rows($sql)){
while($info = mysql_fetch_array($sql, MYSQL_ASSOC ) ) {
echo "<li>";
    echo "<a href=\"http://" . $info['col1'] . ".html\">" . $info['col2'] . "</a>";
    echo "</li>";
}

}else{
echo "No matches found!";
}

echo "</ul>";
}
?>

【问题讨论】:

  • 不要再使用mysql_* 函数,它们已被弃用(参见red box)。此外,mysql_escape_string 不会考虑当前字符集,也不会转义_%(为此使用addcslashes)。此外,您应该在$info[*] 上使用htmlspecialchars 来防止XSS 攻击。
  • 注意,我会更改 php 脚本,但是如何将 id 名称(单击链接时)作为变量传递给我的 php 脚本?
  • 当用户点击一个链接时,会在 ajax 脚本中传递 id 你说的是哪个 ID?单选按钮的 ID 还是其他选项卡中会有带有 ID 的文本框?
  • echo "Enter name to search"; 之后有一个没有 if 的 else
  • @TheRealCoder:我想为我的 href 提供 id,以便可以将这些 id 传递给我的脚本。

标签: php javascript jquery mysql ajax


【解决方案1】:

将块ID传递给search_ajax_way函数:

$("#search_query").keyup(function(event){
    event.preventDefault();
    search_ajax_way(this.id);
});

然后在ajax请求的数据参数中传递block id:

function search_ajax_way(blockId){
   $("#search_results").show();
   var search_this=$("#search_query").val();
   $.post("search.php", {searchit : search_this, 'blockId': blockId}, function(data){
      $("#display_results").html(data);

   })
}

现在 blockId 将在您的 php 脚本中作为 $_POST['blockId'] 可用。

【讨论】:

  • 我会马上试试这个并回复你。非常感谢!
  • 所以,我在上面尝试了这些更改,还通过在类别列表下添加显示结果来编辑我的脚本,但是当我单击 id=dummy 的类别 1 时没有显示任何内容。不过,Tab-1 仍然像以前一样正常工作。
  • 你需要传递什么ID?在我的回答中,您传递的是 $("#search_query") 元素 ID。如果您需要其他东西 - 将 search_ajax_way(this.id) 更改为 search_ajax_way($(this).searchWhatEverYouWant()[0].id) 并将 searchWhatEverYouWant 方法替换为任何搜索您需要的元素的 jQuery 遍历方法。
  • 我有不同的链接,它们会有不同的 id,所以根据 usr 点击的内容,我希望传递该 Id,从而更改查询。非常感谢您的帮助,谢谢。
  • 你需要的一切都已经在我的回答中了。想一想。您的问题是:“使用 ajax 将 id 名称传递给 php”。我已经回答了如何将附加参数传递给 AJAX 调用,现在在点击处理程序中找到您需要的 ID 并传递它。
【解决方案2】:

您说您想在点击链接时传递 id,但您没有任何处理链接点击的代码。为链接添加点击处理程序,并修改 search_ajax_way() 以接受点击链接时的可选 id:

$("a").click(function (event) {
    event.preventDefault();
    search_ajax_way(this.id);
});

function search_ajax_way(clickedId) {
    $("#search_results").show();
    var postData = { searchit: $("#search_query").val() };
    if (clickedId) {
        postData.clickedId = clickedId;
    }
    $.post("search.php", postData, function (data) {
        $("#display_results").html(data);
    })
}

ID 将在 PHP 中以 $_POST['clickedId'] 的形式提供

编辑:实际上,我会重构以使用search_ajax_way() 作为事件处理程序,而不是从匿名事件处理程序中调用它:

$("#button_find,a").click(search_ajax_way);
$("#search_query").keyup(search_ajax_way);

function search_ajax_way(event) {
    event.preventDefault();
    $("#search_results").show();
    var postData = {
        searchit: $("#search_query").val(),
        clickedId: this.id
    };
    $.post("search.php", postData, function (data) {
        $("#display_results").html(data);
    })
}

【讨论】:

  • 上述两种方法我都试过了,我肯定搞砸了。只是无法找出什么。链接只是没有传递 id 名称。我很确定,当我切换标签并单击链接时出现问题。有什么想法吗?
  • @alice - 您是否在 PHP 中寻找 $_POST['clickedId']?您在 JavaScript postData 对象中提供的属性名称是提交给 PHP 的字段名称。使用该字段名称通过 PHP 访问 $_POST[&lt;field name&gt;] 中的值。
  • 那么,这样:$term = strip_tags(substr($_POST['searchit'],0, 100));更改为: $term = strip_tags(substr($_POST['clickedId'],0, 100)); ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 2014-11-25
相关资源
最近更新 更多