How to do an internal redirect to "self"

Discussion in 'PHP' started by JA12, Mar 26, 2009.

  1. #1
    :confused: I've been hunting through the search engines, but I don't know how to ask the question...

    I've got a PHP script that generates an HTML script with a FORM tag.

    The FORM tag action is to the same script.

    ask.php
    
    <?php
    ?>
    <html>
    etc
    <form action="ask.php" method="POST">
    <select name=question>
    <option value="1">A list item</option>
    </select>
    <input type=submit value="Ask">
    <form>
    etc
    <a name="1"></a>
    </html>
    
    Code (markup):
    I was expecting that when I clicked the "Ask" button, that HTML would automatically add the parameters to the submit

    
    ask.php?question=1
    
    Code (markup):
    It doesn't. I'm hoping that the POST value(s) is/are hidden and I can then get them using $_POST...

    My problem is, how do I redirect to the name I want? Preferably without reloading the script.

    
    ask.php#1
    
    Code (markup):
    I'm sure this is a real beginner question, but I'm just getting my head around scripting using PHP, and I've found that one really needs to know WHAT question to ask to get the information one needs!

    ------------------------------------------------------------
    I thought I could possibly use a combination of PHP and Javascript to achieve this, but...
    
    if ($_POST) {
      $getselected = $_POST['atb_id'];
      $location = 'ask_answer.php#' . $getselected;
    echo "$location";
    
    Code (markup):
    The echo here has what I want, for example ask_answer.php#24
    
    } else {
      $location = "";
    }
    ?>
    <html>
    <head>
    <title>TimeBank - Ask TimeBank</title>
    <script language="javascript">
    <?php if ($location) { ?>
    alert("<?php $location ?>");
    
    Code (markup):
    the alert here has nothing at all...
    
    location.replace("<?php $location ?>");
    <?php } ?>
    </script>
    
    Code (markup):
    ------------------------------------------------------------

    PHP Version 5.2.8
     
    JA12, Mar 26, 2009 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I assume that you want the browser to got the bookmark by selecting a form option rather that user clicking a link?

    one alternative is to use JavaScript on its own to capture the onChange event when they user selects an option and then execute a window.location =

    from a php perspective ... yes, the selected option will be in the $_POST variable .. if you want to see it in the url change this to $_GET ... either way I think you will need to reload the script to go to the bookmark ...

    something like this at the beginning :

     
    mallorcahp, Mar 26, 2009 IP
  3. JA12

    JA12 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that.

    I did look at header, but the documentation didn't appear to suggest what I needed...

    I've ended up combining a few examples, your's included!

    
    if (isset($_POST['question'])) {
      $host  = $_SERVER['HTTP_HOST'];
      $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
      $extra = 'ask_answer.php';
      $location = "http://$host$uri/$extra#" . $_POST['question'];
      header("Location: $location");
    }
    
    Code (markup):
     
    JA12, Mar 26, 2009 IP
  4. bhagwant.banger

    bhagwant.banger Active Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    60
    #4
    Well,

    If you are really new to php and form submission

    then why dont you try the firebug plugin for firefox

    where you can get all the information about the parameters passed by you to various places.

    please ask back if any further problems are there
     
    bhagwant.banger, Mar 26, 2009 IP