简体   繁体   English

在页面上提交加载AJAX

[英]Submit on page load AJAX

I'm very new to AJAX and Jquery so I wondered if anybody could help me. 我是AJAX和Jquery的新手,所以我想知道是否有人可以帮助我。

I've made a AJAX search/filter-system. 我做了一个AJAX搜索/过滤器系统。 When you send a form a new page (filter_content.php) will load into the parent page (filter.php). 发送表单时,新页面(filter_content.php)将加载到父页面(filter.php)中。 The output of filter_content.php is based on some GET values which you can fill in the form on filter.php filter_content.php的输出基于一些GET值,您可以在filter.php上填写表格

$("#filter").submit(function(event) {
event.preventDefault();
$("#result").html('');
var values = $(this).serialize();
$.ajax({
    url: "filter_content.php",
    type: "get",
    data: values,
    success: function(data){
        $('#result').hide().html(data).fadeIn('normal');
    },
    error:function(){
        $("#result").html('There is error while submit');
    }
});
});

HTML: HTML:

<form action="" name="filter" method="get" id="filter">
<input type="text" name="s" value="<?php echo $_GET['s']; ?>" />
<select id="genre" name="genre">
<option value="0">Alle</option>
<option value="5">Deep house</option>
</select>
<input type="submit" name="submit" value="Filter" />
</form>

The problem I now experience is when I visit eg "filter.php?query=hello" the search-query box will be filled in with "hello" but the ajax loaded content can't read the query-value so the content will just display without any conditions. 我现在遇到的问题是,当我访问“ filter.php?query = hello”时,搜索查询框中将填充“ hello”,但是加载了ajax的内容无法读取查询值,因此内容只会显示没有任何条件。 I hope somebody can help me with it! 我希望有人可以帮助我!

Thanks in advance! 提前致谢!

PS here's a working example: http://phpserver.de-breul.nl:8082/~106967/pov6/filter2.php PS这是一个工作示例: http : //phpserver.de-breul.nl : 8082/~106967/pov6/filter2.php

Best solution would have the PHP code just output the results from the start. 最好的解决方案是让PHP代码从一开始就输出结果。

But if you still want the client to do the search, submit the form if there is a value. 但是,如果您仍然希望客户进行搜索,请在有值的情况下提交表单。

$(function(){
    if ($('input[name="s"]').val().length>0) {
        $("#filter").submit();
    }
});

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

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