[RESOLVED] $_POST default values
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] $_POST default values

  1. #1
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113

    resolved [RESOLVED] $_POST default values

    Hello,

    Can someone please tell me how I can set a default value to be submitted if a user enters nothing when using $_POST?

    Code:
    	$name = $_POST['name'];
                    if($name == '') $_POST['name'] = "John";
    (Something like that).

    I have default values set within SQL table but for some reason they dont show when the user enters nothing (it shows '..').
    Last edited by neljan; 09-04-2010 at 12:12 PM.

  2. #2
    Bug Killer
    Join Date
    Jun 2008
    Location
    Wisconsin
    Posts
    665
    PHP Code:
    $name = ($_POST['name'] == '') ? 'John' $_POST['name'] ; 
    That is the short version of
    PHP Code:
    if ($_POST['name'] == '') {
        
    $name 'John';
    else {
        
    $name $_POST['name'] ;

    Some Handy Resources:
    php.net for PHP
    W3Schools.com for HTML, Javascript, and CSS
    MySQL 5 Reference Manual
    And if they all fail use the
    Top Secret Web Developer Tool

  3. #3
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    Also note that directly accessing an external variable without first verifying that it exists will generate an E_NOTICE level error message if it wasn't set (e.g. if the form wasn't submitted yet, or that field wasn't included). As such, I prefer to use isset() (or empty(), depending upon my needs) in the conditional instead, e.g.:

    PHP Code:
    $name = (!isset($_POST['name']) ? 'John' $_POST['name']); 
    Last edited by bradgrafelman; 09-04-2010 at 04:36 PM.

  4. #4
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113
    Krik, I think I'm falling in love with you ... thank you once again.

    And thank you bradgrafelman for your take on it.

    Much appreciated guys.

    *EDIT* Brad, is there a close bracket missing in yours?
    Last edited by neljan; 09-04-2010 at 04:22 PM.

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    Quote Originally Posted by neljan View Post
    *EDIT* Brad, is there a close bracket missing in yours?
    Nooo.... I don't see any missing bracket....

  6. #6
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113
    lol my mistake then, sorry.

    XD

  7. #7
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113
    I have another question...

    How could I have an input field set so that if a user submits a single number, it gets submitted as 2 digits?

    > user submits '1' = gets submitted as '01'.

    I also need it for 3 digits:

    > User submits '1' = gets submitted as '001'.

    Heres what I have:

    .php
    Code:
    	if(isset($_POST['submit'])) {
    
            $LC_m = ($_POST['LC_m'] == '') ? '1' : $_POST['LC_m'];
    	$LC_s = ($_POST['LC_s'] == '') ? '12' : $_POST['LC_s'];
    	$LC_ms = ($_POST['LC_ms'] == '') ? '000' : $_POST['LC_ms'];
    
    	$LC = $LC_m.'.'.$LC_s.'.'.$LC_ms;
    .html
    Code:
        <input type="text" name="LC_m" id="LC_m" value="$LC_m" size="2" maxlength="1" onkeypress="return isNumberKey(event)" />
        '
    	<input type="text" name="LC_s" id="LC_s" value="$LC_s" size="3" maxlength="2" onkeypress="return isNumberKey(event)" />
        '
        <input type="text" name="LC_ms" id="LC_ms" value="$LC_ms" size="4" maxlength="3" onkeypress="return isNumberKey(event)" />
    Many thanks my saviours

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    You could either use something like str_pad() or you could use sprintf(), depending upon your needs. For example, if you want to left-pad a string so that it contains at least three characters, you could do something like:

    PHP Code:
    $num sprintf('%03s'$num); 

  9. #9
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113
    Thanks, would you happen to know how I could implement that into my code?

    Code:
    $LC_m = ($_POST['LC_m'] == '') ? '1' sprintf('&#37;03s', $LC_m) : $_POST['LC_m'];
    Close?

  10. #10
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    What is this:
    PHP Code:
    '1' sprintf('%03s'$LC_m
    supposed to mean?

  11. #11
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113
    Pardon me, but I dont appreciate you answering my question with a retorical question, implying that I'm stupid or something. You know fully well I have no idea what it's supposed to mean since this is a 'newbie' forum and you're the one who was helping me (remember?).

    If you dont have the time/patience to help me then just dont... thank you.

  12. #12
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,349
    What I know is that the above code snippet confused me, so before I even try to help you with the syntax I would at least have to know what you're attempting to achieve. If you wanted the default value to be '001', then simply put the string '001' there instead.

    However, since you no longer want help from me, I'll gladly unsubscribe from and ignore this thread.

  13. #13
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113
    Quote Originally Posted by bradgrafelman View Post
    What I know is that the above code snippet confused me, so before I even try to help you with the syntax I would at least have to know what you're attempting to achieve.
    I already said what I was attempting to achieve and provided my code. If you really wanted to help me with the syntax you could've posted it instead of the snippet. You must have known that your snippet wouldn't of been much help to me on its own and I would've had to experiment with the syntax.

    I dont expect people to post precise answers that I can just c/p into my code and off I go, but then some kind of hint as to how I could add a snippet into my existing code would be appreciated.

    Code:
    $num = sprintf('%03s', $num);
    Means nothing to me, I dont even have $num in my code.

    Quote Originally Posted by bradgrafelman View Post
    If you wanted the default value to be '001', then simply put the string '001' there instead.
    I already have the defaults, that problem was solved.

    Quote Originally Posted by bradgrafelman View Post
    However, since you no longer want help from me, I'll gladly unsubscribe from and ignore this thread.
    No problem, thanks for teaching me the snippet and syntax terminologies.

  14. #14
    Senior Member
    Join Date
    Jul 2010
    Location
    London
    Posts
    113
    ...and I just realised I used the wrong example (should be '001' not '1'):

    Code:
    $LC_ms = ($_POST['LC_ms'] == '') ? '001' sprintf('%03s', $LC_ms) : $_POST['LC_ms'];
    Not that it makes much difference since it doesn't work anyway.

    Also tried:

    Code:
    $LC_ms = ($_POST['LC_ms'] == '') ?  '001' : $_POST['LC_ms'] sprintf('%03s', $LC_ms);
    Code:
    $LC_ms = sprintf('%03s', $LC_ms) ($_POST['LC_ms'] == '') ? '001' : $_POST['LC_ms'];

  15. #15
    Bug Killer
    Join Date
    Jun 2008
    Location
    Wisconsin
    Posts
    665
    Ok, not to rehash the past but brad did ask a legitimate question.

    This
    PHP Code:
    $LC_ms = ($_POST['LC_ms'] == '') ? '001' sprintf('%03s'$LC_ms) : $_POST['LC_ms']; 
    is the same as this
    PHP Code:
    if($_POST['LC_ms'] == '') {
        
    $LC_ms =  '001' sprintf('%03s'$LC_ms);
    }
    else {
        
    $LC_ms =  $_POST['LC_ms'];

    Now you may not have picked up on it but this line
    PHP Code:
    $LC_ms =  '001' sprintf('%03s'$LC_ms); 
    is not correct syntax hence why brad asked about this line (well, this line before it was changed)
    PHP Code:
    '001' sprintf('%03s'$LC_ms
    At minimum you need to join the 2 strings (note the period (.))
    PHP Code:
    '001' sprintf('%03s'$LC_ms
    but if "$LC_ms" is "5", for example, that will output "001005", is that what you're looking for?

    I suspect that is not the number you want and you should just drop the 001 from the code.


    Lastly I will add an advisory note. You probably shouldn't send moderators packing, especially ones that are smarter than me.
    Some Handy Resources:
    php.net for PHP
    W3Schools.com for HTML, Javascript, and CSS
    MySQL 5 Reference Manual
    And if they all fail use the
    Top Secret Web Developer Tool

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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