SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating my form

    I have the following code working, in that it inserts into a database if there are values entered in the input boxes and if not, nothing is entered. However when I press submit to test when there is nothing in the boxes the error message isn't returned. From looking at the code below I was wondering if someone might know why the error message is not being printed out. Is there something I'm missing

    input.php

    Code:
    <?php
    
    
    echo '<form method = "post" action ="confirm.php">';
    
    
    ?>
    
    <table>
    <tr>
    <td>name 1</td>
        <td><input name="name1" type="text" value="<?php if(isset($_POST) && isset($_POST['name1'])){
    		echo $row['name1'];
    	}
    	?>"/></td>
        <td><span> <?php if(isset($errors)&& isset($errors['name1'])){
    		echo $errors['name1'];
    	}
    	?>
        </span></td>
    </tr>
    <tr>
    <td>name 2</td>
    <td><input name="name2" type="text" value="<?php if(isset($_POST)&& isset($_POST['name2'])){
    		echo $_POST['name2'];
    	}
    	?>" /></td>
        <td><span> <?php if(isset($errors)&& isset($errors['name2'])){
    		echo $errors['name2'];
    	}
    	?>
        </span></td>
      </tr>
    <tr>
    <td>&nbsp;</td><td><input type = "submit" name = "submit" value = "Sent"></td><td></td>
    </tr>
    </table>
    </form>
    When I click the submit button when the input boxes are empty it directs me to confirm.php but no error messages show. How can I get the error message working. Any help would be great.

    confirm.php

    Code:
    $valid = true;
    
    $errors = array();
    
    if(empty($_POST['name1'])){
    	$valid = false;
    	$errors['name1'] = 'enter name1';
    }
    elseif(empty($_POST['name2'])){
    	$valid = false;
    	$errors['name2'] = 'enter name 2';
    }
    
    	
    	if($valid == true) 
    	{
    	echo 'Validation complete';

  2. #2
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a similar issue while trying to use arrays and found using variables much easier. Have a look at this tutorial :

    http://www.w3schools.com/php/php_form_required.asp


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
  •