2012-05-24 00:49
浏览 269


I have a simple application here (QandATable2.php) where when the user clicks on the plus button, it will open a modal window and it displays the details which is stored in another page (previousquestions.php).

Now the problem I have is that if you straight away click on the "Search" button when the textbox is blank, you will see that it loads the page on its own page, displaying the message to enter in a phrase for the search and it also displays all of the features previously from the modal window into that page as well. This is incorrect.

What I want it to do is that if the user has clicked on the search button, then when it post's the form and outputs the message, it does it within the modal window, not on its own whole page. So does anyone know how this can be acheived?

The modal window I am using is known as SimpleModal and it's website is here

Below is the QandATable2.php code where it displays the plus button and where it opens the modal window, linking the content of the modal window to the previousquestions.php page:

<script type="text/javascript">

  function plusbutton()

    $.modal( $('<div />').load('previousquestions.php #previouslink') );            
    return false;



<table id="plus" align="center">
<a onclick="return plusbutton();">
<img src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage"/>
<span id="plussignmsg">(Click Plus Sign to look <br/> up Previous Questions)</span>

Below is the previousquestions.php code, where it displays the details in the modal window and where the search feature is stored:


      foreach (array('questioncontent') as $varname) {
        $questioncontent = (isset($_POST[$varname])) ? $_POST[$varname] : '';


    <div id="previouslink">
    <button type="button" id="close" onclick="return closewindow();">Close</button>

    <p>Search for a previous question by entering in a phrase in the search box below and submitting the phrase</p>

    <form action="previousquestions.php" method="post">
          <p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" /></p>
          <p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>



    //...connected to DB

                if (isset($_POST['searchQuestion'])) {

                  $questionquery = "SELECT QuestionContent FROM Question
              WHERE(QuestionContent = '".mysql_real_escape_string($questioncontent)."')";

            if (empty($questioncontent)){
        echo "Please enter in a phrase in the text box in able to search for a question";
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtpw54085 2012-05-24 17:39

    I have found out from an answer on another page to a similar question to this that like Bergi has stated, it is easier using an iframe than using ajax to keep content displayed within a modal window. So the best answer for this question is below where it shows how an iframe is used for the question above:

      function plusbutton() { 
        // Display an external page using an iframe 
        var src = "previousquestions.php"; 
        $.modal('<iframe src="' + src + '" style="border:0;width:100%;height:100%;">');
        return false;
    点赞 打赏 评论
  • doutui9606 2012-05-24 01:12

    You'll probably want to use AJAX, since you're already using jQuery you'll just need something like this:

    // override the "default" form submitting behavior with a callback function
        // this is the callback function for your form submit function.
            // this prevents the page from reloading -- very important!
            // get the search data from the input textbox
            var s = $("input[name='questioncontent']").val();
            // see annotation
               .load("previousquestions.php #previouslink", 
                       questioncontent : s,
                       searchQuestion : "Search"
        }); // end submit wrapper

    This will send the value to the server and load it in the div with id simplemodal-data


    The last line in the code does several things. First, it replaces the simplemodal DIV with a "loading" message. At the same time, it makes a POST request to your previousquestions.php page. This part { questioncontent : s, searchQuestion : "Search"} is where the data from the form gets passed to the PHP page, (remember the variable var s assignment above. Lastly, the results from the previousquestions.php page should be loaded in the simplemodal-data modal window.

    One thing that's missing is to add #previousquestions in the load method so that only a portion of your HTML document gets inserted in the modal. It's never a good idea to load an entire HTML page inside another HTML document, and "load" is designed to allow you to just pick the part of the document you want to insert, which is just that DIV.

    I added "#previouslink" after the php filename. This is where the magic happens. The browser knows to extract that DIV from your PHP file and insert just that part on the page, no <head> <body> or any of the unneeded markup.

    点赞 打赏 评论
  • drzb7969753 2012-05-24 01:18

    You can achieve what you're looking for by using AJAX to submit the form instead of using the default behavior where the page reloads with the new content. You can't use modal.load because you need to POST data in the request in order to get the appropriate response.

    However, when using AJAX to post your data, you can take the response as HTML and add that HTML to a DIV on your page, and then invoke the SimpleModal command on that DIV container.

    First, modify the submit button on your form so that it's type="button" instead of type="submit". This will prevent the form from submitting and redirecting the page. Alternatively, you could add event.preventDefault(); and return false to the form submit click handler (see the second step), but it's probably easier to try this method while you're making the changes:

    Step 1:

    <!-- change type to button -->
    <input id="searchquestion" name="searchQuestion" type="button" value="Search" />

    Step 2:

    Create a handler for the form button, which uses serializeArray to serialize the form into a string and then POST it to the server. In the success callback handler, you'll then take the HTML response and replace the content in the modal window with the new HTML. This also catches errors, if any, and alerts them and writes them to the console:

    $('form[type="button"]').click(function() {
      var dataString = $('form').serializeArray();
        url: "/previousquestions.php?t="+new Date().getTime(),
        type: "POST",
        data: dataString,           
        context: document.body,
        success: function(data){ 
       alert(data); // results from server, either the HTML page, or JSON/XML
           $('simplemodal-data').html(data);  // if HTML, just insert into div#results
        error: function(jqXHR, textStatus, errorThrown) {
        if(window.location.hostname == "localhost") {
            alert("Error submitting the form :: " + textStatus + " : " + errorThrown);                        
        console.error("Error submitting the form :: " + textStatus + " : " + errorThrown);

    Step 3:

    Lastly, be sure that your previousquestions.php code returns only a partial HTML document, not a full HTML document. Since you're injecting HTML into an existing page, you don't need the <html>, <head>, or <body> sections. These will just cause your page to not validate, and may cause undesired behavior in legacy browsers. Here is an example of what your response might look like:

    <div id="previouslink">
    <button type="button" id="close" onclick="return closewindow();">Close</button>
    <p>Search for a previous question by entering in a phrase in the search box below and submitting the phrase</p>
    <form action="previousquestions.php" method="post">
          <p>Search: <input type="text" name="questioncontent" value="test" /></p>
          <p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
             Your Search: 'test'     </p>
          <p>Number of Questions Shown from the Search: <strong>0</strong></p><p>Sorry, No Questions were found from this Search</p> 
    点赞 打赏 评论

相关推荐 更多相似问题