• PHP Help PHP Coding
  • Inserting a default value into a form's blank text input field using explode function

the following form collects data about the color preference of the visitors.

how can i insert a default value (For example, "No Color Chosen" is default value here) using explode function if the users submit the blank form when i don't want to prevent users from submitting the blank form.

i came up with the following syntax

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {	
$color = "No Color Chosen ({$_POST['color_name']};)";
$colors = explode (" ", $color);

$insertSQL = sprintf("INSERT INTO colortable (color_id, color_name) VALUES (%s, %s)",

GetSQLValueString($_POST['color_id'], "int"),
GetSQLValueString(trim($colors[]), "text"),

  mysql_select_db($database_x, $x;
  $Result1 = mysql_query($insertSQL, $x) or die(mysql_error());

  $insertGoTo = "choice.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
  <tr valign="baseline">
    <td align="right" nowrap="nowrap">Enter Color Name:</td>
    <td><input type="text" name="color_name" value="" size="32" /></td>
  </tr>
   <tr valign="baseline">
    <td nowrap="nowrap" align="right"><input type="hidden" name="MM_insert" value="form1" /></td>
                    <td>
                    <input type="submit" value="Submit Color Name" />
                    </td>
                  </tr>
                </table>
              </form>
              </td>
            </tr>
      </table>

how should i define the following value as conditional statement of form's text input value in this case so that the default value [FONT=Comic Sans MS](<?php echo $_POST($colors[0]);?> shall insert the default value "No Color Chosen")[/FONT] is inserted on blank form submission ?

value=

"<?php echo $_POST($colors[0]);?>"
"<?php echo $_POST($colors[1]);?>"

thanks,

    For one, the PHP code you posted contains numerous parse errors. Might want to clean up your code before posting it in the future if you expect it to help explain what it is you're wanting to do.

    Ignoring that, there are more errors that further confuse the matter. For example, you've got this:

    $colors = explode (" ", $color);

    which means $colors is an array, but then you later have this:

    GetSQLValueString(trim($colors), "text")

    which treats $colors as if it were a string. Thus, not only have you shown us code that won't parse, but you've also shown us code that makes no sense and contradicts itself. And that is ignoring the fact that you're operating under the (false, IMHO) assumption that no color names contain spaces (what happened to the colors "ruby red" or "robbin egg blue" ??).

    Finally, on to the matter of your question:

    polarexpress;11015555 wrote:

    how should i define the following values as conditional statement of form's text input value in this case?

    Are you asking how to define a variable based on user input, if it is set/nonempty, or a default value, if otherwise? Sounds like you just need a simple [man]if/man statement that checks if a given external variable is [man]empty/man.

      bradgrafelman;11015559 wrote:

      For one, the PHP code you posted contains numerous parse errors. Might want to clean up your code before posting it in the future if you expect it to help explain what it is you're wanting to do.

      Ignoring that, there are more errors that further confuse the matter. For example, you've got this:

      $colors = explode (" ", $color);

      which means $colors is an array, but then you later have this:

      GetSQLValueString(trim($colors), "text")

      which treats $colors as if it were a string. Thus, not only have you shown us code that won't parse, but you've also shown us code that makes no sense and contradicts itself. And that is ignoring the fact that you're operating under the (false, IMHO) assumption that no color names contain spaces (what happened to the colors "ruby red" or "robbin egg blue" ??).

      Finally, on to the matter of your question:

      Are you asking how to define a variable based on user input, if it is set/nonempty, or a default value, if otherwise? Sounds like you just need a simple [man]if/man statement that checks if a given external variable is [man]empty/man.

      thanks for pointing out my mistake. i'm learning to love PHP. i have just edited the syntax. if my syntax is correct now how should i define the conditional value based on explode function so that the value "No Color Chosen" is inserted into database if the visitors submit the blank form.

        can i insert the default value alternatively without explode function if the text input field remains blank? how?

          I don't even see why explode() would come into play at all. All you're wanting to do is:

          if(empty($_POST['some_field_name'])) {
              // form field was left blank
          } else {
              // form field was NOT left blank - use it
          }

          EDIT: In fact, if the "use it" part of the example above is simply going to be "assign it to a variable," then you could shorten the code down to a single line using the ternary operator, e.g.:

          $var = empty($_POST['some_field_name']) ? 'default value' : $_POST['some_field_name'];
            bradgrafelman;11015567 wrote:

            I don't even see why explode() would come into play at all. All you're wanting to do is:

            if(empty($_POST['some_field_name'])) {
                // form field was left blank
            } else {
                // form field was NOT left blank - use it
            }

            EDIT: In fact, if the "use it" part of the example above is simply going to be "assign it to a variable," then you could shorten the code down to a single line using the ternary operator, e.g.:

            $var = empty($_POST['some_field_name']) ? 'default value' : $_POST['some_field_name'];

            i understand that defining the text input value as

            <input type="text" name="color_name" value="<?php echo $_POST[$var];?>" />

            based on the ternary operator you defined is sufficient enough to serve the purpose but i was curious to learn about how can the syntax i posted serves the same purpose based on explode function.

            thanks a lot,

              polarexpress;11015571 wrote:

              i understand that defining the text input value as

              <input type="text" name="color_name" value="<?php echo $_POST[$var];?>" />

              based on the ternary operator you defined is sufficient enough to serve the purpose

              Er, no, you'd simply want to use $var in that case; $_POST[$var] doesn't make any sense (unless both the user-supplied input and the default value happen to exactly match the name of a POST'ed form entity).

              polarexpress;11015571 wrote:

              but i was curious to learn about how can the syntax i posted serves the same purpose based on explode function.

              Again, I have no idea why you're wanting to create an array, so I have no idea how doing so could "serve the same purpose."

                bradgrafelman;11015575 wrote:

                Er, no, you'd simply want to use $var in that case; $_POST[$var] doesn't make any sense (unless both the user-supplied input and the default value happen to exactly match the name of a POST'ed form entity).

                Again, I have no idea why you're wanting to create an array, so I have no idea how doing so could "serve the same purpose."

                but be truthfully

                <input type="text" name="color_name" value="<?php echo $var;?>" />

                doesn't insert the value of blank text field into mysql database on form submission. mysql gives error

                Column 'color_name' cannot be null
                  polarexpress;11015579 wrote:

                  but be truthfully

                  <input type="text" name="color_name" value="<?php echo $var;?>" />

                  doesn't insert the value of blank text field into mysql database on form submission. mysql gives error

                  Column 'color_name' cannot be null

                  These are separate issues - in your example, you're not even trying to insert something into a database.

                  You're defining it as the value of a form input (which is likely to be changed or deleted when the user fills out the form).

                    traq;11015583 wrote:

                    These are separate issues - in your example, you're not even trying to insert something into a database.

                    You're defining it as the value of a form input (which is likely to be changed or deleted when the user fills out the form).

                    that's not right. i want to insert the value of database column color_name even though the users submit a blank form. you haven't understood my question at all then.

                      I did understand that - but now, you're doing something else. Why are you putting the value in a form input if you want it to be in the database?

                        traq;11015617 wrote:

                        I did understand that - but now, you're doing something else. Why are you putting the value in a form input if you want it to be in the database?

                        isn't it the form's input value that shall be inserted into the database?

                        for example, in the following code the value of $var shall be inserted into the database.

                        <input type="text" name="color_name" value="<?php echo $var;?>" />

                        what i'm asking is a predefined value (not available in database) shall be inserted into database if only the visitors don't input any value into form field before they submit it. otherwise, if they input their own value (say, red) into the text filed the database shall accept that value.

                          So check the value of [font=monospace]$_POST['color_name'][/font]. If it's empty, set it to the default value.

                            polarexpress;11015623 wrote:

                            for example, in the following code the value of $var shall be inserted into the database.

                            <input type="text" name="color_name" value="<?php echo $var;?>" />

                            Actually no, nothing about that line has anything to do with a database. This is what traq was trying to point out to you. All this line does is pre-populate a form field with whatever the value is in the $var variable. It does not, in any way, have anything to do with a database. If you are having a problem with inserting into the database you should really show the code that actually has to do with a database.

                              Derokorian;11015641 wrote:

                              Actually no, nothing about that line has anything to do with a database. This is what traq was trying to point out to you. All this line does is pre-populate a form field with whatever the value is in the $var variable. It does not, in any way, have anything to do with a database. If you are having a problem with inserting into the database you should really show the code that actually has to do with a database.

                              <input type="text" name="color_name" value="<?php echo $var;?>" />

                              form input value here not only pre-populate the value of variable in form form field but also it posts a predefined value of variable into the database.

                              Aren't we always insert hidden value of a form input through the above syntax.

                              my question is very simple & clear here and i already posted sufficient codes related to my question. what more code you are expecting?

                              [ATTACH]4701[/ATTACH]
                              [ATTACH]4703[/ATTACH]

                              among the above forms if the visitors submit the blank one, it shall insert a predefined value (not available in database) into the database.

                              On the other hand, if the visitors submit the one that is filled with the value "Green", it shall insert the value "Green" into the database.

                              my question was how to do it using explode function or any other alternative method?

                                polarexpress wrote:

                                using explode function

                                See, this is one of the reasons why everyone's getting confused. More progress would be made if you answered questions that were asked.

                                bradgrafelman wrote:

                                Why are you even using explode() in the first place?

                                It makes us think that you have some idea about what [man]explode[/man] is for, and that it has some relevance to your question. But now it seems (ignoring the apparently spurious irrelevancies about how you set initial values in an HTML form field) that your question is something that has an answer given in the very first reply to your original post.

                                bradgrafelman wrote:

                                Are you asking how to define a variable based on user input, if it is set/nonempty, or a default value, if otherwise? Sounds like you just need a simple [man]if/man statement that checks if a given external variable is [man]empty/man.

                                with sample code that does the job given an hour and a half later.

                                  the following code actually what i was looking for in case the explode function isn't effective in my case.

                                  $color_name = "No Color Chosen";
                                  if(isset($POST['color_name']) && !empty($POST['color_name'])){

                                  $color_name = $_POST['color_name'];

                                    That's what I thought you meant.

                                    The code that bradgrafelman suggested earlier in the thread does the same thing, and is more succinct:

                                    bradgrafelman;11015567 wrote:
                                    $var = empty($_POST['some_field_name']) ? 'default value' : $_POST['some_field_name'];

                                    I'm still not clear why you were trying to use the result as the [font=monospace]value[/font] of your form inputs, though.

                                    The reason we're confused about your attempt to use [man]explode/man is that it doesn't do anything even close to what you say you're trying to do.

                                      polarexpress wrote:
                                      if(isset($_POST['color_name']) && !empty($_POST['color_name']))

                                      From the definition of [man]empty[/man] you can figure out that the [man]isset[/man] here is redundant.