dongyue934001 2014-08-15 02:32
浏览 11
已采纳

有人可以帮我在Jquery中写这个/其他吗[关闭]

   <!-- Search input -->
   <form method="post" action="query.php" id="searchform" name="search" class="search">
    <input id="inpBusiness" type="text" placeholder="Find a Business by Name" name="Business"/>
    <input id="inpLocation" type="text" placeholder="Or by a Massachusetts City/Town" name="City"/>
    <input id="btnSearch" type="submit" value="Search VetsDirectory" name="btnSearch"/><br/>
   </form>
   <hr />

   </td>
  </tr>
  <tr>
   <td>
    <!-- This is where the featured listings and search Query will load -->
    <h3 align="left">FEATURED BUSINESS LISTINGS:</h3>
    <div id="inputhere">
    <?PHP
    include 'php.php';
    ?>
    </div>

This is my form, upon submit, I need to check if a user used the inpBusiness or inpLocation to search. If they used location to search, I need the action to be query.php, if they used business to search I need action to be query2.php, if both or neither are used populate an error to basically say "pick one"

After this check. I need query.php or query2.php to populate where php.php is included inside the #inputhere div, I need it to clear php.php from that div, then append either query or query 2 based upon input.

UPDATE:

<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script>
$(document).ready(function () {
    var $article = null;
    $('#searchform').submit(function () {
        if ($article == null) {
            $("#inputhere").load("query.php").appendTo('.inputhere');
        }
    });
});
</script>

Without changing anything in my form, I am attempting to now just load the form into the div, but it is still redirecting me immediately to a new page displaying query.php, which I can see it loading it into the div right before the redirect. Any ideas?

Last one:

<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script>
$(document).ready(function () {
    var $article = null;
    $('#searchform').submit(function (e) {
        e.preventDefault();
        if ($article == null) {
            $("#inputhere")).html('Loading...').load('query.php');
        }
    });
});
</script>

Its still loading the query on a separate page. As can be seen on my server @ www.masswarveterans.com

  • 写回答

1条回答 默认 最新

  • duanmao9918 2014-08-15 02:42
    关注

    Something like this is what you want:

    $('#searchform').submit(e) {
        e.preventDefault();
        var business = $('#inpBusiness').val().trim();
        var location = $('#inpLocation').val().trim();
    
        if(
            (business == '' && location == '') // both empty
            ||
            (business != '' && location != '') // both not empty
        ) {
            alert('Pick one');
            return false;
        }
    
        var searchTerm = 'business';
        var action = 'query2.php';
        // The above definitions correspond to the implicit opposite of the statement
        // below. Overriding where necessary.
        if(location != '' && business == '') {
            action = 'query.php';
            searchTerm = location;
        }
    
        // Perform AJAX request to populate your div
        $('#inputhere').html('Loading...').load(action, {
            searchTerm: searchTerm,
        }, function(ret) {
            // If you need a callback on complete, use this.
        });
    });
    

    Then your PHP code, use $_POST['searchTerm'] to help you decide what to display.


    Looking forward...

    Once you've worked out how to do this, you should start again and design it better. It'd be a whole lot better for your application if you only had one search script, and one place to input a search term. As mentioned in the comments, use a radio choice or a dropdown box to define which search type it should be.

    If you do that, later on when you decide to add ten more types of searches you'll be able to do it easily without having to add conditions for every new type to your jQuery.

    You'll just end up performing an AJAX request like this (for example):

    $('#searchform').submit(e) {
        e.preventDefault();
        if($('#search_term').val().trim() == '') {
            alert('Enter something man...');
            return false;
        }
        $('#inputhere').html('Loading...').load('search_file.php', {
            search_term: $('#search_term').val().trim(),
            search_type: $('#search_type :selected').val();
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。