dounong5373 2011-08-08 08:06
浏览 9
已采纳

创建Php页面,重定向到另一个页面传输一些内容

I'm a beginner with PHP and I'd like to do the following but I have not a clue of how to do this :

I have a webpage where I ask a user to submit his postal code. After he/she submits it the page redirects to another PHP page where I search in a datebase the city corresponding to the postal code. Then I write : "You're city is ...". What I'd like is to have this happen using only one webpage (with no visible redirection for the user). I know we can use header to redirect to the first page but I don't know how to transmit the content (city).

  • 写回答

5条回答 默认 最新

  • duancan1900 2011-08-08 08:16
    关注

    It sounds like you're looking for an AJAX post. This might sound like an advanced topic for a beginner, but if you check out a framework like jQuery (http://api.jquery.com/jQuery.post/) you'll see it's quite simple.

    Try something like this:

    $('#myform').submit(function() {
       var url = 'databasequery.php';
       var postcode = $('#postcode').val();
       $.post( url, { postcode: postcode },
          function( data ) {          
              $( "#result" ).empty().append( data );
          }
        );
      return false;
    });
    

    In your HTML you would tag your form as myform and create an empty div with the id "result".

    Your 'databasequery.php' file should accept a POST variable called postcode and simply output the response you want to display on your page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?