SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  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)
    It's a PHP variable so you need to wrap it in PHP tags:

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

  12. #12
    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.

  13. #13
    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

  14. #14
    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
    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?
    It's not defined by HTML specification thus it cannot be guaranteed that every browser will behave like this. Of course it may work but we should adhere to standards. As to why validator doesn't issue an error (or warning) -- that's a question for the W3C. I can only speculate that the reason is that the standard doesn't specifically require a URI, it only says that "behavior for a value other than an HTTP URI is undefined". In other words HTML specification doesn't say what the browser should do when the value isn't a URI.

    http://www.w3.org/TR/html401/interac...ml#adef-action

    EDIT: Actualy HTML4 specification does require a URI as a value of action attribute.

  15. #15
    SitePoint Member
    Join Date
    Jun 2007
    Location
    Finland
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    According to the HTML specification, the data type for the action attribute is uri, and the Basic HTML data types page then says that the specification uses the term URI as defined in RFC 2396. Here's a quote from the RFC:

    4.2. Same-document References

    A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document. Traversal of such a reference should not result in an additional retrieval action. However, if the URI reference occurs in a context that is always intended to result in a new request, as in the case of HTML's FORM element, then an empty URI reference represents the base URI of the current document and should be replaced by that URI when transformed into a request.
    int main()
    {

    // No pun indented.
    }

  16. #16
    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

  17. #17
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SamiP View Post
    According to the HTML specification, the data type for the action attribute is uri, and the Basic HTML data types page then says that the specification uses the term URI as defined in RFC 2396. Here's a quote from the RFC:
    It's clear that HTML4 specification does require action attribute to be a URI. It's too bad the specification is so ambiguous. Stating that "behavior for a value other than an HTTP URI is undefined" doesn't help at all. But it's been a long known fact that HTML specification is full of such ambiguous statements.

    EDIT: HTML5 is looking better, but it's still missing loads of points I would like to see clearly specified. Disabled attribute for option elements being at the top of my list. It seems the specification has changed since I last checked. I'm really glad.

  18. #18
    bronze trophy
    Join Date
    Dec 2004
    Location
    Sweden
    Posts
    2,670
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The empty string is a fine URI reference, as SamiP said.
    Quote Originally Posted by decowski View Post
    EDIT: HTML5 is looking better, but it's still missing loads of points I would like to see clearly specified. Disabled attribute for option elements being at the top of my list. It seems the specification has changed since I last checked. I'm really glad.
    Is there anything else on your list?
    Simon Pieters

  19. #19
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zcorpan View Post
    The empty string is a fine URI reference, as SamiP said.Is there anything else on your list?
    I must have had a dumb moment or a really bad day to miss the fragment SamiP had quoted (both when digging through HTML spec and reading the thread). Apologies for the confusion it might have caused.

  20. #20
    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...

  21. #21
    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 ?

  22. #22
    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

  23. #23
    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.

  24. #24
    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
  •