Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts

    Help with form handling and nesting of conditionals

    I have a form called submit.php wherein the user is asked to submit an entity name, location (country) of the entity, and a brief description of the entity.

    Here is the code for the submit.php page:
    PHP Code:
    <?php
        ob_start
    ();
        
    session_start();
        require_once (
    'includes/config.inc.php');
        
    $page_title 'Dataconia | Submit New Entity';
        
    $page_description 'Dataconia | Submit New Entity; submission by user of new organization within the Dataconia database';
        
    $page_keywords 'submit new entity, submission, company, organization, Dataconia, quantifying perception, brands, products, rating, reviews';
        
    $page_author 'Kylan Hurt';
        require_once(
    'includes/header.html');
        
    // Welcome the user (by name if they are logged in):
            
    echo '<h2>New Entity Submission ';
            if (isset(
    $_SESSION['first_name'])) {
            echo 
    "by {$_SESSION['first_name']}!";}
            echo 
    '</h2>';
            
    if (isset(
    $_POST['submitted'])) 
        {
    // Start of main $_POST conditional +1 = 1
            
    require_once (MYSQL);
            
    //Trim all field of the post / form
            
    $trimmed array_map('trim'$_POST);
            
    //Assume invalid values
            
    $uid $en $c $ed FALSE;
            
    $errors = array(); // Initialize an error array.
            //Validate User
            
    if (!isset($_SESSION['user_id'])) // if post is submitted and user_id is not set
                
    {
                    echo 
    '<p class="error">You must be logged in to submit a new entity</p>';
                } else {    
    $uid $_SESSION['user_id'];             //if post is submitted and user_id IS set
                        
    if (isset($_POST['entity_name']))    {        //if post is submitted, user_id is set, and entity_name is set
                                
    $en mysqli_real_escape_string ($dbc$trimmed['entity_name']);
                        } else     { 
    // if post is submitted, user_id is set, but entity_name not set
                                
    echo '<p class="error">Please enter a name for the entity you are trying to submit.</p>'
                                } 
    // end of else clause for no entity name
                        
    if (isset($_POST['country'])) 
                            { 
    // if post is submitted, user_id is set, and country isset
                                
    if ($_POST['country'] = "...") {
                                    echo 
    '<p class="error">You did not enter a valid country.</p>';
                                }
                                else {
    $c mysqli_real_escape_string ($dbc$trimmed['country']);}
                                }
                             else     {
                                        echo 
    '<p class="error">Please enter the country in which the entity is based.</p>'//+1 = 2
                                    
    }         // end of else clause for entity country presence // -1 = 1
                            
    if (isset($_POST['description']))    { // start of entity description conditional +1 = 2
                                
    $ed mysqli_real_escape_string ($dbc$trimmed['description']); 
                            } else     {
                                    echo 
    '<p class="error">Please enter a description for the entity being submitted:</p>'// +1 = 2
                                    
    }// end of else clause for entity description field -1 = 1 
                            
    if ($uid && $en && $c && $ed) { // If everything's OK...            //+1 = 2        
                                
    $q "SELECT entity_id FROM entities WHERE entity_name = '$en'";
                                
    $r mysqli_query ($dbc$q) or trigger_error("Query: $q\n<br />MySQL Error: " mysqli_error($dbc));
                                if (
    mysqli_num_rows($r) == )    { // then the space is available //+1 = 3, Add the user to the database:
                                    
    $q "INSERT INTO entities (entity_name, entity_location, entity_description, date_of_creation, created_by) VALUES ('$en', '$c', '$ed', 'NOW()', '$uid' )";
                                    
    $r mysqli_query ($dbc$q) or trigger_error("Query: $q\n<br />MySQL Error: " mysqli_error($dbc)); // Close of mysqli_num_rows positive-1 = 2
                                    
    if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. +1 = 3
                                        
    echo '<p>Thank you for your submission.</p>';
                                        include (
    'includes/footer.html');
                                        
    ob_end_flush(); 
                                    } else     {
    //if mysqli_affected_rows is not 1
                                        
    echo '<p class="error">Your submission could not be processed at this time due to a system error. We apologize for any inconvenience.</p>';
                                }
                                } else     { 
    // the entity name is not available +1-1 = 2
                                    
    echo '<p class="error">The entity name that you entered has already been submitted.  Please try submitting a different entity.</p>';
                            }
                            } else    { 
    //
                                
    echo '<p class="error">Please fill out the form again.</p>';}
                                }
        }

                                                                                
                            
    ?>
    I will leave the HTML out of it for now unless someone wants me to include it but for some reason when I try filling out the form I always get my own coded error messages of "You did not enter a valid country" and "Please fill out the form again." Even when the form seems to be filled out correctly.

    Nesting conditionals is something that is new territory to me as I would put myself as a "beginner" when it comes to PHP and MySQL. Can anyone here help me to identify my mistake? Any help would be greatly appreciated!

    Edit: I figure it might help to include the rest of the code for the form as well. Here it is:
    Code:
    <div class="form" style="width:90%; margin: 0 auto 0 0;">
    <form action="submit.php" method="post" style="padding:0 0 0 20px;">
    <p>Please fill out the following form in order to submit a new entry to the Dataconia database:</p>
    <table>
    	<tr><td class="form_left" style="text-align:right;">Entity Name:</td><td class="form_right" style="text-alight:left;"><input type="text" name="entity_name" size="30" maxlength="100" value="<?php if (isset($_POST['entity_name'])) echo $_POST['entity_name']; ?>" />
    	* </td></tr>
        <tr><td class="form_left"> Country: </td><td class="form_right">  <select name="country">
    	<option value="...">...</option>
        <option value="United States">United States</option>
    	<option value="Afganistan">Afghanistan</option>
    ...
    ...
    ...
    
    	<option value="Zimbabwe">Zimbabwe</option>
    	</select>
        </td></tr>
        <tr><td class="form_left" style="text-align:right;">Please write a description for the entity:</td><td><textarea rows="15" cols="60" name="description"></textarea></td></tr>
    </table>
    <p><input style="text-align:center;" type="submit" name="submit" value="Submit New Entry" /></p>
    	<input type="hidden" name="submitted" value="TRUE" />
    </form>
    </div>
    
    
    
    
    <?php
    	require('includes/footer.html');
    		ob_end_flush();
    ?>
    Last edited by d'Anconia; 04-23-2012 at 10:52 PM.
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #2
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts
    LOL I just found out my problem. For the conditional checking if $_POST['country'] is equal to the string ".." I forgot to put two equals signs instead of one.

    Easy mistake but a bit amateurish =P oh well I'm glad I figured it out.
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    tip: swap variable and value like if ("..." == $_POST['country']) {. if you should accidentally write = instead of ==, you’ll get a syntax error (wherein the other case it becomes an assignment).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder d'Anconia's Avatar
    Join Date
    Jan 2010
    Location
    Tempe, AZ
    Posts
    149
    Thanks
    16
    Thanked 6 Times in 6 Posts
    Got it, Dormilich. I remember someone mentioning that tip before and maybe it's about time I started using it.
    Datagonia Web (My Portfolio)

    Powerful ideas for all lovers of personal and political freedom:
    Freedomain Radio
    Free Talk Live


  •  

    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
    •