Inserting text into textbox after submit button is clicked

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWest46088
    New Member
    • Sep 2006
    • 74

    Inserting text into textbox after submit button is clicked

    I am working in Dreamweaver CS4 and have a contact form. I am using PHP to send the info. I have a disabled textbox that I want to use to display whether the contact form was sent successfully or failed. How would I send a success or failure message to this disabled textbox after the submit button is clicked? Thanks in advance!
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    Dreamweaver is just an editor and has no effect on what you will be doing. Why are you using a text box for form feedback? And why do you expect this to be done in the same page?

    Is your form processing performed purely server-side or client-side with AJAX?

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      A simple example using pseudo code
      Code:
      if(isset($_POST['submit']))
          echo '<input type="text" value="'.$message.'"/>';
      else
        echo '<input type="text" value=""/>disabled';

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        I think you meant to place "disabled" inside of the element as an attribute, not outside of it.

        And OP said the textbox is *always* disabled. I'd just like for him to explain what he is trying to accomplish, as I have never seen his methodology before.

        Comment

        • JWest46088
          New Member
          • Sep 2006
          • 74

          #5
          kovik,

          I want to show the user some sort of notification that the message they sent using the contact form on the website was sent successfully. So, I want to display a message, like "Your email was sent successfully," in the textbox after they click the submit button.

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            Then do it the normal way: By showing them the messages. A good way to do this, in its most basic form, is a boolean variable to indicate success and an array to indicate error messages. Here's an example controller and view file:

            Controller:
            Code:
            <?php
            $success = false;
            $errors = array();
            
            if (!empty($_POST)) {  // Form has been submitted
              // Validate all form elements and add errors
              //   to the $errors array when invalid
              if (empty($_POST['name'])) {
                $errors[] = 'Name must be filled.';
              }
            
              // Perform the operation if the form is valid
              if (empty($errors)) {
                // Do stuff here
                $success = true;  // Set $success = true if successful
              }
            }
            ?>

            View:
            Code:
            <?php if ($success) { ?>
            <p>Successful!</p>
            <?php } else { ?>
            <form method="post" action="#">
              <?php if (!empty($errors)) { ?>
              <ul>
                <?php foreach ($errors as $error) { ?>
                <li><?php echo $error; ?></li>
                <?php } ?>
              </ul>
              <?php } ?>
              <input type="text" name="name" />
              <button type="submit">Submit</button>
            </form>
            <?php } ?>

            Comment

            • JWest46088
              New Member
              • Sep 2006
              • 74

              #7
              I added this to my php code, but the textbox is showing up when the page is loaded. How can I fix that so it only shows up after the submit button is clicked? I thought that was what isset was supposed to do.

              Code:
              <?php
              if(isset($_POST['submit']))
              {
              	echo '<input name="result" type="text" disabled="disabled" id="result" size="50" maxlength="25" value="Your email was sent successfully."/>';
              ?>

              Comment

              • kovik
                Recognized Expert Top Contributor
                • Jun 2007
                • 1044

                #8
                Firstly, depending on the browser, "submit" may not always be set. I think it was IE that had the problem, but there's a browser that does no set "submit" if the user presses Enter instead of typing the button. It's more reliable to check if the $_POST array is empty or not.

                Secondly, you never close the brackets of the if statement.

                Thirdly, you haven't shown us much. I don't know what you want us to do with this, knowing nothing at all about your page.

                Just follow the example I gave you.

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  Kovik, I am with you the fact the OP is providing little information.

                  But you have an irritating habit of correcting minor errors.
                  I think you meant to place "disabled" inside of the element as an attribute, not outside of it.
                  I never supply perfect code, unless a ready made function, because I don't have time.
                  Hence
                  A simple example using pseudo code
                  which admittedly was a little bit more, but it was simply an idea, not a ready made solution

                  Comment

                  • kovik
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1044

                    #10
                    Your solution was wrong and didn't address the problem that the OP was asking. You can't blame me for trying to stop the OP from becoming anymore confused. This is why my first response was a question and not a solution: to make the problem more clear for us all.

                    Comment

                    • code green
                      Recognized Expert Top Contributor
                      • Mar 2007
                      • 1726

                      #11
                      This is why my first response was a question and not a solution:
                      And that is why my first response was a pseudo-code example and not a solution

                      Comment

                      • kovik
                        Recognized Expert Top Contributor
                        • Jun 2007
                        • 1044

                        #12
                        Why are you defending it? You either make an input box with a message or one with disabled written next to it. At least correct it to fit the post...

                        Comment

                        Working...