Results 1 to 4 of 4

Thread: Form validation

  1. #1
    Join Date
    Sep 2017
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Form validation

    how to send form to next page after validation through header('location:sucess.php');
    please suggest me
    below find my code


    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php 
    	// define variables and set to empty values
    $nameErr = "";
    $name = "";
    	
    	if ($_SERVER["REQUEST_METHOD"] == "POST") {
    		if (empty($_POST["name"])) {
        $nameErr = "Name is required";
      } 	
            if(isset($_POST['state'])) { 
                if($_POST['state'] == 'NULL') { 
                    echo '<p>Please select state.</p>'; 
                } }
    			
            if(isset($_POST['city'])) { 
                if($_POST['city'] == 'NULL') { 
                    echo '<p>Please select city.</p>'; 
    				 }
    		}	
    		}
    		?> 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
        <fieldset> 
            <legend>Please select a state</legend> 
            <select name="state"> 
               <option value="NULL">-- Please select a state --</option> 
                <option value="Member" <?php if(isset($_POST['state']) && $_POST['state']=="Member") { ?>selected<?php  } ?>>Member</option>
    <option value="New" <?php if(isset($_POST['state']) && $_POST['state']=="New") { ?>selected<?php  } ?>>New</option>
                <option value="one" <?php if(isset($_POST['state']) && $_POST['state']=="one") { ?>selected<?php  } ?>>one</option>
    
    </select> 
            
            <select name="city"> 
               
                <option value="NULL">-- Please select a state --</option> 
                
                <option value="Member" <?php if(isset($_POST['city']) && $_POST['city']=="Member") { ?>selected<?php  } ?>>Member</option>
    <option value="New" <?php if(isset($_POST['city']) && $_POST['city']=="New") { ?>selected<?php  } ?>>New</option>
                <option value="one" <?php if(isset($_POST['city']) && $_POST['city']=="one") { ?>selected<?php  } ?>>one</option>
    
    
                </select>
                <input type="text" name="phone">
                
                 <input type="tel" name="name" id="name" value="<?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?>"> <?php echo $nameErr;?>        
            <input type="submit" name="submit" value="submit"> 
        </fieldset> 
    </form>
    </body>
    </html>
    Last edited by jscheuer1; 09-24-2017 at 01:22 AM. Reason: format code

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I don't see "header" anywhere in your code. So obviously it's not going to:

    Code:
    header('location:sucess.php');
    if that's not in the code.

    One thing I can tell you is that you need to send headers before you write to the page, so do all your calculation/evaluation that may result in:

    Code:
    header('location:sucess.php');
    before the opening <!doctype html> tag. Do them also before any echo statements, etc., before anything that would cause the server to send text or formatting data to the browser.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Something like:

    Code:
    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST" and !empty($_POST["name"]) and isset($_POST['state']) and ($_POST['state'] !== 'NULL') and isset($_POST['city']) and $_POST['city'] !== 'NULL'){
    header('location:sucess.php');
    }
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php 
    	// define variables and set to empty values
    $nameErr = "";
    $name = "";
    	
    	if ($_SERVER["REQUEST_METHOD"] == "POST") {
    		if (empty($_POST["name"])) {
        $nameErr = "Name is required";
      } 	
            if(isset($_POST['state'])) { 
                if($_POST['state'] == 'NULL') { 
                    echo '<p>Please select state.</p>'; 
                } }
    			
            if(isset($_POST['city'])) { 
                if($_POST['city'] == 'NULL') { 
                    echo '<p>Please select city.</p>'; 
    				 }
    		}	
    		}
    		?> 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
        <fieldset> 
            <legend>Please select a state</legend> 
            <select name="state"> 
               <option value="NULL">-- Please select a state --</option> 
                <option value="Member" <?php if(isset($_POST['state']) && $_POST['state']=="Member") { ?>selected<?php  } ?>>Member</option>
    <option value="New" <?php if(isset($_POST['state']) && $_POST['state']=="New") { ?>selected<?php  } ?>>New</option>
                <option value="one" <?php if(isset($_POST['state']) && $_POST['state']=="one") { ?>selected<?php  } ?>>one</option>
    
    </select> 
            
            <select name="city"> 
               
                <option value="NULL">-- Please select a state --</option> 
                
                <option value="Member" <?php if(isset($_POST['city']) && $_POST['city']=="Member") { ?>selected<?php  } ?>>Member</option>
    <option value="New" <?php if(isset($_POST['city']) && $_POST['city']=="New") { ?>selected<?php  } ?>>New</option>
                <option value="one" <?php if(isset($_POST['city']) && $_POST['city']=="one") { ?>selected<?php  } ?>>one</option>
    
    
                </select>
                <input type="text" name="phone">
                
                 <input type="tel" name="name" id="name" value="<?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?>"> <?php echo $nameErr;?>        
            <input type="submit" name="submit" value="submit"> 
        </fieldset> 
    </form>
    </body>
    </html>
    But that won't send any data anywhere. What exactly do you want to have happen?

    You probably want the action of the form to be sucess.php but have it send the user back to the form page if the information is incomplete.

    There are advanced methods whereby you can send the post data along, or you could send it relatively easily as get data. But generally what is done is to use javascript to validate the form, if it passes, send the data along in the normal fashion to the action page/php script.
    Last edited by jscheuer1; 09-24-2017 at 02:45 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    pkrishna42 (09-24-2017)

  5. #4
    Join Date
    Sep 2017
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    hey thanks for your help its working great..........thanks a lot

Similar Threads

  1. Use Spry Validation with jQuery Form to Form Wizard (v1.1)
    By dreamwuser in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 01-05-2012, 11:57 PM
  2. Resolved PHP form Validation
    By john0611 in forum PHP
    Replies: 1
    Last Post: 05-10-2009, 03:40 PM
  3. Form validation
    By akluch2 in forum JavaScript
    Replies: 0
    Last Post: 02-19-2009, 02:30 PM
  4. Form Validation
    By calumogg in forum JavaScript
    Replies: 0
    Last Post: 04-16-2008, 05:57 PM
  5. Form Validation
    By chrish110 in forum JavaScript
    Replies: 7
    Last Post: 08-19-2006, 09:58 PM

Bookmarks

Posting Permissions

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