Hello,

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

	$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 '..').

    $name = ($_POST['name'] == '') ? 'John' : $_POST['name'] ;
    

    That is the short version of

    if ($_POST['name'] == '') {
        $name = 'John';
    else {
        $name = $_POST['name'] ;
    }
    

      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 [man]isset/man (or [man]empty/man, depending upon my needs) in the conditional instead, e.g.:

      $name = (!isset($_POST['name']) ? 'John' : $_POST['name']);

        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?

          neljan;10962685 wrote:

          EDIT Brad, is there a close bracket missing in yours?

          Nooo.... I don't see any missing bracket.... :evilgrin:

            lol my mistake then, sorry.

            XD

              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

              	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

                  <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 🙂

                You could either use something like [man]str_pad/man or you could use [man]sprintf/man, 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:

                $num = sprintf('&#37;03s', $num);

                  Thanks, would you happen to know how I could implement that into my code?

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

                  Close?

                    What is this:

                    '1' sprintf('&#37;03s', $LC_m)

                    supposed to mean?

                      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.

                        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.

                          bradgrafelman;10962698 wrote:

                          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.

                          $num = sprintf('%03s', $num);

                          Means nothing to me, I dont even have $num in my code.

                          bradgrafelman;10962698 wrote:

                          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.

                          bradgrafelman;10962698 wrote:

                          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.

                            ...and I just realised I used the wrong example (should be '001' not '1'):

                            $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:

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

                              Ok, not to rehash the past but brad did ask a legitimate question.

                              This

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

                              is the same as this

                              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

                              $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)

                              '001' sprintf('%03s', $LC_ms)
                              

                              At minimum you need to join the 2 strings (note the period (.))

                              '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. 😃

                                but if "$LC_ms" is "5", for example, that will output "001005", is that what you're looking for?

                                Heres what I'm looking for:

                                if $LC_ms = ' ' then $LC_ms = '001' (default for empty submission)
                                elseif $LC_ms = only 1 digit instead of 3 digits then $LC_ms = '00#' (example: 5 = 005, 4 = 004)
                                elseif $LC_ms = ony 2 digits instead of 3 digits then $LC_ms = '0##' (example: 55 = 055, 44 = 044)
                                else $LC_ms = $_POST['LC_ms'];

                                So I'm guessing:

                                if($_POST['LC_ms'] == '') {
                                    $LC_ms =  '001';
                                }
                                else {
                                    $LC_ms =  $_POST['LC_ms'] . sprintf('%03s', $LC_ms);
                                } 

                                (if it cant be written in 1 line, which was what I tried to do initially).

                                EDIT

                                That gave me 1000 for an input of '1', so I changed it to %02s to get 100, but I would like to know how I can have the zero's to the left (001) if you would be so kind.

                                EDIT

                                	if($_POST['LC_ms'] == '') {
                                         $LC_ms =  '001';}
                                        else {
                                         $LC_ms = sprintf('%02s', $LC_ms) . $_POST['LC_ms'];}

                                This will submit 001 when 1 is entered, but it also submits 0011 if 11 is entered (need it to submit 011).

                                  Oh wow I did it 😃

                                  Found this in the link Brad gave:

                                  http://uk.php.net/manual/en/function.str-pad.php#86404

                                  So I did this:

                                  if($_POST['LC_ms'] == '') {
                                               $LC_ms =  '001';}
                                             else {
                                               $LC_ms = str_pad((int) $_POST['LC_ms'], 3, "0", STR_PAD_LEFT);}

                                  Thanks, I hope I wont be annoying you again for atleast another hour or so 😉

                                    Dont forget Brad said always use isset() just to make sure it will not throw E notice
                                    this is the shorter version of your syntax:

                                    $LC_ms = isset($_POST['LC_ms']) && $_POST['LC_ms'] == '' ? '001' : str_pad((int) $_POST['LC_ms'], 3, "0", STR_PAD_LEFT);
                                      Write a Reply...