SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Send post values with header location

    Hello everyone,

    I'm using a form to send some values to another page, but how does it work if I use the header function? Once the form has been submitted, how do I access the $name and $employer variables on the receiving page? The form also has a hidden input with a value.


    Please help me out. Thank you so much!



    PHP Code:
    <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors = array();
        
    if (empty(
    $_POST['name'])) {
            
    $errors['name'] = 'Please enter your name.';
        } else {
            
    $name filter_var($_POST['name'], FILTER_SANITIZE_STRING);
        }
        
        
        if (empty(
    $_POST['employer'])) {
            
    $errors['employer'] = 'Please add your employer.';
        } else {
            
    $employer filter_var($_POST['employer'], FILTER_SANITIZE_STRING);
        }
        
        if (
    $name && $employer ) { //    
    header("location: page3.php ");
    exit ();    
    }

    }

    HTML Code:
    <form name="myform" action="" method="post">
    
    <p>Name</p>
    <input type="text" name="name" value="" />
    <span class="warning"><?php if (isset($errors['name'])) echo $errors['name']; ?> </span>
    
    <p>Employer</p>
    <input type="text" name="employer" value="" />
    <span class="warning"><?php if (isset($errors['employer'])) echo $errors['employer']; ?> </span>
    
    <input name="somename" type="hidden" value="somevalue"/>
    <input type="submit" value="send">
    </form>

  2. #2
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a PHP session. On the redirect page, start the session and assign the $_POST data to the $_SESSION:

    PHP Code:
    <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors = array();
        
    if (empty(
    $_POST['name'])) {
            
    $errors['name'] = 'Please enter your name.';
        } else {
            
    $name filter_var($_POST['name'], FILTER_SANITIZE_STRING);
        }
        
        
        if (empty(
    $_POST['employer'])) {
            
    $errors['employer'] = 'Please add your employer.';
        } else {
            
    $employer filter_var($_POST['employer'], FILTER_SANITIZE_STRING);
        }

            
    session_start();
            
    $_SESSION $_POST;
            
    session_write_close();
        
        if (
    $name && $employer ) { //    
    header("location: page3.php ");
    exit ();    
    }

    }

    HTML Code:
    <form name="myform" action="" method="post">
    
    <p>Name</p>
    <input type="text" name="name" value="" />
    <span class="warning"><?php if (isset($errors['name'])) echo $errors['name']; ?> </span>
    
    <p>Employer</p>
    <input type="text" name="employer" value="" />
    <span class="warning"><?php if (isset($errors['employer'])) echo $errors['employer']; ?> </span>
    
    <input name="somename" type="hidden" value="somevalue"/>
    <input type="submit" value="send">
    </form>

    Then on page3.php, restart the session and set the $_SESSION data to the $_POST. Then use as normal:

    PHP Code:
    <?php
    session_start
    ();
    $_POST $_SESSION;
    ?>
    Check Out Syntaxxx.com - HTML5, CSS3, PHP, jQuery & More

  3. #3
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there threehundred,

    thanks for your help.

    I didn't think that it would be necessary to use a session. I have found that this also works:

    header("location: page3.php&name=$name ");

    But not sure if this is an acceptable solution. Normally when I use forms I use the mail function to send form values to an email address. This is the first time that I'm sending the values to another page, and I'm uncertain if the header function has to be used at all. Without validating the values I could send the values as is to another page, but with validation I don't know what to do after: if ($name && $employer ).

    Is the header function the standard way to send form data to another page?

    Thank you!

  4. #4
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The header function would be the standard way, at least when a redirect is needed.

    You can do header("location: page3.php&name=$name ") which is much easier but that uses $_GET rather than $_POST.

    $_POST is typically used when handling forms
    Check Out Syntaxxx.com - HTML5, CSS3, PHP, jQuery & More

  5. #5
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thank you.


    Could someone else please confirm that using header(location) together with either a session or appending a query string to the location URL is the only way to send post values via a form to another page - given how I have structured my validation code. And without using JavaScript/Ajax.


    I just need to be sure.


    Thanks!

  6. #6
    SitePoint Member
    Join Date
    Jan 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    yes, session is the best way for it.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mushira.


    Hi fretburner, cpradio and others...


    what would you recommend? I don't really want to use sessions because not every visitor will have cookies enabled.


    Please point me in the right direction. Thank you very much!

  8. #8
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,257
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    Hey RedBishop,

    Good weekend?

    Quote Originally Posted by RedBishop View Post
    what would you recommend? I don't really want to use sessions because not every visitor will have cookies enabled.
    Do you have a reason to think that many of your visitors would have cookies disabled? If it is an issue, PHP can pass the session ID as a URL parameter rather than in a cookie.

  9. #9
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good weekend?
    Yeah, good thanks and you?


    Do you have a reason to think that many of your visitors would have cookies disabled?

    I for one mostly have cookies disabled and probably quite a few other people do so as well. But if I know I need to enable cookies to use a site like sitepoint.com then it isn't such a big deal. Thank you for the link to the PHP manual. I have come across some info advising against using session IDs in URLs because of negative SEO consequences. But don't know if this is true or not.

  10. #10
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RedBishop View Post
    I have come across some info advising against using session IDs in URLs because of negative SEO consequences. But don't know if this is true or not.
    Yes, because session ID is part of the URL and it is different every time a search engine robot crawls your site in effect the URL's of your pages change all the time and this may not be SEO friendly. But if you use the session ID only for your form then what's the problem? Search engines don't and aren't supposed to be filling in and submitting forms on your site anyway, so appending session ID's to form URL's will not do any harm.

  11. #11
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, because session ID is part of the URL and it is different every time a search engine robot crawls your site in effect the URL's of your pages change all the time and this may not be SEO friendly. But if you use the session ID only for your form then what's the problem? Search engines don't and aren't supposed to be filling in and submitting forms on your site anyway, so appending session ID's to form URL's will not do any harm.

    Yes, that all makes sense. Thanks for the clarification.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •