SitePoint Sponsor

User Tag List

Results 1 to 24 of 24

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to append input ?

    Working with registration form. I need all the input remain if the form can not process due to missing field, or email and username provided are unvailable.

  2. #2
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you doing your vaildation?

    If you are doing javascript validation then you would never have left the page and thus the information would still be there.

    If you are doing some kind of server side validation (like php), then all your form variables would be in the $_GET or $_POST super global arrays, and you can thus access you variables like such

    Code:
      $name_var = $_POST['name'];
    The above assume you use the POST method on your form, and had a variable called name, and you stuck it into a variable called $name_var.
    You can then use that variable to populate the form, like so

    Code:
      <form method="post" action="$_SERVER['php_self']">
        <input type="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
        <input type="submit" />
      </form>
    Hope this helps

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by poizn View Post
    Code:
      <form method="post" action="$_SERVER['php_self']">
        <input type="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
        <input type="submit" />
      </form>
    HTML Code:
    <input type="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
    type="name"???

    Is this a valid type in HTML form?

    I would do like this:
    - Submit the form to the same page.
    Suppose I have a page mypage.php
    PHP Code:
    <?php
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if(!empty(
    $_POST['my_name']) && !empty($_POST['my_address'])){
            
    # process your code further
            # redirect to some page
        
    }
        
    # else the page will remain posted. 
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        <meta name="author" content="3261706071">
        <title>Test</title>
    </head>
    <body>
    <form action="" method="post" name="frmreg">
        Name:<input type="text" name="my_name" id="my_name" value="<?php echo $_POST['my_name'];?>" />
        Address:<input type="text" name="my_address" id="my_address" value="<?php echo $_POST['my_address'];?>" />
        <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
    </form>
    </body>
    </html>
    Hope you understand what I am doing.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dope, my bad, well picked up there

    This

    Code:
    <input type="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
    should be this

    Code:
    <input type="text" name="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
    .

    You have implemented the logic correctly, it should work fine

  5. #5
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      <form method="post" action="$_SERVER['php_self']">

    What does this $_SERVER['php_self'] do ?

  6. #6
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    What does this $_SERVER['php_self'] do ?
    http://uk2.php.net/manual/en/reserve...les.server.php

  7. #7
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does this $_SERVER['php_self'] do ?
    Its a variable that holds the location of the current page (to sum it up)


  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make sure to run $_SERVER['PHP_SELF'] through htmlspecialchars() otherwise you risk xss attacks. Same goes for when you echo the variables back into the form fields.

  9. #9
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, when i put that in form action, run the page. An error message "$_SERVER['PHP_SELF'] is not available in your system or site" something like that.

    I will try again and get the correct message post here

  10. #10
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    ok, when i put that in form action, run the page. An error message "$_SERVER['PHP_SELF'] is not available in your system or site" something like that.

    I will try again and get the correct message post here
    Rather than using $_SERVER['PHP_SELF'], simply leave the action="" empty it will work for you. That means it will post the form to the same page. I have shown on my example above.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  11. #11
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Rather than using $_SERVER['PHP_SELF'], simply leave the action="" empty it will work for you. That means it will post the form to the same page. I have shown on my example above.
    It works (in most browsers, anyway) but this behaviour is not defined by HTML standard:

    Quote Originally Posted by W3C
    action = uri [CT]
    This attribute specifies a form processing agent. User agent behavior for a value other than an HTTP URI is undefined.

  12. #12
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    but this behaviour is not defined by HTML standard:
    What you mean? I have never seen that. I have done in that way in hundreds of websites but never heard that. The sites are passed with w3 validator as well. Maybe I did not try to see anywhere. Can you please elaborate bit more about this issue?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  13. #13
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a PHP variable so you need to wrap it in PHP tags:

    Code:
    <form method="post" action="<?php echo $_SERVER['php_self']; ?>">

  14. #14
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Although it has to be there, in XHTML Strict validation anyway (and who ISN'T using at least XHTML transitional by now?)
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #15
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    (and who ISN'T using at least XHTML transitional by now?)
    Me
    I've never had a good reason to use an xhtml doctype. I don't parse my output as xml so...

  16. #16
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about doing this with select and radio ?

  17. #17
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by runrunforest View Post
    how about doing this with select and radio ?
    What you mean? Can you please say something more in the question?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  18. #18
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    append chosen radio option and select option.

    Basically, in previous discussion we echo the data to the value attribute of text field/input to append it.

    But radio button and menu/list need to have their own value at all time, so I don't think of how could I append it.

  19. #19
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Me
    I've never had a good reason to use an xhtml doctype. I don't parse my output as xml so...
    Yeh, I've started using HTML strict again recently. XHTML isn't supported by IE, and I've been swayed by the arguments on here (I like the XHTML syntax, but HTML is more practical / correct until the problems with XHTML are sorted out.. and even then it won't be suitable for a lot of applications). I may convert some of my existing sites to HTML strict as well.


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
  •