Posting from a simple form.

Discussion in 'PHP' started by karl_murphy, Feb 3, 2009.

  1. #1
    I have created a simple form, which contains two drops down menus and a submit button. When the form is submitted the values selected in the drop down menus update a database. Is it possible to refresh the screen once the form is submitted to show these updates? At the moment I have to refresh the screen manually in order to see the updates. The code is as follows:

    $sql = "SELECT * FROM `Config`
    					WHERE `CFG_ID` = '16'";
    					
    					include("../includes/php/connect.php");
    			
    			while ($row = mysql_fetch_assoc($rst))
    			{
    				$yr9_check = $row['CFG_Value'];		
    			}
    			
    			$sql = "SELECT * FROM `Config`
    					WHERE `CFG_ID` = '17'";
    					
    					include("../includes/php/connect.php");
    			
    			while ($row = mysql_fetch_assoc($rst))
    			{
    				$yr9_type = $row['CFG_Value'];		
    			}				
    			
    			if($yr9_check == 0)
    			{
    				echo "<h4>Year 9: No Subject Trawl or Internal Review has been submitted for this year group.</h4>";
    			}
    			else
    			{			
    				echo "<h4>Year 9: Currently set to <font color = '#B84340'>Check " . $yr9_check . "</font> as <font color = '#B84340'>"; if($yr9_type == "S"){echo "Subject Trawl";} elseif($yr9_type == "I"){echo "Internal Review";}"</font></h4>";
    			}	
    			?>
    			<form method = "post" action = "<?php echo $PHP_SELF;?>">
    				<table cellpadding="4" cellspacing="4">
    					<tr>
    						<td valign="top">
    							<h4><font color="#000000">Set to:</h4>
    						</td>
    						<td valign="top">
    							<select name = "check">
    								<option value = "1">Check 1</option>
    								<option value = "2">Check 2</option>
    								<option value = "3">Check 3</option>	
    								<option value = "4">Check 4</option>
    								<option value = "5">Check 5</option>
    								<option value = "6">Check 6</option>
    							</select>
    						</td>
    						<td valign="top">
    							<h4><font color="#000000">and</h4>
    						</td>
    						<td valign="top">
    							<select name = "type">
    								<option value = "S">Subject Trawl</option>
    								<option value = "I">Internal Review</option>
    							</select>
    						</td>
    						<td valign="top">
    						<input type = 'submit' value = 'Submit' name = 'submit' onclick = 'javascript: return confirm("Are you sure you wish to continue?")' />
    						<input type = 'hidden' name = 'submitted' value = 'true'>
    						</td>
    					</tr>
    				</table>
    				</form>
    			<?php
    			
    			$check = $_POST['check'];
    			$type = $_POST['type'];		
    			
    			if (isset($_POST['check'])) 
    			{
    			 	$sql2 = "UPDATE `Config` SET `CFG_Value` = '$check' WHERE `CFG_ID` = '16'";
    				include("../includes/php/connect2.php");
    			}
    			
    			if (isset($_POST['type'])) 
    			{
    			 	$sql2 = "UPDATE `Config` SET `CFG_Value` = '$type' WHERE `CFG_ID` = '17'";
    				include("../includes/php/connect2.php");
    			}				
    		 }
    PHP:
    Any help would be greatly appreciated.

    Regards.
     
    karl_murphy, Feb 3, 2009 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Move all the code that performs the updates to the top of the script (before and of the SELECT statments), that way the database is updated before the information to be displayed is retrieved.
     
    mallorcahp, Feb 3, 2009 IP
  3. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    So should I moved everything within the POST statements above the SELECT statements as well as the form itself?
     
    karl_murphy, Feb 3, 2009 IP
  4. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, except the form can stay where it is (which is useful if you ever want to populate it with info from the SELECT statements) ...

    Try puttin this :

    At the beginning of the code you posted ...
     
    mallorcahp, Feb 3, 2009 IP
    karl_murphy likes this.
  5. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thank you very much, that works great!
     
    karl_murphy, Feb 4, 2009 IP