www.webdeveloper.com
Results 1 to 2 of 2

Thread: If Statement That Runs on Form Submit

  1. #1
    Join Date
    Oct 2013
    Posts
    1

    If Statement That Runs on Form Submit

    I have a real simple form where the use enters a word and hits submit. The word is then appended to the end of a url that I have in the code and redirects to the new url. The script I have is working but the only thing I can't figure out how to do is when it hits submit I want it to run an if statement first that checks the word they entered and if it's the exact match to a word we have in the if statement it redirects. If it doesn't match it says a line of text under the text box "Sorry that word is incorrect". Here's the working code. Any help would be GREATLY appreciated.

    Code:
     <?php
     if (isset($_POST['bt']))
     {
     header("Location: http://www.google.com/" . $_POST['folder']);
     }
     ?>
     <html>
     <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
     <input type="text" name="folder" id="folder" />
     <input type="submit" name="bt" id="bt" value="Go To" />
     </form>
     </html>

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    782
    Hi, if you mean validating the field on the client before submitting then here is an example:

    Code:
    <?php
    
    	if (isset($_POST['bt'])) {
    		header("Location: http://www.google.com/" . $_POST['folder']);
    	}
    
    ?>
    <html>
    	<head>
    		<script type="text/javascript">
    
    			function validate(form)
    			{
    				switch (form.folder.value) {
    					case 'word' :
    					case 'text' :
    					case 'another' :
    						//ALLOW FORM TO SUBMIT
    						return true;
    						break;
    				}
    				alert('Sorry that word is incorrect');
    				return false;
    			}
    
    		</script>
    	</head>
    	<body>
    		<form onsubmit="return validate(this);" id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    			<input type="text" name="folder" id="folder" />
    			<input type="submit" name="bt" id="bt" value="Go To" />
    		</form>
    	</body>
    </html>
    The code will currently only allow the form to submit if you enter one of these words: "word", "text" or "another"
    JavaScript: Learn | Validate | Compact

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles