开发者

Create Php page that redirects to another page transmitting some content

I'm a beginner with PHP and I'd like to do the following but I have not a clue of how to 开发者_JS百科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).


You can pass city name in query parameter like

headers('Location: serachdatabase.php?city='.$cityname);

But that may not be what you are looking for. You can consider using Ajax to do this. Using Ajax the page will not refresh completely but only the portion of page can be refreshed. Ajax, if you dont know about it, is widely used.


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.


You can redirect using header("Location:...");

or you could simply just post a form

 <form method="post" action="yourURL">
     Postal code <input type='text' name='postal_code' /> <br/>
     <input type="submit" value="submit form" name="submit" />
 </form>

and then do in php something like this:

     <?php
        $postal = $_POST["postal_code"];

      // match against databse..
     ?>

Note that this code isn't "safe" to put it in a mysql query!


If you don't want any page redirect, you'll need to use either an iFrame or some CSS-id'd divs to load your content into using javascript's XMLHttpRequest or jQuery's axaj or similar to load info from another PHP page and insert it inside the current document.

Here is an example use of XmlHttpRequest, and Here is jQuery's API documentation on its ajax method.


The key to your problem's solution are GET and POST parameters. Learn about HTML forms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜