+ Post New Thread
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 47
Coding Thread, PHP form in Coding and Web Development; Originally Posted by Steve21 Can you try: Code: <input type="submit" name="submit" id="submit" value="Submit" onclick="javascript :document.sports.submit();javascript :document.sports.reset();" /> Didn't think it ...
  1. #31

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    6,875
    Thank Post
    393
    Thanked 581 Times in 532 Posts
    Rep Power
    173
    Quote Originally Posted by Steve21 View Post
    Can you try:

    Code:
    <input type="submit" name="submit" id="submit" value="Submit" onclick="javascript:document.sports.submit();javascript:document.sports.reset();" />
    Didn't think it was fussy enough but maybe it's being silly O.o or I'm just missing something obvious.

    Steve
    That doesn't seem to be working.

  2. #32

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,394
    Thank Post
    108
    Thanked 266 Times in 197 Posts
    Rep Power
    168
    The best way IMO to do this is to submit the form to a new page that processes the data and then returns the user to the form page...that should clear $_POST so that the form is blank...

  3. #33

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,394
    Thank Post
    108
    Thanked 266 Times in 197 Posts
    Rep Power
    168
    @steve21

    I don't think the javascript approach will work because the first statement submits the page to itself and I think the second statement will then not get run...

    The form reset function works for a button on a page to clear the form after it has been filled in but not after the form is submitted.

  4. #34

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    6,875
    Thank Post
    393
    Thanked 581 Times in 532 Posts
    Rep Power
    173
    Not being a proficient coder I'm now proper confused!

  5. #35

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,662
    Thank Post
    324
    Thanked 506 Times in 474 Posts
    Rep Power
    177
    Sorry I disappeared, was busy at work.

    Can you try this:

    Code:
    <style type="text/css">
    /*Contact form*/
    label {
    width:12em;
    float:left;
    text-align:right;
    margin-right:1.5em;
    display:block
    }
    .submit input {
    margin-left:4.5em;
    }
    input.text {
    border:1px solid #232a15
    margin:2px;
    border:1px solid #A8A8A8
    font-family:Verdana,Arial,Helvetica,sans-serif;
    }
    textarea {
    border: 1px solid #232a15
    margin: 2px;
    border:1px solid #A8A8A8
    font-family:Verdana,Arial,Helvetica,sans-serif;
    }
    fieldset {
    border:1px solid #232a15
    padding: 4px;
    border:1px solid #A8A8A8
    }
    p.error, p.success {
       font-weight: bold;
       padding: 10px;
       border: 1px solid;
      }
      p.error {
       background: #ffc0c0
       color: #900
      }
      p.success {
       background: #b3ff69
       color: #4fa000
      }
    </style>
    <form name="sports" action="<?php echo basename(__FILE__); ?>" method="post" onSubmit
    ="javascript:document.sports.reset();" >
    <noscript>
      <p><input type="hidden" name="nojs" id="nojs" /></p>
    </noscript>
         <fieldset>
         <div>
         <label for="name">Name</label>
         <input type="text" class="text" name="name" id="name" size="30" value="<?php if (isset($_POST['name']) && !empty($_POST['name'])) echo $_POST['name']; ?>" />
        </div>
         <div>
         <label for="pupilsname">Pupils Name (if different)</label>
         <input type="text" class="text" name="pupilsname" id="pupilsname" size="30" value="<?php if (isset($_POST['pupilsname']) && !empty($_POST['pupilsname'])) echo $_POST
    
    ['pupilsname']; ?>" />
        </div>
         <div>
         <label for="tutorgroup">Tutor group</label>
         <input type="text" class="text" name="tutorgroup" id="tutorgroup" size="30" value="<?php if (isset($_POST['tutorgroup']) && !empty($_POST['tutorgroup'])) echo $_POST
    
    ['tutorgroup']; ?>" />
        </div>
        <div>
         <label for="pupilssportactivity">Pupils sport/activity</label>
         <input type="text" class="text" name="pupilssportactivity" id="pupilssportactivity" size="30" value="<?php if (isset($_POST['pupilssportactivity']) && !empty($_POST
    
    ['pupilssportactivity'])) echo $_POST['pupilssportactivity']; ?>" />
        </div>
        <div>
         <label for="pupilsachievement">Pupils achievement</label>
         <textarea rows="6" cols="40" name="pupilsachievement" id="pupilsachievement" value="<?php if (isset($_POST['pupilsachievement']) && !empty($_POST['pupilsachievement'])) 
    
    echo $_POST['pupilsachievement']; ?>" /></textarea>
        </div>
         <div>
         <label for="email">E-mail</label>
         <input type="text" class="text" name="email" id="email" size="30" value="" />
        </div>
        <div class="submit">
         <input type="submit" name="submit" id="submit" value="Submit" />
        </div>
        </fieldset>
       </form>
    Think I was being silly, and put it in wrong box

    Steve

  6. #36

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    6,875
    Thank Post
    393
    Thanked 581 Times in 532 Posts
    Rep Power
    173
    Problem with that is that it appears to clear the form before it sends and I get:

    ERROR: Please fill in all the required fields and submit again.
    Please fill in all the required fields and submit again.
    Please fill in all the required fields and submit again.
    Please fill in all the required fields and submit again.
    Please fill in all the required fields and submit again.
    That is not a valid e-mail address.

  7. #37

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,662
    Thank Post
    324
    Thanked 506 Times in 474 Posts
    Rep Power
    177
    Quote Originally Posted by Edu-IT View Post
    Problem with that is that it appears to clear the form before it sends and I get:
    Meh I dislike webstuff! Give me coding any day :P

    Honestly not sure, only thing I can think of doing is turning it into a function it calls. Then add the submit, then clear in that function.

    Steve

  8. #38

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,394
    Thank Post
    108
    Thanked 266 Times in 197 Posts
    Rep Power
    168
    If you want the fields empty after form submission then why are you doing this
    Code:
    <?php if (isset($_POST['name']) && !empty($_POST['name'])) echo $_POST['name']; ?>
    The field will not be emptied if the form validation fails.

  9. #39

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    6,875
    Thank Post
    393
    Thanked 581 Times in 532 Posts
    Rep Power
    173
    I give up. I think I'll find some other way of doing it.

  10. #40

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,374
    Thank Post
    625
    Thanked 951 Times in 653 Posts
    Blog Entries
    2
    Rep Power
    318
    Edu-IT: Make a new pastebin or update the old one with the current code you have, and please explain what you'd like it to and what it's doing wrong at the moment; and I'll have a look for you.

  11. #41

    Edu-IT's Avatar
    Join Date
    Nov 2007
    Posts
    6,875
    Thank Post
    393
    Thanked 581 Times in 532 Posts
    Rep Power
    173
    webman - Pastebin.com

    Trying to get it to submit the form and on submit, clear the fields.

    Ideally, also, instead of displaying multiple 'please complete all fields' just display the name of the frields that need to be completed. Something like 'Please enter your name, email address and message'. At the moment it just says 'please complete all fields' over and over again if you submit a blank form.

  12. #42

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,394
    Thank Post
    108
    Thanked 266 Times in 197 Posts
    Rep Power
    168
    Sorry I wasn't trying to confuse you...honestly

    I think all you need to do is add code to set a flag once a successful submission has been made and then use that flag to decide whether to fill the form fields in or not. Something like this.

    Code:
    Set flag to Not OK
    Do form validation
    If OK then submit data
    If successful set flag to OK
    If flag is OK Show form without data
    If flag is not OK then show form with existing data
    Does that make any sense?

  13. #43

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,394
    Thank Post
    108
    Thanked 266 Times in 197 Posts
    Rep Power
    168
    Oops forgot the error message part...

    Instead of adding a complete sentence each time an error is detected as here
    Code:
    		if (!isset($_POST[$field]) || empty($_POST[$field]))
    			$error_msg .= "Please fill in all the required fields and submit again.\r\n";
    	}
    Start the error message outside the test loop and just add the name of each field that is blank to the message
    Code:
    	
    $error_msg = "Please enter the following data, ";
    foreach($requiredFields as $field) {
    	trim($_POST[$field]);
    		
    	if (!isset($_POST[$field]) || empty($_POST[$field]))
    		$error_msg .= "$field,";
    }
    $error_msg .= "\r\n";
    Last edited by CESIL; 27th May 2011 at 01:01 PM. Reason: Messed up the code!

  14. #44

    webman's Avatar
    Join Date
    Nov 2005
    Location
    North East England
    Posts
    8,374
    Thank Post
    625
    Thanked 951 Times in 653 Posts
    Blog Entries
    2
    Rep Power
    318

  15. #45

    CESIL's Avatar
    Join Date
    Nov 2006
    Location
    Hampshire
    Posts
    1,394
    Thank Post
    108
    Thanked 266 Times in 197 Posts
    Rep Power
    168
    @webman...nice idea using the error message as the flag...I should have thought of that but I am feeling a bit jaded today...might be because we celebrated my son's 21st last night

SHARE:
+ Post New Thread
Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. [PHP] Form to Email.php
    By tomgrindle in forum Web Development
    Replies: 4
    Last Post: 26th April 2011, 12:26 PM
  2. HTML/PHP form problem
    By glennda in forum Web Development
    Replies: 4
    Last Post: 17th November 2010, 10:22 AM
  3. PHP - Form POST script
    By BarryWAaMC in forum Coding
    Replies: 9
    Last Post: 31st March 2010, 11:32 AM
  4. [PHP] PHP Form Generator
    By _Bat_ in forum Web Development
    Replies: 1
    Last Post: 25th March 2009, 11:39 AM
  5. PHP form validation
    By thesk8rjesus in forum Web Development
    Replies: 10
    Last Post: 21st January 2009, 12:09 AM

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
  •