If Else Loop, with form input.

Discussion in 'PHP' started by Matt Ridge, Nov 16, 2011.

  1. #1
    I have a form I'm working on, actually it's a bit more than a generic form, I have it so that if data isn't in the database it will show a form which will allow you to submit the data.

    What I want to be able to do is push forward the data to the database, then reload the page, and show the data in place of where the input field is.

    I think I have the basics written down correctly, I need help figuring out how to make it so it can submit correctly. Most of the forms I've seen out there don't use an if else loop in this way.

    Can someone please help me?

    Here is the site: http://kaboomlabs.com/PDI/test.php
    Here is the code:

    <?php  require_once('connectvars.php');
    ?>
    
    
    <!DOCTYPE html 
    
    
         PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>PDI Non-Conforming Materials Report</title>
    <link rel="stylesheet" type="text/css" href="CSS/view.css" />
    </head>
    <body>
    </body> 
    </html>
    <?php
    
    
    echo '<form id="all">';
    echo '<fieldset>';
    echo '<div id="box4-1">';
    // We know both $ncmrsr AND $ncmrsc are blank
    $row['ncmrsr'] = trim($row['ncmrsr']);
    $row['ncmrsc'] = trim($row['ncmrsc']);
    if (empty($row['ncmrsr']) && empty($row['ncmrsc'])) {
         // code to add comment would go here.
    	 	echo '<div id="ncmrsr"><span class="b">NCMR Supplier Response:<br /></span><textarea name="ncmrsr" rows="6" cols="85">"N/A"</textarea></div><br />';
    		echo '<br />';
    		echo '<div id="ncmrsc"><span class="b">NCMR Supplier Comment:<br /></span><textarea name="ncmrsr" rows="6" cols="85" ></textarea></div><br />';
    		}
    
    
    		
    		
    else {
    // echo the two fields
    				if (!empty($row['ncmrsr'])) {
    					echo '<div id="ncmrsr"><span class="b">NCMR Supplier Response:<br /></span></td><td>' . $row['ncmrsr'] . '</div>';}
    				if (!empty($row['ncmrsc'])) {
    					echo '<div id="ncmrsc"><span class="b">NCMR Supplier Comment:<br /></span>' . $row['ncmrsc'] . '</div>';}
    					echo '</div>';
    echo '</div>';
    echo '</fieldset>';
    echo '</form>';
    
    
    }
    ?>
    
    Code (markup):

    There is a caveat here, this is box 4-1, that means that the form is already taking data from the database prior to this section... so I need to know how to make the code below work with the code above and allow me to input data...

    This is a wee bit more advanced than I am used to working with, so again, any help would be greatly appreciated.

    That code looks like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>PDI NCMR - View</title>
      <link rel="stylesheet" type="text/css" href="../CSS/view.css" />
    </head>
    <body>
       <div id="logo">
    	<img src="../images/PDI_Logo_2.1.gif" alt="PDI Logo" />
    </div>
    
    
    <?php
      require_once('../connectvars.php');
    
    
      // Connect to the database
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    
      // Grab the profile data from the database
      if (!isset($_GET['id'])) {
        $query = "SELECT * FROM ncmr WHERE id = '$id'";
      }
      else {
        $query = "SELECT * FROM ncmr WHERE id = '" . $_GET['id'] . "'";
      }
      $data = mysqli_query($dbc, $query);
    
    
      if (mysqli_num_rows($data) == 1) {
        // The user row was found so display the user data
        $row = mysqli_fetch_array($data);
        echo'<h3 id="NCMR2">Non-Conforming Materials Report (NCMR:&nbsp;&nbsp;' . $row['NCMR_ID'] . ')</h3>';
    		echo '<form id="all">';
    			echo '<fieldset>';
    
    etc..
    etc...
    etc....
    
    Code (markup):
     
    Matt Ridge, Nov 16, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Your problem comes about because the text areas have the same name. That needs to change to
    [FONT=monospace]<textarea name="ncmrsr[]" rows="6" cols="85" >[/FONT]
    Code (markup):

    or
    <textarea name="ncmrsr[{$i}]" rows="6" cols="85" >
    Code (markup):
    where i is some sort of counter

    Then in your handling script you can see
    if count($_POST['ncmrsr']) >0
    and then do a foreach to save the data
     
    sarahk, Nov 16, 2011 IP