POST data

Discussion in 'PHP' started by anurag.sharma, Jun 26, 2008.

  1. #1
    I have an html form(say data.html) that will POST data to a php file(say, a.php). I wish to have that php page (a.php) to submit the same data to another php (say, b.php) file (using POST function) with out any input from the user.

    Please suggest some way.
     
    anurag.sharma, Jun 26, 2008 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    You could try using some javascript;

    <body onload='document.myform.submit();'>
    
    <form name='myform' action='mypage.php' method='POST'>
    
    <input type='hidden' name='field1' value='<?=$_POST['field1']?>' />
    
    </form>
    HTML:

    Alternatively you could use sessions and store your posted variables in the session and just redirect the user to the 2nd page;

    <?php
    session_start();
    foreach($_POST as $field => $value){
    
      $_SESSION[$field] = $value;
    
    }
    
    header('Location: mypage.php');
    ?>
    PHP:
     
    Weirfire, Jun 26, 2008 IP
  3. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Couldn't you just do $_SESSION = $_POST instead of that loop?

    Dan
     
    Danltn, Jun 26, 2008 IP
  4. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks for the help.

    but have some problem in implement ion

    Here is the code I used

    <?php
    preg_match('/I am from ([a-z]+)/i',$_POST['search_var'],$match);
    #print_r($_POST);
    #print_r($match);
    session_start();
    if(isset($_POST['search_var'])) {
    $_SESSION = $_POST
    header('Location: '.$match[1].'.php');
    exit;

    ?>


    I want the user to be redirected to the next page and prints the data he entered.

    If he had entered the data as

    "I am from usa. How are you?" He should be redirected to usa.php and that page should display

    "I am from usa. How are you?"
     
    anurag.sharma, Jun 26, 2008 IP
  5. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    So you want to have a data form, a user enters data, the data submits and shows the user what data he has entered, then finally he confirms??

    If this is the case then you can just pass the variables to the a.php script which will do the relevant database work or woteva. This script can then build then html placing the appropriate variable values in the appropriate places on the page. The user can then click confirm to complete which will run b.php to do final database work and finally display success page.

    Im not too sure on what you are asking but that is how I would do what I think your asking :)
     
    projectWORD, Jun 26, 2008 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    lol yeah that works for me too. I'd like to hire you to reduce my code. :D
     
    Weirfire, Jun 27, 2008 IP
  7. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yes. that is what i need. could you please reedit the code for me?
     
    anurag.sharma, Jun 27, 2008 IP
  8. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I am having some problem while using

    header('Location: '.$match[1].'.php');


    in session

    Please help
     
    anurag.sharma, Jun 27, 2008 IP
  9. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Danltn, could you help?
     
    anurag.sharma, Jun 27, 2008 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Then please do. :D

    ___________________________________

    Your first HTML file I assume you already have... This goes to the first PHP, I'm not going to include any decent validation here as I assume you can do it yourself...

    <?php

    /**
    * Danltn | http://danltn.com/
    * MSN:
    * Email:
    * Started: 27/6/2008 15:02 (UK)
    * Tested: No
    * PHP 4/5: Both
    * No warranty is given to code used
    */

    $buffer = ''; // Initialize the variables.
    $hidden = '';

    $_POST = array_map('htmlentities', $_POST); // Very basic validation, stops XSS

    foreach($_POST as $key => $value) :
    $hidden .= '<input type="hidden" name="' . $key . '" value="' . $value . '" />' . "\n";
    $buffer .= ucwords(strtolower($key)) . ': ' . $value . '<br />' . "\n";
    endforeach;

    unset($key, $value); // Remove what we've used, get some more memory

    ?>
    <form method="post" action="b.php">
    Your input:<br />
    <?php echo $buffer , $hidden; ?>
    Submit: <input type="submit" name="submit" value="It's all correct!" />
    </form>
     
    Danltn, Jun 27, 2008 IP
  11. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    But the automatic redirection will not work in this case.

    If the user enters the data as

    "I am from usa. How are you?" He should be redirected to usa.php and that page should display

    "I am from usa. How are you?"

    And if he enters

    "I am from uk. How are you?" He should be redirected to uk.php and that page should simply display

    "I am from uk. How are you?"

    A simple redirection the respective page (say usa.php or uk.php) is what I require (along with the POST data).

    I require your help in this aspect
     
    anurag.sharma, Jun 28, 2008 IP
  12. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Can also use curl to send to a second script.
     
    itnashvilleCOM, Jun 28, 2008 IP
  13. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #13
    I think he needs a little more guidance than that. Would you care to expand upon your post?
     
    Weirfire, Jun 28, 2008 IP
  14. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Could you please explain with some code?
     
    anurag.sharma, Jun 28, 2008 IP
  15. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    anurag.sharma, Jun 28, 2008 IP
  16. anurag.sharma

    anurag.sharma Guest

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    can any one help?
     
    anurag.sharma, Jun 30, 2008 IP
  17. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Why not just use a hidden form? Once "a.php" passes data to "b.php" through a form using POST, simply receive the value in "b.php" through $_POST['val'] and place them in a hidden form for sending to "c.php" also through POST. This allows you to pass information to an infinite number of pages using POST.

    Alternatively, instead of having to list down every variable you receive form POST, you can use this instead which mimics the config "register_globals=on" when in reality it's really off:
    
    //makes variables of the "name" attribute of your forms and
    //assigns the "value" attribute to it automatically. Cool yah?
    foreach ($_POST as $key => $val){
        $$key = $val;
    }
    PHP:
    If you have a form with name="address", you can simply access its value as $address without having to go through
    $address = $_POST['address'];
    PHP:
     
    enchance, Jun 30, 2008 IP
  18. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #18
    enchance:

    Well if you want vulnerabilities, just use:

    extract($_POST);

    (It's the same as that loop, but with more options ;) )
     
    Danltn, Jul 6, 2008 IP